MyBatis学习动态SQL

一.概述

MyBatis提供对SQL语句动态的组装能力,而且他有几个基本元素,我们可以通过对这些元素的使用达到我们的目的。

MyBatis的动态SQL包括以下几种元素:

1.if元素
if元素是我们最常用到的判断语句,它常常和test属性联合使用。

下面我们学习如何使用:
现在我们要根据角色名称去查找角色,但是角色名称是一个可填可不填的条件,不填写的时候就不将它作为查询条件,我们用if就可以实现它:

<select id="findRoles" parameterType="string" resultMap="roleResultMap">
 select role_no, role_name,note from t_role where 1=1
 <if test="roleName != null and roleName !=''">
  and role_name like concat('%',#{roleName},'%')
 </if>
</select>

上面代码的含义是当我们将参数roleName传递到映射器中,采取构造对roleName的模糊查询,如果这个参数为空,就不要去构造这个条件。

2.choose ,when, otherwise元素
这里是针对多种情况的元素,类似于switch case 语句,在映射器的动态语句中choose,when ,otherwise元素承担了这个功能。

如下:
当角色编号不为空时,则只用角色编号作为查询条件
当角色编号为空时,而角色名称不为空时,则用角色名称作为条件进行模糊查询
当角色编号和角色名称都为空,则要求角色备注不为空。

<select id="findRoles" parameterType="role" resultMap="roleResultMap">
  select role_no, role_name,note from t_role where 1=1
  <choose>
     <when test="roleNo != null and roleNo !=''">
        AND role_no = #{roleNo}
     </when>
     <when test="roleName != null and roleName !=''">
     AND role_name like concat('%',#{roleName},'%')
     </when>
     <otherwise>
      AND note is not null
     </otherwise>
  </choose>
</select>

这样MyBatis就会根据参数的设置进行判断来动态组装SQL以满足不同的业务要求。

3.trim,where set元素
where用法:

<select id="findRoles" parameterType="string" resultMap="roleResultMap">
  select role_no, role_name ,note from t_role
  <where>
  <if test="roleName !=null and roleName !=''">
  and role_name like concat('%',#{roleName},'%')
  </if>
  </where>
</select>

当where元素里面的条件成立的时候,才会加入where这个SQL关键字到组装的SQL里面,否则就不加入。

有时候我们要去掉一些特殊的SQL语法,比如常见的and,or,而使用trim元素可以达到:

<select id="findRoles" parameterType="string" resultMap="roleResultMap">
  select role_no, role_name ,note from t_role
 <trim prefix="where" prefixOverrides="and">
  <if test="roleName !=null and roleName !=''">
   and role_name like concat('%',#{roleName},'%')
  </if>
 </trim>
</select>

trim元素就意味着我们需要去掉一些特殊的字符串,prefix代表的是语句的前缀,而prefixOverrides代表的是你需要去掉的那种字符串,上面的写法基本与where是等效的。

set元素用于更新表的数据:

<update id="updateRole" parameterType="role">
  update t_role
  <set>
   <if test="roleName != null and roleName !=''">
     role_name =#{roleName} ,
   </if>
   <if test="note !=null and note !=''">
    note =#{note}
   </if>
  </set>
  where role_no =#{roleNo}
</update>

set元素遇到了逗号,他会把对应的逗号去掉,如果我们自己编写那将是多少次的判断呢?当我们只想更新备注,我们只需要传递备注信息和角色编号即可,而不需要再传递角色名称,MyBatis就会根据参数的规则进行动态SQL组装。

4.foreach元素
foreach是一个循环语句,它的作用是遍历集合,它能够很好的支持数组和List , Set接口的集合,对此提供遍历的功能。

在数据库中,数据字典是经常使用的内容,比如在用户表中,性别可以分为男,女或者未知,我们把性别作为一个字典:
1-男 2-女 0-未知

实际工作中,用户可能查找非未知性别的用户,也可能查找女性和未知,还可能查找男性和未知
具体的参数需要使用foreach元素去确定,代码如下:

<select id="findUserBySex" resultType="user">
 select * from t_user where sex in
 <foreach item="sex" index="index" collection="sexList" open="("separator="," close=")">
 #{sex}
 </foreach>
</select>

collection配置的sexList是传递进来的参数名称,它可以是一个数组或者List,Set等集合。
item配置的是循环中当前的元素。
index配置的是当前元素在集合的位置下标。
open和close配置的是以什么符号将这些集合元素包装起来。
separator是各个元素的间隔符。

5.test的属性
test的属性用于条件判断的语句中的,它的作用相当于判断真假。在大部分的场景下我们都是用它来判断空和非空。有时候我们需要判断字符串,数字和枚举等。所以十分有必要讨论一下它的用法。前面我们学习了if元素,我们知道了如何判断非空,但是如何用if语句判断字符串呢?

<select id="getRoleTest" parameterType="string" resultMap="roleResultMap">
  select role_no, role_name,note from t_role 
  <if test="type = 'Y'">
   where 1=1
  </if>
</select>

把type="Y"传递给这条SQL,我们发现程序自动加入了where 1=1.换句话说,这条语句判断成功了。

6.bind元素
bind元素的作用是通过OGNL表达式去自定义一个上下文变量,这样更方便我们使用。

比如我们要按角色名称进行模糊查询,我们可以把映射文件写成这样:

<select id="findRoles" parameterType="string" resultMap="RoleBean">
 <bind name="pattern" value="'%' + _parameter + '%'" />
 SELECT id, role_name as roleName ,create_date as createDate,end_date as endFlag,
 end_flag as endFlag ,note FROM t_role where role_name like #{pattern}
</select>

这样我们就可以不需要局限于数据库的不同而改变我们的sql语句了。

猜你喜欢

转载自blog.csdn.net/c1776167012/article/details/108081358