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

来源:互联网 发布:天津广电网络客服电话 编辑:程序博客网 时间:2024/06/06 04:01

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的意思。


4 1
原创粉丝点击