如何正确的使用Mybatis模糊查询

发布时间:2021-01-18 15:47:08 作者:Leah
来源:亿速云 阅读:246

这期内容当中小编将会给大家带来有关如何正确的使用Mybatis模糊查询,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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>

#{} 是采用预编译的写法,也就是JDBC中的PerpareStatement,这种写法可以防止sql注入,但${}这种写法是不采用预编译,其中的参数写成类中的属性或者map的key值或者为接口中注解的参数名。

mybatis 提供了bind 标签。下面举个例子

 <select id="select01" resultMap="BasicResultMap">
  <bind name="emp_name" value="'%'+ _parameter.getEmp_name() +'%'"/>
   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标签的属性及其含义

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 他们的含义是在遍历开始和结束时分别添加其内容。

上述就是小编为大家分享的如何正确的使用Mybatis模糊查询了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. mybatis like 模糊查询
  2. MyBatis模糊查询

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis

上一篇:怎么在C++中引用作为函数

下一篇:如何在JavaScript中使用this方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》