简述MyBatis动态SQL

1|0 简述MyBatis动态SQL


1|0前言

  MyBatis是一个用于Java持久层的开源框架,它提供了一种简化数据库访问的方式。MyBatis的动态SQL功能允许我们根据不同的条件动态生成SQL语句,以实现更灵活的数据库操作。


在MyBatis中,我们经常使用以下标签来编写动态SQL:

1|0< if/>

作用:用于实现简单的条件判断,例如:非空判断,空值判断等。

注意:如果< if/>标签test属性的值为true则标签中的内容会执行;反之标签中的内容不会得到执行。使用< if/>时请注意,在test的属性值中不必再使用占位符#{参数名}的形式获取参数值,直接使用参数名获取对应的参数值即可。

List<User> queryUserWithIf(@Param("username") String username, @Param("password") String password); <select id="queryUserWithIf" resultType="User"> select * from user where 1=1 <if test="username !=null and username !='' "> and username=#{username} </if> <if test="password !=null and password !='' "> and password=#{password} </if> </select>

1|0< where/>

作用:

1.如果满足条件,那么标签将自动添加where关键字并自动删除查询条件中最前方多余的and或or关键字

2.如果不满足条件,那么标签失效而且不添加and或or关键字

3.如果没有where子句则不生成where关键字

注意:在< if/>标签的案例中为了防止SQL语句拼接后的意外报错特意加入了“where 1=1”的查询条件,以此避免了where后面第一个单词是and或者or之类的关键字。为了更合理地处理类似的状况,我们可以使用< where/>标签。

List<User> queryUserWithWhere(@Param("username") String username, @Param("password") String password); <select id="queryUserWithWhere" resultType="User"> select * from user <where> <if test="username !=null and username !=''"> and username=#{username} </if> <if test="password !=null and password !=''"> and password=#{password} </if> </where> </select>

1|0< choose/>、< when/>、< otherwise/>

作用:
< choose/>标签常与< when/>标签、< otherwise/>标签搭配使用以实现多分支选择,其使用方式和作用非常类似于Java语言中的switch-case-default语句,它只从多个分支中选一个执行。

List<User> queryUserWithChoose(@Param("username") String username, @Param("password") String password); <select id="queryUserWithChoose" resultType="User"> select * from user <where> <choose> <when test="username !=null and username !=''"> and username=#{username} </when> <when test="password !=null and password !=''"> and password=#{password} </when> <otherwise> and username like concat('%','lu','%') </otherwise> </choose> </where> </select>

1|0< set/>

作用:

1.如果满足条件,那么标签将自动添加set关键字

2.如果不满足条件,那么标签失效也不会添加set关键字

3.删除set子句中多余的逗号

注意:通常情况下,< set/>标签结合< if/>标签一起使用,主要用于管理映射文件中语句的set子句。

int updateUserWithSet(User user); <update id="updateUserWithSet" parameterType="User"> update user <set> <if test="username != null and username != ''"> username = #{username}, </if> <if test="password != null and password != ''"> password = #{password}, </if> <if test="gender != null and gender != ''"> gender = #{gender}, </if> </set> where id = #{id} </update>

1|0< trim/>

作用:< trim/>标签常用于用于在SQL语句前后添加或删除一些内容。

注意:
标签常用属性及其作用如下:

1.prefix:在SQL语句前添加内容。

2.prefixOverrides:删除SQL语句前多余的关键字或字符。

3.suffix:在SQL语句后添加内容。

4.suffixOverrides:删除SQL语句后多余的关键字或字符。

int updateUserWithTrim(User user); <update id="updateUserWithTrim" parameterType="User"> update user <trim prefix="set" suffixOverrides="and"> username = #{username} and </trim> where id = #{id} </update>

1|0< bind/>

作用:< bind/>标签用于数据绑定,常用于模糊查询。

List<User> queryUserWithBind(@Param("username") String username); <select id="queryUserWithBind" resultType="User"> select * from user <where> <if test="username !=null and username !=''"> <bind name="compositeUsername" value="'%'+username+'%'"/> username like #{compositeUsername} </if> </where> </select>

1|0< foreach/>

作用:< foreach/>标签用于在SQL语句中遍历List、数组、Map等集合。除此以外,该标签常用于SQL中的in查询。

注意:
标签常用属性及其作用如下:

1.collection:待遍历的集合。

2.item:当集合为List和数组时,index是对象的索引。
当集合是Map时,index是Map的键。

3.index:在SQL语句后添加内容。

4.open:表示开始符号,其常用值为"("。

5.separator:表示各元素之间的分隔符,其常用值为","。

6.close:表示结束符号,其常用值为")"。

// foreach遍历List List<User> queryUserWithForeach1(@Param("userIDList") List<Integer> userIDList); <!-- 测试foreach遍历List --> <select id="queryUserWithForeach1" resultType="User"> select * from user where id in <foreach collection="userIDList" open="(" separator="," close=")" item="userID"> #{userID} </foreach> </select> // foreach遍历数组 List<User> queryUserWithForeach2(@Param("userIDArray") int[] userIDArray); <!-- 测试foreach遍历数组 --> <select id="queryUserWithForeach2" resultType="User"> select * from user where id in <foreach collection="userIDArray" index="i" item="userID" open="(" separator="," close=")" > #{userID} </foreach> </select> // foreach与Map的使用 List<User> queryUserWithForeach3(@Param("userMap") Map<String ,Object> userMap); <!-- 测试foreach与Map的使用 --> <select id="queryUserWithForeach3" resultType="User"> select * from user where gender = #{userMap.gender} and id in <foreach collection="userMap.userIDList" item="userID" index="key" open="(" separator="," close=")" > #{userID} </foreach> </select>

1|0< sql/>

作用:< sql/>标签用于定义可重用的SQL片段,该标签常用属性为id作为SQL片段的唯一标识。

1|0< include/>

作用:< include/>标签常与< sql/>标签配合使用,即使用< include/>引用已经定义的SQL片段。< include/>标签的属性refid表示引用的< sql/>标签的id值。

List<User> queryUserWithInclude(@Param("username") String username, @Param("password") String password); <!-- 定义SQL片段 --> <sql id="columns">id,username,password,gender</sql> <!-- 测试动态SQL语句include --> <select id="queryUserWithInclude" resultType="User"> select <include refid="columns"/> from user <where> <if test="username !=null and username !=''"> and username=#{username} </if> <if test="password !=null and password !=''"> and password=#{password} </if> </where> </select>

1|0总结

  在项目的开发中,编码人员经常需要根据不同的条件拼接SQL语句。而在组拼SQL语句的过程中除了实现核心功能以外还需要处处小心,时时警惕确保不遗漏必要的标点符号、空格以及关键字。


__EOF__

本文作者一支萝卜
本文链接https://www.cnblogs.com/cjw2002/p/17758273.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   一支萝卜  阅读(108)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示