文章目录
其实动态 sql 语句的编写往往就是一个拼接的问题,为了保证拼接准确,我们最好首先要写原生的 sql 语句出来,然后在通过 mybatis 动态sql 对照着改,防止出错。
【1】if 与 where 标签:
parmaeterType 中基本数据类型可直接写类型(Integer、String、Map 等),如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
<select id="selectUserByUsernameAndSex" resultMap="user" parameterType="com.pojo.User">
select * from user
<where>
<if test="username != null">
username=#{username}
</if>
<!--如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。-->
<if test="username != null">
and sex=#{sex}
</if>
</where>
</select>
【2】 if 与 set 标签:
如果标签返回的内容是逗号结尾的,则它会剔除掉逗号。
<update id="updateUserById" parameterType="com.pojo.User">
update user u
<set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex}
</if>
</set>
where id=#{id}
</update>
【3】choose、when 和 otherwise 标签:
选择其中的一个查询条件,一个满足即可,类似于 Java 的 switch 语句。
<select id="selectUserByChoose" resultType="com.pojo.User" parameterType="com.pojo.User">
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>
【4】trim 标签:
标记是一个格式化的标记,可以完成 set 或者是 where 标记的功能,prefix:前缀 、suffix:后缀。prefixoverride:去掉第一个配置的值,suffixoverride:去掉最后一个配置的值。
<select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.pojo.User">
select * from user
<trim prefix="where" prefixOverrides="and | or">
<if test="username != null">
and username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</trim>
</select>
两个使用实例:
trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码:
1、
select * from user
<trim prefix="WHERE" prefixoverride="AND |OR">
<if test="name != null and name.length()>0"> AND name=#{name}</if>
<if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
</trim>
假如说name和gender的值都不为null的话打印的SQL为:select * from user where name = ‘xx’ and gender = ‘xx’
在红色标记的地方是不存在第一个and的,上面两个属性的意思如下:
prefix:前缀
prefixoverride:去掉第一个and或者是or
2、
update user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
<if test="name != null and name.length()>0"> name=#{name} , </if>
<if test="gender != null and gender.length()>0"> gender=#{gender} , </if>
</trim>
假如说name和gender的值都不为null的话打印的SQL为:update user set name=‘xx’ , gender=‘xx’ where id=‘x’
在红色标记的地方不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,其中prefix意义如上:
suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
suffix:后缀
【5】sql 与 include 标签:
有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。
<!-- 定义 sql 片段 -->
<sql id="columns">
name,age
</sql>
<!-- 引入sql代码块-->
<select id="selectUser" resultMap="com.pojo.User" >
select <include refid="columns"/> from User
</select>
【6】foreach 标签:
当传入参数为数组或者集合时需要通过标签进行遍历。
参考文章,点击这里
foreach的说明:
collection:指定要遍历的集合
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符(表示该语句以什么开始)
close:遍历出所有结果拼接一个结束的字符(表示以什么结束)
index:索引。遍历list的时候是index就是索引,item就是当前值
遍历map的时候index表示的就是map的key,item就是map的值。
collection属性:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为
list - 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为
array - 如果传入的参数是多个的时候,我们就需要把它们封装成一个
Map了,当然单参数也可
<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
<where>
<!--
collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
select * from user where 1=1 and id in (1,2,3)
-->
<foreach collection="ids" item="id" open="and id in (" close=") " separator=",">
#{id}
</foreach>
</where>
</select>
本文深入解析MyBatis框架中的动态SQL语句编写技巧,包括if、where、set、choose、trim、sql与include及foreach标签的使用,旨在帮助开发者掌握如何灵活构造SQL语句,提高代码复用性和维护性。
402

被折叠的 条评论
为什么被折叠?



