Mybatis 9种动态 sql 标签使用

MyBatis提供了9种动态SQL标签:trim、where、set、foreach、if、choose、when、otherwise、bind;

1.if 标签

XML 复制代码
<select id="getUser">
    select * from User
    <where>
        <if test=" age != null ">
            and age > #{age}
        </if>
        <if test=" name != null ">
            and name like concat(#{name},'%')
        </if>
    </where>
</select>

2.choose 标签、when 标签、otherwise 标签

XML 复制代码
<select id="getUser">
    select * from User
    <where>
        <choose>
            <when test=" age != null ">
                and age > #{age}
            </when>
            <when test=" name != null ">
                and name like concat(#{name},'%')
            </when>
            <otherwise>
                and sex = '女'
            </otherwise>
        </choose>
    </where>
</select>

3.foreach 标签

XML 复制代码
<select id="findAll">
    select  * from user where ids in
    <foreach collection="list"
             item="item" index="index"
             open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

最终拼接完整的语句就变成:
select  * from user where ids in (1,2,3,...,100);

当然你也可以这样编写:
<select id="findAll">
    select  * from user where
    <foreach collection="list"
             item="item" index="index"
             open=" " separator=" or " close=" ">
        id = #{item}
    </foreach>
</select>

最终拼接完整的语句就变成:
select  * from user where id =1 or id =2 or id =3  ... or id = 100;

4.where 标签、set 标签

XML 复制代码
<select id="getUser">
    select * from User
    <where>
        <if test=" age != null ">
            and age > #{age}
        </if>
        <if test=" name != null ">
            and name like concat(#{name},'%')
        </if>
    </where>
</select>
XML 复制代码
<update id="updateUser">
    update user
    <set>
        <if test="age !=null">
            age = #{age},
        </if>
        <if test="username !=null">
            username = #{username},
        </if>
        <if test="password !=null">
            password = #{password},
        </if>
    </set>
    where id =#{id}
</update>

注意,set 标签之所以能够支持去除前缀逗号或者后缀逗号,
是由于其在构造 trim 标签的时候进行了前缀后缀的去除设置,而 where 标签在构造 trim 标签的时候就仅仅设置了前缀去除。

5.trim 标签

XML 复制代码
<trim prefix="SET" prefixOverrides="," >
    ...
</trim>

或者

<trim prefix="SET" suffixesToOverride="," >
    ...
</trim>

由于 where 标签和 set 标签这两种 trim 标签变种方案已经足以满足我们实际开发需求,所以直接使用 trim 标签的场景实际上不太很多(其实是我自己使用的不多,基本没用过)。

6.bind 标签

XML 复制代码
平时你使用 mysql 都是如何拼接模糊查询 like 语句的 ( oracle 不支持)
select * from user where name like concat('%',#{name},'%')


<select id="selecUser">
    <bind name="myName" value="'%' + _parameter.getName() + '%'" />
    SELECT * FROM user
    WHERE name LIKE #{myName}
</select>

无论使用哪种数据库,这个模糊查询的 Like 语法都是支持的

拓展:sql标签 + include 标签

XML 复制代码
<!-- 可复用的字段语句块 -->
<sql id="userColumns">
    id,username,password
</sql>
查询或插入时简单复用:

<!-- 查询时简单复用 -->
<select id="selectUsers" resultType="map">
    select
    <include refid="userColumns"></include>
    from user
</select>
相关推荐
pjx9874 分钟前
服务间的“握手”:OpenFeign声明式调用与客户端负载均衡
java·运维·spring·负载均衡
prinrf('千寻)36 分钟前
MyBatis-Plus 的 updateById 方法不更新 null 值属性的问题
java·开发语言·mybatis
老华带你飞42 分钟前
实习记录小程序|基于SSM+Vue的实习记录小程序设计与实现(源码+数据库+文档)
java·数据库·spring boot·小程序·论文·毕设·实习记录小程序
在未来等你1 小时前
互联网大厂Java求职面试:AI与大模型应用集成及云原生挑战
java·微服务·ai·kubernetes·大模型·embedding·spring ai
源码技术栈1 小时前
SaaS基于云计算、大数据的Java云HIS平台信息化系统源码
java·大数据·云计算·云his·his系统·云医院·区域his
编程、小哥哥2 小时前
互联网大厂Java面试:从Spring Boot到微服务架构的技术深挖
java·spring boot·redis·微服务·prometheus·面试技巧
揽你·入怀2 小时前
数据结构:ArrayList简单实现与常见操作实例详解
java·开发语言
okok__TXF2 小时前
SpringBoot3+AI
java·人工智能·spring
AA-代码批发V哥2 小时前
Math工具类全面指南
java·开发语言·数学建模
wangzhongyudie3 小时前
SQL实战:06交叉日期打折问题求解
数据库·sql