- MyBatis的各种动态sql写法
- 1、各种动态sql所需使用的标签
- 1.foreach 标签
- 2.where标签
- 3. sql标签
- 4.trim标签
- 2、批量添加、更新、删除
- 3、给一个类起别名
首先在mapper中接收到的方法参数应该是list、map或者array类型的。
使用方法如下
2.where标签#{item}
3. sql标签select * from user username = #{username} and sex = #{sex}
Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的
使用方法如下
4.trim标签userName, age, sex, salary, completed, remark #{item.userName}, #{item.age}, #{item.sex}, #{item.salary}, #{item.completed}, #{item.remark} insert into UserInfo(userName,age,sex,salary,completed,remark) VALUES ( ) insert into UserInfo( ) VALUES( )
2、批量添加、更新、删除insert into user id, userId, username, #{id}, #{userId}, #{username},
详细语法 请看上文sql标签的使用!3、给一个类起别名update UserInfo set age= when userInfoId=#{item.userInfoId} then #{item.age} where userInfoId in#{item.userInfoId} delete from UserInfo where userInfoId in #{item} update UserInfo where userInfoId in when userInfoId=#{item.userInfoId} then #{item.userName} when userInfoId=#{item.userInfoId} then #{item.age} when userInfoId=#{item.userInfoId} then #{item.sex} when userInfoId=#{item.userInfoId} then #{item.salary} when userInfoId=#{item.userInfoId} then #{item.completed} when userInfoId=#{item.userInfoId} then #{item.remark} #{item.userInfoId}
注: 目前能想到的关于动态sql的标签和相关知识点只有上面那么多,欢迎大家如果有知道新的标签,可以在下方留言,我会进行采纳整理的,大家一起来整理相关知识,我相信每一个人都会学到很多,感谢啦!
欢迎大家点赞收藏,一起学技术



