Mybatis动态SQL的实现示例

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

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

Mybatis动态SQL的实现示例

亦心谷   2020-10-14 我要评论

场景

在实际应用开发过程中,我们往往需要写复杂的 SQL 语句,需要拼接,而拼接SQL语句又稍微不注意,由于引号,空格等缺失可能都会导致错误。
Mybatis提供了动态SQL,也就是可以根据用户提供的参数,动态决定查询语句依赖的查询条件或SQL语句的内容。

动态SQL标签

if 和 where 标签

<!--动态Sql : where / if-->
  <select id="dynamicSql" resultType="com.lks.domain.User">
    select <include refid="tableAllkey"/> from users
    <where>
      <if test="id != null and id != 0">
        AND id = #{id}
      </if>
      <if test="name != null and name != ''">
        AND name = #{name}
      </if>
      <if test="county != null and county != ''">
        AND county = #{county}
      </if>
    </where>
  </select>

一般开发列表业务的查询条件时,如果有多个查询条件,通常会使用 标签来进行控制。 标签可以自动的将第一个条件前面的逻辑运算符 (or ,and) 去掉,正如代码中写的,id 查询条件前面是有“and”关键字的,但是在打印出来的 SQL 中却没有,这就是 的作用。打印SQL语句的使用可以在mybatis-config文件中添加setting标签:

<settings>
    <!-- 打印查询语句 -->
    <setting name="logImpl" value="STDOUT_LOGGING" />
  </settings>

choose、when、otherwise 标签

这三个标签需要组合在一起使用,类似于 Java 中的 switch、case、default。只有一个条件生效,也就是只执行满足的条件 when,没有满足的条件就执行 otherwise,表示默认条件。

<!--动态Sql: choose、when、otherwise 标签-->
  <select id="dynamicSql2" resultType="com.lks.domain.User">
    select * from users
    <where>
      <choose>
        <when test="name != null and name != ''">
          AND name = #{name}
        </when>
        <when test="county != null and county != ''">
          AND county = #{county}
        </when>
        <otherwise>
          AND id = #{id}
        </otherwise>
      </choose>
    </where>
  </select>

在测试类中,即使同时添加name和county的值,最终的sql也只会添加第一个属性值。

set 标签

使用set标签可以将动态的配置 SET 关键字,并剔除追加到条件末尾的任何不相关的逗号。使用 if+set 标签修改后,在进行表单更新的操作中,哪个字段中有值才去更新,如果某项为 null 则不进行更新,而是保持数据库原值。

<!--动态Sql: set 标签-->
  <update id="updateSet" parameterType="com.lks.domain.User">
    update users
    <set>
      <if test="name != null and name != ''">
        name = #{name},
      </if>
      <if test="county != null and county != ''">
        county = #{county},
      </if>
    </set>
    where id = #{id}
  </update>

trim 标签

trim 是一个格式化标签,可以完成< set > 或者是 < where > 标记的功能。主要有4个参数:
① prefix:前缀

② prefixOverrides:去掉第一个and或者是or

③ suffix:后缀

④ suffixOverrides:去掉最后一个逗号,也可以是其他的标记

<!--动态Sql: trim 标签-->
  <select id="dynamicSqlTrim" resultType="com.lks.domain.User">
    select * from users
    <trim prefix="where" suffix="order by age" prefixOverrides="and | or" suffixOverrides=",">
      <if test="name != null and name != ''">
        AND name = #{name}
      </if>
      <if test="county != null and county != ''">
        AND county = #{county}
      </if>
    </trim>
  </select>

foreach 标签

foreach标签主要有以下参数:
item :循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details,在list和数组中是其中的对象,在map中是value。
index :在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。
open :表示该语句以什么开始
close :表示该语句以什么结束
separator :表示元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。

list批量插入

 <!--动态Sql: foreach标签, 批量插入-->
    <insert id="dynamicSqlInsertList" useGeneratedKeys="true" keyProperty="id">
      insert into users (name, age, county, date)
      values
      <foreach collection="list" item="user" separator="," >
        (#{user.name}, #{user.age}, #{user.county}, #{user.date})
      </foreach>
    </insert>

在这里插入图片描述

从结果可以看出,我们一下插入了两条数据,每条数据之间使用“,”进行分割,separator="," 的作用就是如此。其中< foreach >标签内部的属性务必加上item.

list集合参数

<!--动态Sql: foreach标签, list参数查询-->
  <select id="dynamicSqlSelectList" resultType="com.lks.domain.User">
    SELECT * from users WHERE id in
    <foreach collection="list" item="id" open="(" close=")" separator="," >
      #{id}
    </foreach>
  </select>

在这里插入图片描述

可以看出我们的 SQL 语句新增了:( ? , ? ) ,前后的括号由 open="(" close=")" 进行控制,用“?”占位符占位,并通过separator以:“,”隔开,内部两个循环遍历出的元素。array 集合与 list 的做法也是类似的:

<!--动态Sql: foreach标签, array参数查询-->
  <select id="dynamicSqlSelectArray" resultType="com.lks.domain.User">
    select * from users WHERE id in
    <foreach collection="array" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
  </select>

map参数

< map> 标签需要结合MyBatis的参数注解 @Param()来使用,需要告诉Mybatis配置文件中的collection="map"里的map是一个参数:

<!--动态Sql: foreach标签, map参数查询-->
  <select id="dynamicSqlSelectMap" resultType="com.lks.bean.User">
    select * from users WHERE
    <foreach collection="map" index="key" item="value" separator="=">
      ${key} = #{value}
    </foreach>
  </select>

需要主要${}和#{}的使用。

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

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