mybatis--动态sql中的where语句

初次接触mybatis的时候,是不是遇到过这样的情况呢?

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  WHERE 
  <if test="state != null">
    state = #{state}
  </if> 
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</select>

如果这些条件没有一个能匹配上将会怎样?最终这条 SQL 会变成这样:

SELECT * FROM BLOG
WHERE
这会导致查询失败。
如果仅仅第二个条件匹配又会怎样?这条 SQL 最终会是这样:

SELECT * FROM BLOG
WHERE 
AND title like ‘someTitle’


也许你会聪明的这么写:

<pre name="code" class="html"><select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  WHERE <span style="color:#ff0000;">1=1</span>
  <if test="state != null">
    state = #{state}
  </if> 
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</select>

 

这样写其实并不优美,如果你查看mybatis的官方文档,那么你就会发现mybatis提供了动态sql的支持,解决上面的情况有两种方法:

第一种是通过

<where>标签

MyBatis 有一个简单的处理,这在90%的情况下都会有用。而在不能使用的地方,你可以自定义处理方式来令其正常工作。一处简单的修改就能得到想要的效果:

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  <where> 
    <if test="state != null">
         state = #{state}
    </if> 
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>
</select>

where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。

如果 where 元素没有按正常套路出牌,我们还是可以通过自定义 trim 元素来定制我们想要的功能。比如,和 where 元素等价的自定义 trim 元素为:

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ... 
</trim>
  1. <select id="getStudentList_if_trim" resultMap="resultMap_studentEntity">  
  2.     SELECT ST.STUDENT_ID,  
  3.            ST.STUDENT_NAME,  
  4.            ST.STUDENT_SEX,  
  5.            ST.STUDENT_BIRTHDAY,  
  6.            ST.STUDENT_PHOTO,  
  7.            ST.CLASS_ID,  
  8.            ST.PLACE_ID  
  9.       FROM STUDENT_TBL ST   
  10.     <trim prefix="WHERE" prefixOverrides="AND|OR">  
  11.         <if test="studentName !=null ">  
  12.             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
  13.         </if>  
  14.         <if test="studentSex != null and studentSex != '' ">  
  15.             AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
  16.         </if>  
  17.         <if test="studentBirthday != null ">  
  18.             AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
  19.         </if>  
  20.         <if test="classId != null and classId!= '' ">  
  21.             AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
  22.         </if>  
  23.         <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
  24.             AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
  25.         </if>  
  26.         <if test="placeId != null and placeId != '' ">  
  27.             AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
  28.         </if>  
  29.         <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">  
  30.             AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
  31.         </if>  
  32.         <if test="studentId != null and studentId != '' ">  
  33.             AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
  34.         </if>  
  35.     </trim>     
  36. </select>  








  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值