Mybatis动态sql

动态sql

通过mybatis提供的各种标签方法实现动态拼接sql。

1.1 if标签 if就是用来对输入映射的字段进行判断 一般是非空判断 null ""

1.2动态SQL 相当于 where关键字 可以自动处理第一个前and 或者or。 当条件都没有的时候 where也不会加上 。

1.3choose—when--when--otherwise when可以多个 otherwise只能有一个 类似于 switch case

需求:输入用户id 按照用户id进行精确查找 其他条件不看 没有输入 id 用户名模糊查找 都没有的话 查询id=1的用户 ​ 

1.4 动态sql之set 代替set关键字 set标签可以帮助我们去掉最后一个逗号

  update users     uname =#{uname},  upwd =#{upwd},  sex =#{sex},  birthday =#{birthday},  where uid=#{uid} 

1.5 trim

1.trim代替where

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

引用:


分享到:


相關文章: