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>

#{} 是采用预编译的写法,也就是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标签的属性及其含义
- 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 他们的含义是在遍历开始和结束时分别添加其内容。

欢迎学习,如有错误请及时联系本人修改。
希望指出本人不足共同进步
LLY19960418

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值