MybatisSQL动态语句

MyBatis 的动态 SQL 元素与 JSTL 或 XML 文本处理器相似,

常用 <if>、<choose>、<when>、<otherwise>、<trim>、<where>、<set>、<foreach> 和 <bind> 等元素。

一、if标签:条件判断

MyBatis 中 <if> 元素是最常用的元素,它类似于 Java 中的 if 语句。具体过程如下:

在 com.mybatis 包的 UserMapper.xml 文件中添加如下 SQL 映射语句:

<!--使用 if 元素根据条件动态查询用户信息-->
<select id="selectUserByIf" resultType="com.po.MyUser" parameterType="com.po.MyUser">
    select * from user where 1=1
    <if test="uname!=null and uname!=''">
        and uname like concat('%',#{uname},'%')
    </if >
    <if test="usex !=null and usex !=''">
        and usex=#{usex}
    </if >
</select>

测试:

// 使用 if 元素查询用户信息
MyUser ifmu=new MyUser();
ifmu.setUname ("张");
ifmu.setUsex ("女");
List<MyUser> listByif=userDao.selectUserByIf(ifmu);
System.out.println ("if元素================");
for (MyUser myUser:listByif) {
    System.out.println(myUser);
}

if 条件判断+where

<select id="findByConditionWithIf" resultType="com.ykq.entity.Account">
         select * from account
        <where>
             <if test="name!=null and name!=''">
                 and name like concat('%',#{name},'%')
             </if>
             <if test="isdeleted!=null">
                  and isdeleted=#{isdeleted}
             </if>
        </where>
    </select>

二、choose+when+otherwise

有些时候不想用到所有的条件语句,而只想从中择取一二,针对这种情况,MyBatis 提供了 <choose> 元素,它有点像 Java 中的 switch 语句。具体过程如下:

<!--使用choose、when、otherwise元素根据条件动态查询用户信息-->
<select id="findByConditionWithChoose" resultType="com.ykq.entity.Account">
    select * from account
  <where>
    <choose>
      <when test="name!=null and name!=''">
        and name like concat('%',#{name},'%')
      </when>
      <when test="isdeleted!=null">
        and isdeleted=#{isdeleted}
      </when>
      <otherwise>
        <![CDATA[and money <1000 ]]>
      </otherwise>
    </choose>
  </where>
</select>

三、<trim>元素

<trim> 元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是 prefix 和 suffix。

可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是 prefixOverrides 和 suffixOverrides。正因为 <trim> 元素有这样的功能,所以也可以非常简单地利用 <trim> 来代替 <where> 元素的功能。

<select id="findByConditionWithChoose" resultType="com.ykq.entity.Account">
          select * from account
                 <trim prefix="where" prefixOverrides="or|and" >
                       <choose>
                           <when test="name!=null and name!=''">
                                and name like concat('%',#{name},'%')
                           </when>
                           <when test="isdeleted!=null">
                               and isdeleted=#{isdeleted}
                           </when>
                           <otherwise>
                             <![CDATA[or money <1000 ]]>
                           </otherwise>
                       </choose>
                 </trim>
    </select>

四、foreach标签

<foreach> 元素主要用在构建 in 条件中,它可以在 SQL 语句中迭代一个集合。

<foreach> 元素的属性主要有 item、index、collection、open、separator、close。

  • item 表示集合中每一个元素进行迭代时的别名。
  • index 指定一个名字,用于表示在迭代过程中每次迭代到的位置。
  • open 表示该语句以什么开始。
  • separator 表示在每次进行迭代之间以什么符号作为分隔符。
  • close 表示以什么结束。

在使用 <foreach> 元素时,最关键、最容易出错的是 collection 属性,该属性是必选的,但在不同情况下该属性的值是不一样的,主要有以下 3 种情况:

    • 如果传入的是单参数且参数类型是一个 List,collection 属性值为 list。
    • 如果传入的是单参数且参数类型是一个 array 数组,collection 的属性值为 array。
    • 如果传入的参数是多个,需要把它们封装成一个 Map,当然单参数也可以封装成 Map。Map 的 key 是参数名,collection 属性值是传入的 List 或 array 对象在自己封装的 Map 中的 key。
<select id="findByConditionWithFor" resultType="com.ykq.entity.Account">
         select * from account
         <where>
              <if test="ids!=null and ids.length>0">
                  id in
                  <foreach collection="ids" open="(" close=")" separator="," item="id">
                      #{id}
                  </foreach>
              </if>
         </where>
    </select>
原文地址:https://www.cnblogs.com/axinga/p/14571267.html