MyBatis动态SQL表达式详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

MyBatis动态SQL表达式详解

一个双子座的Java攻城狮   2022-12-27 我要评论

动态 sql 简单来讲就是我们能通过条件的设置生成不同的 sql,MyBatis 中常用的动态 sql 表达式主要是有五种:

  • if
  • choose (when, otherwise)
  • trim, where, set
  • foreach
  • sql

if

动态 sql 中最常见的场景是根据条件查询,比如要实现一个查询语句,当不传任何参数时查询所有用户,当传了 id 或者 name 时根据条件进行查询,就可以这样写:

<select id="selectUserByMap" parameterType="map" resultType="user">
    select * from user where 1=1
    <if test="id != null and id!=''">
        and id=#{id}
    </if>
    <if test="name != null and name!=''">
        and name=#{name}
    </if>
</select>

对应的 mapper 接口如下:

public interface UserMapper {
    List<User> selectUserByMap(Map<String,Object> param);
    void updateUser(Map<String,Object> param);
}

对应的 Java 代码如下:

Map<String,String> map=new HashMap<String, String>();
map.put("id","1");
map.put("name","javayz");
List<User> users = mapper.selectUserByMap(map);

当 map 中什么都不传时,sql 语句变为:

select * from user where 1=1

当传了 id 或 name 时,分别执行对应的查询。

choose when otherwise

choose 的使用很像 Java 中的 switch 语法,当满足第一个 when 条件时,就不去看后续的条件,如果条件都不满足,则执行 otherwise:

<select id="selectUserByChoose" parameterType="map" resultType="user">
    select * from user where 1=1
    <choose>
        <when test="id != null">
            and id=#{id}
        </when>
        <when test="name != null">
            and name=#{name}
        </when>
        <otherwise>
            and 1=1
        </otherwise>
    </choose>
</select>

trim where set

还记得前面的语法中都写了 where 1=1 吗,至于为什么这样写,目的在于让语法能顺利执行,以 if 语句为例:如果不写 1=1,语法就会变成下面这样:

<select id="selectUserByMap" parameterType="map" resultType="user">
    select * from user where
    <if test="id != null">
        id=#{id}
    </if>
    <if test="name != null">
        and name=#{name}
    </if>
</select>

这个时候如果满足了第一个 if 条件,那不会有问题,但是如果只满足第二个条件,语句就会变成:

select * from user where and name=?

语法直接报错。

MyBatis 提供了一种标签来代替 1=1 的写法,where 标签只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

<select id="selectUserByMap" parameterType="map" resultType="user">
    select * from user
    <where>
        <if test="id != null">
            and id=#{id}
        </if>
        <if test="name != null">
            and name=#{name}
        </if>
    </where>
</select>

set 的语法和 where 类似,在更新时会用到 set:

<update id="updateUser" parameterType="map">
    update user
    <set>
        <if test="name != null">name =#{name},</if>
    </set>
    where id = #{id}
</update>

使用 where 时会用自动去替换掉 and 或者 or,而使用 set 时会动态地在行首插入 SET 关键字,并会删掉额外的逗号。

使用 trim 可以用于去除拼接 sql 时的 and、or 关键字或者逗号等等。

之前使用 where 标签去除了最开始的 and 关键字,用 trim 也同样可以实现:

<select id="selectUserByMap" parameterType="map" resultType="map">
    select * from user
    <trim prefix="where" prefixOverrides="and">
        <if test="id != null">
            and id=#{id}
        </if>
        <if test="name != null">
            and name=#{name}
        </if>
    </trim>
</select>

foreach

foreach 在需要对集合进行遍历的场景中使用很广泛,尤其是在 in 语句中,比如下面这条语句:

select * from user where id in (1,2,3,4)

通过 foreach 实现方式如下,foreach 中的 ids 可以是参数传入的一个 List 集合:

<select id="selectUserByForeach" parameterType="map" resultType="User">
    select * from user where id in
        <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
</select>

sql片段

通过 sql 片段标签,可以将重复的 sql 提取出来,使用时通过 include 引用即可。

<sql id="select_user_where">
    <if test="id != null and id!=''">
        and id=#{id}
    </if>
    <if test="name != null and name!=''">
        and name=#{name}
    </if>
</sql>
<select id="selectUserByMap" parameterType="map" resultType="user">
    select * from user where 1=1
    <include refid="select_user_where">
</select>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们