详解Mybatis模糊查询和动态sql语句的用法

Mybatis 模糊查询和动态sql语句

模糊查询

对数据库最常用的操作就是查询了,但是如何使用Mybatis进行模糊查询呢?下面先看一个简单的模糊查询

  <select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
  FROM
   oa_employee 
  WHERE emp_name LIKE #{asd} 
  </select>

这是一条伪模糊查询, 因为没有实现真正的模糊 “%”。参数为字符串,所以#{}中内容不被限制。但是应该如何插入 % 字符呢。 我们首先想到的是传递字符串参数时将%插入到字符串中 “张%”,但是这种方法操作略微繁琐了一些。 下面提供了使用sql方法的策略

 <select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
  FROM
   oa_employee 
  WHERE emp_name LIKE CONCAT( #{asd} ,'%')
  </select>

另外一种不推荐的写法给大家

<select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
  FROM
   oa_employee 
  WHERE emp_name LIKE '${emp_name}%'
  </select>

他是在#{}表达式自动填入value值,值得注意的是“_parameter.getEmp_name()” 调用的方法是对象中作为查询参数的属性的get方法

多条件查询

多种条件查询的要点是判断查询条件是否为空,拼接sql语句。在mybatis中提供了if标签和where 标签。 下面来介绍两种标签的用法。

if标签

<select id="select01" resultMap="BasicResultMap"> 
SELECT 
* 
FROM 
oa_employee 
WHERE 1=1 
<if test="emp_name != null and emp_name != ''"> 
and emp_name = #{emp_name } 
</if> 
<if test="emp_sex != null and emp_sex != ''"> 
and sex = #{emp_sex} 
</if> 
</select> 

mybatis 中的if标签有些类似于EL表达式的使用,test中可以直接写入类中的属性或者key值。

where标签

<select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
   FROM
   oa_employee 
   <where>
   <if test="emp_name != null and emp_name != ''">
    and emp_name = #{emp_name }
   </if>
   <if test="emp_sex != null and emp_sex != ''">
    and sex = #{emp_sex}
   </if>
   </where>
  </select>

这里的where标签 替换了前一段代码的 where 1=1 。 mybatis中的where 标签会判断标签内是否有内容, 如果有内容就自动生成where 并把 where 后面的第一个and +一个空格,or+一个空格 去掉。

choose , when 和 otherwise 标签

<select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
   FROM
   oa_employee 
   <where>
   <choose>
    <when test="emp_name != null and emp_name != ''">
       and emp_name = #{emp_name }
    </when>
     <when test="emp_sex != null and emp_sex != ''">
       and sex = #{emp_sex}
    </when>
    <otherwise>
      emp_id = 50
    </otherwise>
   </choose>
   </where>
  </select>

当所有条件不满足时,执行otherwise标签的内容。

trim标签

<select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
   FROM
   oa_employee 
    <trim prefix="where" prefixOverrides="and |or ">
    <if test="emp_name != null and emp_name != ''">
      and emp_name = #{emp_name }
    </if>
    <if test="emp_sex != null and emp_sex != ''">
       and sex = #{emp_sex}
    </if>
  </trim>

trim标签的属性及其含义

  • prefix : 标签之间有内容在最前面加入
  • prefixOverrides: 检查内容的最前面是否匹配,匹配就删除
  • suffix: 标签之间有内容在最后面加入
  • suffixOverrides:检查内容的最后面是否匹配,匹配就删除

set标签

set标签常用于update操作,并且会自动抹掉无关的

<update id="update01" >
  UPDATE 
   oa_employee 
  <set>
    <if test="emp_name != null and emp_name != ''">
       emp_name = #{emp_name}
    </if>
    <if test="emp_sex != null and emp_sex != ''">
      ,sex = #{emp_sex}
    </if>
  </set>
  WHERE emp_id = 50 
  </update>

foreach标签

foreach 用于处理数组或者list集合,下面是一个批量添加的例子

 <insert id="insert01">
  INSERT INTO 
  oa_employee 
  ( emp_name, sex, fk_dept_id) 
  VALUES
  <foreach collection="list" item="employee" separator=","> 
   (#{employee.emp_name},#{employee.emp_sex},#{employee.fk_dept_id})
  </foreach>
  </insert>

其中 如果参数为数组 则collection只能为“array” 参数为List集合则collection只能为 “list” item类似JSTL 中的var的作用, 指代容器中的每一个对象。separator=”,”的含义是每条数据以 , 分割。 未注明的属性有 open 和 close 他们的含义是在遍历开始和结束时分别添加其内容。


学习的路上难免迷茫,所以一套好的学习资料,就十分重要,
所以在这里给大家分享一套视频。
这套视频包含源码,可以帮助大家很好的打好基础,
关注小编,点击免费领取视频160集,备注暗号CSDN


版权声明:本文为weixin_51495453原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。