关键字if+trim

trim可以去除多余的关键字,是where和set的组合

trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码:

<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
prefix:在trim标签内sql语句加上前缀。
suffix:在trim标签内sql语句加上后缀。
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
prefixOverrides:指定去除多余的前缀内容
<select id="select03" resultMap="BaseResultMap">
select * from personDemo
<trim prefix="where" prefixOverrides="and|or">
//这里的trim意思是代替where,并且在某个条件下,把多余的and或者or去掉比如name为空的时候
//第二条age是不是会有个and,这样sql语句就错了,这时候prefixOverride就可以把and去掉
//prefixOverrides去掉前缀的and 关键字
<if test="name !=null and name!=''">
name=#{name}
</if>
<if test="age !=null and age!=''">
and age=#{age}
</if>
<if test="id !=null and id!=''">
and id=#{id}
</if>
</trim>
</select>
<update id="update02" parameterType="com.demo.mybatisdemo.bean.Person">
update personDemo
<trim prefix="set" suffixOverrides=",">
<if test="name != null and name != ''">
name=#{name},
</if>
<if test="age != null and age !=''">
age=#{age}
</if>
</trim>
where id=#{id}
</update> 标签<choose><when><otherwise>
这个类似于java里面的switch标签,首先在choose标签里面,加入判断条件<when>如果有一个when的条件符合,那么久不执行其他when标签,假如所有when标签都不符合,那么就执行
<otherwise>标签里面的内容
<update id="update03" parameterType="com.demo.mybatisdemo.bean.Person"> update personDemo
  <set>
<choose>
<when test="name!=null and name.length>0">
name=#{name}
</when>
<when test="age!=null and age.length>0">
age=#{age}
</when>
<otherwise>
name='BBBBB'
</otherwise>
</choose>
</set>
where id=#{id}
</update> 还有标签<foreach>,多配合 关键字in使用
这个标签就是循环,里面有collection,item,open,separator,close
collection接口,例如传入一个数组ids,那么这个值为array,如果是list,那么这个值为list
item为传入的值的名字,如传入String[] ids 那么item为ids
open,即开头拼接的字符
close,结束拼接的字符
separator,中间分割的字符


<select id="select04" resultMap="BaseResultMap">
select * from personDemo where id in <foreach collection="array" item="ids" open="(" separator="," close=")">
#{ids}
</foreach> </select>
上面这段SQL打印出来为:select * from personDemo where id in ( ? , ? ) 

最新文章

  1. 深入理解Java多态机制
  2. DBLink创建 ORA-12154: TNS: 无法解析指定的连接标识符
  3. ASP.NET MVC 程序 报错“CS0012: 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义”的解决办法
  4. 《自己动手写操作系统》pmtest2笔记
  5. HDU 4617 Weapon (简单三维计算几何,异面直线距离)
  6. copy&amp;deepcopy
  7. 第三方Charts绘制图表四种形式:饼状图,雷达图,柱状图,直线图
  8. 【最小生成树】Bzoj1232 [Usaco2008Nov]安慰奶牛cheer
  9. eclipse 开发web 项目,使用gradle 需要安装的插件
  10. python3 整数类型PyLongObject 和PyObject源码分析
  11. [工具向]__申请,下载,使用百度地图api
  12. linux内核里的字符串转换 ,链表操作常用函数(转)
  13. 考虑实现一个不抛异常的swap
  14. ADI SHARC 学习之PLL
  15. 让可等待的计时器添加APC调用
  16. C# 16进制与字符串、字节数组之间的转换
  17. Unlicensed ARC session – terminating!
  18. 【LeetCode】140. Word Break II
  19. 【Python入门学习】闭包&amp;装饰器&amp;开放封闭原则
  20. Java八种基本类型

热门文章

  1. BFS+PRIM
  2. windows虚拟机下 安装docker 踩过的坑
  3. Swoole和Workerman到底选谁?
  4. 51Nod 1013 3的幂的和(快速幂+逆元)
  5. iOS 获取类的字符串名称 Swift4
  6. Homebrew 常用命令
  7. jmeter(二十一)JMeter 命令行(非GUI)
  8. 514 Freedom Trail 自由之路
  9. D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash
  10. 分享几个自己喜欢的前端UI框架