mybatis动态SQL中的trim、foreach和bind

参考地址

https://blog.csdn.net/PostersXu/article/details/79058365

https://www.cnblogs.com/qiankun-site/p/5758924.html

一、trim标签

trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码:

  1、

  select * from user 

  <trim prefix="WHERE" prefixoverride="AND |OR">

    <if test="name != null and name.length()>0"> AND name=#{name}</if>

    <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>

  </trim>

  假如说name和gender的值都不为null的话打印的SQL为:select * from user where 空格  name = 'xx' and gender = 'xx'

  在红色标记的地方是不存在第一个and的,上面两个属性的意思如下:

  prefix:前缀      

  prefixoverride:去掉第一个and或者是or

  2、

  update user

  <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">

    <if test="name != null and name.length()>0"> name=#{name} , </if>

    <if test="gender != null and gender.length()>0"> gender=#{gender} ,  </if>

  </trim>

  假如说name和gender的值都不为null的话打印的SQL为:update user set name='xx' , gender='xx'   空格   where id='x'

  在红色标记的地方不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,其中prefix意义如上:

  suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)

  suffix:后缀

二、foreach标签

动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历,通常是在构建 IN 条件语句的时候。比如:

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>       

foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及在迭代结果之间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。    

注意 你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

1、foreach标签属性解读

MyBatis的foreach标签应用于多参数的交互如:多参数(相同参数)查询、循环插入数据等,foreach标签包含collection、item、open、close、index、separator,MyBatis的foreach标签与jstl标签的使用非常相似,以下为几个属性的意思解释:

collection:collection是传进来的参数名称,以是一个数组或者List、Set等集合。根据Mapper接口的参数名确定,也可以使用@Param注解指定参数名。

item:参数调用名称,通过此属性来获取集合单项的值

open:相当于prefix,即在循环前添加前缀

close:相当于suffix,即在循环后添加后缀

index:当前元素在集合中的索引、下标

separator:分隔符,每次循环完成后添加此分隔符

2、foreach标签应用

1).多参数查询

场景描述:传入多个用户ID,获取用户的结果集

Junit业务方法调用




mapper接口(指定参数名称)



XML实现:


SQL打印:


2).循环插入数据
Junit业务方法调用




SQL打印

[com.xuyong.dao.UserMapper.insertMultiUsers]-==>  Preparing: insert into user(user_name,gender,email,address,dept_id) values (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) 
  [com.xuyong.dao.UserMapper.insertMultiUsers]-==> Parameters: HanMeimei(String), 1(Integer), [email protected](String), 江苏省南京市(String), 2(Integer), 李雷(String), 0(Integer), [email protected](String), 北京市(String), 1(Integer), QQ小冰(String), 1(Integer), [email protected](String), 深圳市(String), 2(Integer)
  [com.xuyong.dao.UserMapper.insertMultiUsers]-<==    Updates: 3
  3


三、bind标签

1、bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如:

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>

2、  mybatis中使用mysql的模糊查询字符串拼接(like) 中也涉及到bind的使用

      <!-- List<Employee> getEmpsTestInnerParameter(Employee employee); -->
      <select id="getEmpsTestInnerParameter" resultType="com.hand.mybatis.bean.Employee">
          <!-- bind:可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 -->
          <bind name="bindeName" value="'%'+eName+'%'"/>
 eName是employee中一个属性值
          SELECT * FROM emp 
          <if test="_parameter!=null">
            where ename like #{bindeName}
          </if>
      </select>

   测试类中:

           Employee emp=new Employee();
            emp.setEname("张");   为eName属性赋值为“张”
            List<Employee> list=mapper.getEmpsTestInnerParameter(emp);
            for (Employee employee : list) {
                System.out.println(employee);
            }

3、实例

<select id="selectSysUsersAdvancedWithWhere" resultType="com.artisan.mybatis.xml.domain.SysUser">
        SELECT
            a.id,
            a.user_name userName,
            a.user_password userPassword,
            a.user_email userEmail,
            a.user_info userInfo,
            a.head_img headImg,
            a.create_time createTime
        FROM
            sys_user a
        <where>
            <if test="userName != null and userName != '' ">
                and user_name like concat('%',#{userName},'%')
            </if>
            <if test="userEmail != null and userEmail != '' ">
                and user_email = #{userEmail}
            </if>
        </where>
    </select>

使用concat函数连接字符串,在MySQL中,这个函数支持多个参数,但是在Oracle中只支持两个参数。 由于不同数据库之间的语法差异,如果更换了数据库,有些SQL语句可能就需要重写。 针对这种情况,可以使用bind标签来避免由于更换数据库带来的一些麻烦。 我们将上面的语句改为bind方式,如下

bind标签的两个属性都是不选项,name为绑定到上下文的变量名,value为OGNL表达式,创建一个bind标签后,就可以在下面直接使用了。 使用bind拼接字符串不仅可以避免因更换数据库而修改SQL,也能预防SQL注入。

示例

1.增加接口方法



2.配置动态SQL

猜你喜欢

转载自blog.csdn.net/beidaol/article/details/83085755