动态sql
通过mybatis提供的各种标签方法实现动态拼接sql。
1.1 if标签 if就是用来对输入映射的字段进行判断 一般是非空判断 null ""
1.2动态SQL
1.3choose—when--when--otherwise when可以多个 otherwise只能有一个 类似于 switch case
需求:输入用户id 按照用户id进行精确查找 其他条件不看 没有输入 id 用户名模糊查找 都没有的话 查询id=1的用户
1.4 动态sql之set 代替set关键字 set标签可以帮助我们去掉最后一个逗号
update users where uid=#{uid} uname =#{uname}, upwd =#{upwd}, sex =#{sex}, birthday =#{birthday},
1.5 trim
1.trim代替where
select * from users
2.代替set
update users
1.6动态sql foreach
用来进行遍历 数组 List
1.用来进行遍历
select * from users where uid in
#{item}
2.用来遍历集合 批量删除
delete from users where uid in
#{item}
1.7 sql片段
Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:
select uid,uname,upwd,sex,birthday
引用:
閱讀更多 跟老司機學Java 的文章