网站还是app,建设工程施工合同在哪个网站,自己学做网站看什么书,高新公司网站建设电话前言
本篇介绍MyBatis里如何使用动态SQL#xff0c;了解如何去简单使用动态标签#xff1b;如有错误#xff0c;请在评论区指正#xff0c;让我们一起交流#xff0c;共同进步#xff01; 文章目录 前言MyBatis - 动态 SQLif标签trim标签where标签update set 标签delet…
前言
本篇介绍MyBatis里如何使用动态SQL了解如何去简单使用动态标签如有错误请在评论区指正让我们一起交流共同进步 文章目录 前言MyBatis - 动态 SQLif标签trim标签where标签update set 标签delete foreach 标签 总结 本文开始 MyBatis - 动态 SQL
使用动态SQL的好处根据不同的条件拼接 SQL 语句提高了SQL的灵活性
if标签
if标签判断时使用满足test中的判断执行if条件 格式 if test“xxx ! null” /if -写在.xml文件中 -test中的是类中属性的值
trim标签
trim标签判断时使用去除后缀 与 if标签配合使用 格式 属性prefix: 这个语句前面加前缀例如 左括号 suffix: 这个语句前面加后缀例如 右括号 suffixOverrides去掉整个语句后缀例如 末尾的逗号 ,
示例
insert idadd4insert into userinfotrim prefix( suffix) suffixOverrides,if testusername ! nullusername,/ifif testpasswordpassword,/if/trimvaluestrim prefix( suffix) suffixOverrides,if testusername ! null#{username},/ifif testpassword ! null#{password},/if/trim/insertwhere标签
where标签查询时使用如果有添加条件就生成where语句没有就不添加条件不会生成where语句 -可以去掉前缀and (如下示例在只有第二个条件时where会自动去掉and)
示例 select idtestWhere resultTypecom.example.demo.model.Userinfoselect * from userinfo whereif testid 0id#{id}/ifif testusername ! nulland username#{username}/if/where/selectupdate set 标签
update set标签修改时使用 -会自动去掉最后的后缀逗号 update idupdateupdate userinfosetif testusername ! nullusername#{username},/ifif testpassword ! nullpassword#{password}/if/setwhere id#{id}/updatedelete foreach 标签
delete foreach标签删除时使用删除多个就需要遍历删除这就使用了foreach 属性collection: 为传递过来的集合名称 open: 语句前缀 close: 语句后缀 item: 遍历的每个对象的名称 separator: 每次遍历 的 分隔符 delete iddelByIdsdelete from userinfowhere id inforeach collectionlists open( close) itemid separator,#{id}/foreach/delete总结
✨✨✨各位读友本篇分享到内容如果对你有帮助给个赞鼓励一下吧 感谢每一位一起走到这的伙伴我们可以一起交流进步一起加油吧