1.<if>

<select id="" parameterType="" resultType="">
select * from user where 1 = 1
<if test="username != null">
and username = #{username}
</if>
<if test="userCode != null">
and user_code = #{userCode}
</if>
</select>

2.<choose>

<select id="" parameterType="" resultType="">
select * from user where 1 = 1
<choose>
<when test="username != null">
and username = #{username}
</when>
<when test="userCode != null">
and user_code = #{userCode}
</when>
<otherwise>
and user_id = "1"
</otherwise>
</choose>
</select>

  相当于Java中的switch语句,当满足when中条件时即执行相应内容,都不满足则执行otherwise中的内容; 它是按顺序执行,只要when中只要满足一条即刻跳出choose,即:所有的when和otherwise条件中只满足一条.

3.<where>

<select id="" parameterType="" resultType="">
select * from user
<where>
<if test="username != null">
username = #{username}
</if>
<if test="userCode != null">
and user_code = #{userCode}
</if>
</where>
</select>

4.<trim>

<select id="" parameterType="" resultType="">
select * from user
<trim prefix="where" prefixOverrides="and |or"> --添加前缀where   去掉第一个and或者or
<if test="username != null">
username = #{username}
</if>
<if test="userCode != null">
and user_code = #{userCode}
</if>
</trim>
</select>

  可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;

  可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;

  trim的此功能,我们可以非常简单的利用trim来代替where元素的功能.

5.<set>

<update id="" parameterType="">
update user
<set>
<if test="username != null">
username = #{username},
</if>
<if test="userCode != null">
user_code = #{userCode},
</if>
</set>
where user_id = #{userId}
</update>

  用于更新操作;

  如果包含的语句是以逗号结束的话将会把该逗号忽略;

  如果set中一个条件都不满足,即set中包含的内容为空的时候就会报错.

6.<foreach>

<select id="" resultType="">
select * from user where user_id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

  item表示集合中每一个元素进行迭代时的别名;

  index指定一个名字,用于表示在迭代过程中,每次迭代到的位置;

  open表示该语句以什么开始;

  separator表示在每次进行迭代之间以什么符号作为分隔符;

  close表示以什么结束;

  在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,

  该属性的值是不一样的:

    -如果传入的是单参数且参数类型是一个List的时候,collection属性值为list;

    -如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array;

    -如果传入的参数是多个的时候,把它们封装成一个Map了,当然单参数也可以封装成map,

      实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,

      所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key

最新文章

  1. ie6 ie7 ie8 ie9兼容问题终极解决方案
  2. iOS - Json解析精度丢失处理(NSString, Double, Float)
  3. Ubuntu16.04安装vim插件YouCompleteMe
  4. &lt;转&gt;iOS9 Day-by-Day:iOS开发者必须了解的iOS 9新技术与API
  5. Delphi操作XML简介
  6. UIApplication和delegate代理
  7. myeclipse关闭html,jsp等页面的可视化编辑器
  8. 推荐一个很棒的JS绘图库Flot
  9. SqlSever基础 两个条件 group by 分组显示
  10. 1043: [HAOI2008]下落的圆盘 - BZOJ
  11. SQL DMO のDMV
  12. [Lua]Mac系统上安装Lua环境
  13. Dynamics 365-ExecuteWorkflowRequest
  14. 关闭 synactive guixt. 在sap gui的右上角一个标志里,将 active guixt 选项去掉即可。
  15. git 冲突解决的方法
  16. listcontrolc插入列时,出现断言错误
  17. pro*c的使用
  18. [POI2015]LOG(树状数组)
  19. linux信息收集篇之sosreport
  20. vue 将值存储到vuex 报错问题

热门文章

  1. VUE 轮询、轮询终止 beforeRouteLeave
  2. Python修改柱状图边缘柱子与图边界的距离
  3. vue学习笔记(二) ---- vue实例的生命周期
  4. 借助 Flutter 跨平台特性连接 10 亿玩家 | Flutter 开发者故事
  5. 基于APIView&amp;ModelSerializer写接口
  6. 【论文笔记】FCN全卷积网络
  7. 高效、优雅的对象copy之MapStruct入门到精通,实战踩坑版
  8. qt元对象系统之 Q_OBJECT宏
  9. CentOS7一键安装RPM版LNMP(NGINX+PHP+MySQL)环境
  10. 网页实时显示已经运行了多少天 html+js