网站建设教程软件,wordpress多页面传变量,扬州做网站公司,wordpress ios shared前言#x1f36d; ❤️❤️❤️SSM专栏更新中#xff0c;各位大佬觉得写得不错#xff0c;支持一下#xff0c;感谢了#xff01;❤️❤️❤️ Spring Spring MVC MyBatis_冷兮雪的博客-CSDN博客 终于到了MyBatis最后一篇#xff0c;这篇讲的是动态SQL的使用。
复杂情…前言 ❤️❤️❤️SSM专栏更新中各位大佬觉得写得不错支持一下感谢了❤️❤️❤️ Spring Spring MVC MyBatis_冷兮雪的博客-CSDN博客 终于到了MyBatis最后一篇这篇讲的是动态SQL的使用。
复杂情况动态SQL使用
动态 SQL 是Mybatis的强大特性之⼀能够完成不同条件下不同的 SQL 拼接。 可以参考官方文档mybatis – MyBatis 3 | 动态 SQL
一、if标签
在注册用户的时候可能会有这样⼀个问题有的信息是必填有的是选填那如果在添加⽤户的时候有不确定的字段传入程序应该 如何实现呢
这个时候就需要使用动态标签 if 来判断了比如添加的时候性别 sex 为非必填字段具体实现如下
insert idinsertinsert into user(username,password,if testsex ! nullsex,/ifbirthday,head) values (#{username},#{password},if testsex ! null#{sex},/if#{birthday},#{head})/insert 需要注意 test 中的 sex是传入对象中的属性不是数据库字段。而且if标签都是成对出现的 二、trim标签
之前的插入用户功能只是有⼀个 sex 字段可能是选填项如果所有字段都是非必填项就考虑使用trim标签结合if标签对多个字段都采取动态生成的方式。
trim标签中有如下属性
prefix表示整个语句块以prefix的值作为前缀 suffix表示整个语句块以suffix的值作为后缀 prefixOverrides表示整个语句块要去除掉的前缀 suffixOverrides表示整个语句块要去除掉的后缀
调整 UserMapper.xml 的插入语句为 insert idinsertinsert into usertrim prefix( suffix) suffixOverrides,if testusername ! nullusername,/ifif testpassword ! nullpassword,/ifif testsex ! nullsex,/ifif testbirthday ! nullbirthday,/ifif testhead ! nullhead,/ifif testcreateTime ! nullcreate_time,/if/trimtrim prefixvalues ( suffix) suffixOverrides,if testusername ! null#{username},/ifif testpassword ! null#{password},/ifif testsex ! null#{sex},/ifif testbirthday ! null#{birthday},/ifif testhead ! null#{head},/ifif testcreateTime ! null#{createTime},/if/trim/insert
在以上 sql 动态解析时会将第⼀个 trim 部分做如下处理
基于 prefix 配置前缀部分加上 ( 基于 suffix 配置后缀部分加上 ) 多个 if组织的语句都以 , 结尾在最后拼接好的字符串还会以 , 结尾会基于 suffixO verrides 配置去掉最后⼀个 , 注意 if test“createTime ! null” 中的 createTime 是传入对象的属性不是数据库字段
三、where标签
传入的用户对象根据属性做 where 条件查询用户对象中属性不为 null 的都为查询条件。如user.username 为 a则查询条件为 where usernamea
UserMapper
ListUser selectByCondition(User user);
UserMapper.xml: select idselectByCondition parameterTypecom.example.ssmdemo1.entity.Userinfo resultMapbaseMapselect id, username, password, nickname, sex, birthday, head, create_time from userwhereif testusername ! nulland username#{username}/ifif testpassword ! nulland password#{password}/ifif testsex ! nulland sex#{sex}/ifif testbirthday ! nulland birthday#{birthday}/ifif testhead ! nulland head#{head}/ifif testcreateTime ! nulland create_time#{createTime}/if/where/select 以上where标签也可以使用trim prefixwhere prefixOverridesand 替换中间的if标签代码不需要变。 四、set标签 根据传入的用户对象属性来更新用户数据可以使用set标签来指定动态内容。 UserMapper 接口中修改用户方法根据传入的用户 id 属性修改其他不为 null 的属性 int updateById(User user); UserMapper.xml 中添加更新用户 SQL update idupdateById parameterTypecom.example.ssmdemo1.entity.Userinfoupdate usersetif testusername ! nullusername#{username},/ifif testpassword ! nullpassword#{password},/ifif testsex ! nullsex#{sex},/ifif testbirthday ! nullbirthday#{birthday},/ifif testhead ! nullhead#{head},/ifif testcreateTime ! nullcreate_time#{createTime},/if/setwhere id#{id}/update 以上set标签也可以使用 trim prefixset suffixOverrides, 替换。 五、foreach标签 对集合进⾏遍历时可以使用该标签。foreach标签有如下属性 collection绑定方法参数中的集合如 ListSetMap或数组对象 item遍历时的每⼀个对象 open语句块开头的字符串 close语句块结束的字符串 separator每次遍历之间间隔的字符串 示例根据多个文章 id 来删除文章数据。 ArticleMapper 中新增接口方法 int deleteByIds(ListInteger ids); ArticleMapper.xml 中新增删除 SQL delete iddeleteByIdsdelete from article where id inforeach collectionlist itemitem open( close) separator,#{item}/foreach/delete 单元测试代码删除id为5和6的用户 Testvoid deleteByIds() {ListInteger idsnew ArrayList();ids.add(5);ids.add(6);int resultuserMapper.deleteByIds(ids);System.out.println(删除result);} 六、choose-when-otherwise 类似于Java中的switch语句根据不同条件选择不同的SQL片段。 select idgetUserList resultTypeUserSELECT * FROM userswherechoosewhen teststatus ! nullAND status #{status}/whenwhen testname ! null and name ! AND name #{name}/whenotherwiseAND active 1/otherwise/choose/where
/select动态SQL是MyBatis的一个重要特性它允许你在SQL语句中根据条件动态地添加、修改或删除语句片段以便更灵活地构建SQL查询和更新操作。 上面这些示例只是MyBatis动态SQL的一小部分用法。你可以根据自己的需求和情况结合使用这些特性来构建更灵活、可维护的数据库操作语句。记得阅读MyBatis的官方文档以深入了解动态SQL的更多用法和细节。