MyBatis学习总结(六)——动态SQL

MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。

MyBatis中用于实现动态SQL的元素主要有:

 

  • if
  • choose(when,otherwise)
  • foreach
  • where
  • set
  • trim
下面我们主要说 where set trim 这三个标签


1,where标签


<!-- 查询学生list,like姓名,=性别 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     
    SELECT * from STUDENT_TBL ST      
        WHERE      
        <if test="studentName!=null and studentName!='' ">     
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
        </if>     
        <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.STUDENT_SEX = #{studentSex}      
        </if>     
</select> 

 如果上面例子,参数studentName为null或’’,则或导致此sql组合成“WHERE AND”之类的关键字多余的错误SQL。
 这时我们可以使用where动态语句来解决。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。


使用where标签

<!-- 查询学生list,like姓名,=性别 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     
    SELECT * from STUDENT_TBL ST      
    <where>     
        <if test="studentName!=null and studentName!='' ">     
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
        </if>     
        <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.STUDENT_SEX = #{studentSex}      
        </if>     
    </where>     
</select>   

2,set标签


当在update语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。
没有使用if标签时,如果有一个参数为null,都会导致错误,如下示例:


<!-- 更新学生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     
    UPDATE STUDENT_TBL      
       SET STUDENT_TBL.STUDENT_NAME = #{studentName},      
           STUDENT_TBL.STUDENT_SEX = #{studentSex},      
           STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
           STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>  

使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值。如下示例:


<!-- 更新学生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     
    UPDATE STUDENT_TBL      
    <set>     
        <if test="studentName!=null and studentName!='' ">     
            STUDENT_TBL.STUDENT_NAME = #{studentName},      
        </if>     
        <if test="studentSex!=null and studentSex!='' ">     
            STUDENT_TBL.STUDENT_SEX = #{studentSex},      
        </if>     
        <if test="studentBirthday!=null ">     
            STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
        </if>     
        <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
            STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
        </if>     
    </set>     
    WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>     

3,trim标签


trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。

where例子的等效trim语句:


<!-- 查询学生list,like姓名,=性别 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     
    SELECT * from STUDENT_TBL ST      
    <trim prefix="WHERE" prefixOverrides="AND|OR">     
        <if test="studentName!=null and studentName!='' ">     
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
        </if>     
        <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.STUDENT_SEX = #{studentSex}      
        </if>     
    </trim>     
</select>   

set例子的等效trim语句:

<!-- 更新学生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     
    UPDATE STUDENT_TBL      
    <trim prefix="SET" suffixOverrides=",">     
        <if test="studentName!=null and studentName!='' ">     
            STUDENT_TBL.STUDENT_NAME = #{studentName},      
        </if>     
        <if test="studentSex!=null and studentSex!='' ">     
            STUDENT_TBL.STUDENT_SEX = #{studentSex},      
        </if>     
        <if test="studentBirthday!=null ">     
            STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
        </if>     
        <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
            STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
        </if>     
    </trim>     
    WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>     

总结:


其实在真正应用时,我们更多的是使用查询,上面介绍了动态SQL,下面我们也来对比一下


-- 方式一
<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean类名">    
select * from table_name where 1=1      
<if test="resourceId != null">        
and resource_id = #{resourceId,jdbcType=INTEGER}      
</if>      
<if test="appid != null">       
and  appid = #{appid,jdbcType=TINYINT}      
</if>      
<if test="resourceUrl != null">       
and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      
</if>      
<if test="resourceDesc != null">       
and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      
</if>  
</select>
这种方式的要点在where后面的1=1,加上这个能够避免第一个if条件后面是否需要加and的选择困境。


-- 方式二
<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean类名">    
select * from 表名    
<where>      
<if test="resourceId != null">        
and resource_id = #{resourceId,jdbcType=INTEGER}      
</if>      
<if test="appid != null">       
and  appid = #{appid,jdbcType=TINYINT}      
</if>      
<if test="resourceUrl != null">       
and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      
</if>      
<if test="resourceDesc != null">       
and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      
</if>    
</where>  
</select>

这种方式比第一种多了一个where标签,而且不需要在where后面显示的加一个1=1的字段。


-- 方式三
<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean类名">    
select * from 表名    
<trim prefix = "where" prefixOverrides="and|or">      
<if test="resourceId != null">        
and resource_id = #{resourceId,jdbcType=INTEGER}      
</if>      
<if test="appid != null">       
and  appid = #{appid,jdbcType=TINYINT}      
</if>      
<if test="resourceUrl != null">       
and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      
</if>      
<if test="resourceDesc != null">       
and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      
</if>    
</trim>  
</select>
第三种方式是最值得提倡的方式,其中的trim标签中标记该标签是以where为前缀的,即where条件的字句。后面的prefixOverrides="and|or"是说如果where标签中包含的内容开头是and或者or,那么久忽略and或者or。还有另外一个:suffixOverrides="and|or",表示where字句中是and或者or结尾则忽略and或者or的意思。


  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值