个人网站做什么类型好,新站加快网站收录,玉山建设局网站,中国百强城市榜单说明#xff1a;之前介绍过MyBatis的用法#xff0c;像 用注解和Mapper.xml操作数据库、在Mapper.xml里写动态SQL。最近在一次用MyBatis批量更新数据库对象的场景中#xff0c;意识到对MyBatis的一些标签用法不太熟悉#xff0c;所以去 MyBatis官网 看了一些文档#xff0…说明之前介绍过MyBatis的用法像 用注解和Mapper.xml操作数据库、在Mapper.xml里写动态SQL。最近在一次用MyBatis批量更新数据库对象的场景中意识到对MyBatis的一些标签用法不太熟悉所以去 MyBatis官网 看了一些文档基于此本文介绍一些可能会用到的MyBatis用法。
$ 占位符
通常我们会使用#{}表示占位符即该位置的值使用传递的参数在运行SQL拼接时会用 ?占位在执行SQL的时候才会将我们的参数替换执行不会有SQL注入的风险。如下
controller层 /*** 查询用户* param id* return*/GetMapping(/getUser/{id})public String getUser(PathVariable(id) String id){return userMapper.getUser(id).toString();}mapper层 Select(select * from user where id #{id})User getUser(String id);控制台打印的执行日志 换成 ${}再看下 Select(select * from tb_user where id ${id})User getUser(String id);查看控制台可以看到执行过程是直接将参数进行拼接的 这么说那还需要这个干什么直接用#{}不就行了。我在查看官网文档时文档提到了一个用法可以动态查询数据库表中的某列字段如下 Select(select ${field} from tb_user where id #{id})User getUser(String field, String id);field表示User表中的任意字段该接口的作用就是根据ID查询User表中的任意字段值该字段可用前端、或者Service层判断后传入
field传username表示根据ID查询username field传password表示根据ID查询password 另外如果可以你还可以在 order by 后面方放一个 ${sortField}根据某字段排序让结果集的排序可根据前端或者Service层来动态的控制非常灵活。这也算是$ 占位符的一抹光辉。
使用上的注意事项官网中有一段说明如下 script 标签
我们都知道使用Mabatis框架操作数据库有两种方式一种是在Mapper中的接口上写注解注解里写SQL语句就像上面那样一种是在对应的Mapper.xml中写SQL关联到具体的Mapper接口如下
namespace里写Mapper的全限定类名mapper标签里面写对应类里的接口id为方法名resultType为结果集封装的对象的全限定类名
mapper namespacecom.hezy.mapper.UserMapperselect idgetUser resultTypecom.hezy.pojo.Userselect *from tb_userwhere id #{id}/select
/mapper关于这两种方式该怎么选择官网中也有一段非常优雅的描述如下 而 script标签 的作用就是把Mapper.xml中的动态SQL写在注解里如下根据ID更新数据在Mapper.xml我们是这么写的。 update idupdateUserByIdupdate tb_usersetif testusername ! null and username ! username #{username},/ifif testpassword ! null and password ! password #{password},/if/setwhere id #{id}/update如果你不想写在Mapper.xml里面虽然不知道你为什么不想就可以使用 script标签 我们可以把它写在注解里如下 Update(script update tb_user set if testusername ! nullusername #{username},/if if testpassword ! nullpassword #{password}/if where id #{id} /set /script)void updateUserById(User user);虽然有点麻烦我调试了一会儿但效果是一样
trim 标签
在介绍trim标签前先介绍我们经常会用到的两个标签set标签 和 where标签
set标签 在更新数据时使用如下 update idupdateUserByIdupdate tb_usersetif testusername ! null and username ! username #{username},/ifif testpassword ! null and password ! password #{password},/if/setwhere id #{id}/update它的作用是可以在拼接SQL时删掉多于的逗号,比如上面这段动态SQL当password值不为空时拼接出来的SQL是下面这样的
update tb_user set username zhangsan_fix, password 123456_fix, where id 1执行会报错 where标签 在复杂的条件查询时使用如下 select idqueryUser resultTypecom.hezy.pojo.Userselect * from tb_userwhereif testid ! null and id ! and id #{id}/ifif testusername ! null and username ! and username #{username}/ifif testpassword ! null and password ! and password #{password}/if/where/select它的作用是可以在拼接SQL时删掉多于的 and 或者 or像上面的动态SQL如果id不为空就会在where 后面拼接一个莫名其妙的and这样的SQL同样是执行不成功的。 而 trim 标签的作用是可以自定义选择在哪个关键字附近添加或删减什么字符。trim 标签属性如下 prefix前面拼接的内容 suffix后面拼接的内容 prefixOverrides去除的前缀内容 suffixOverrides去除的后缀内容
trim 标签可以起到set标签 和 where标签相同的作用如下
trim标签里面的语句前面加个 set末尾多于的逗号去掉等同于 set标签 update idupdateUserByIdupdate tb_usertrim prefixset suffixOverrides,if testusername ! null and username ! username #{username},/ifif testpassword ! null and password ! password #{password},/if/trimwhere id #{id}/update控制台打印的日志信息 trim标签里面的语句前面加个 where去掉前面多于的and 或者 or 等同于 where标签 select idqueryUser resultTypecom.hezy.pojo.Userselect * from tb_usertrim prefixwhere prefixOverridesand |or if testid ! null and id ! and id #{id}/ifif testusername ! null and username ! and username #{username}/ifif testpassword ! null and password ! and password #{password}/if/trim/select控制台打印的日志信息 官网中有说明and |or 这里的空格是必要的但我去掉之后执行时没有问题的或许是考虑在一些特殊的场景没有空格是会有问题的。我想不到有读者想得到请告诉我do ze。 总结
本文介绍了MyBatis一些补充用法