MyBatis的动态SQL查询常用几种标签

一:if标签

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


     <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.STUDENT_SEX = #{studentSex}      
      </if>

</select>  


二:where、set、trim标签

2.1 if + where 的条件判断

    <!--  select - where/if(判断参数) - 将实体类不为空的属性作为where条件 -->  
    <select id="getStudentList_whereIf" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">  
        SELECT ST.STUDENT_ID,  
               ST.STUDENT_NAME,  
               ST.STUDENT_SEX,  
               ST.STUDENT_BIRTHDAY,  
               ST.STUDENT_PHOTO,  
               ST.CLASS_ID,  
               ST.PLACE_ID  
          FROM STUDENT_TBL ST   
        <where>  
            <if test="studentName !=null ">  
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
            </if>  
            <if test="studentSex != null and studentSex != '' ">  
                AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
            </if>  
            <if test="studentBirthday != null ">  
                AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
            </if>  
            <if test="classId != null and classId!= '' ">  
                AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
            </if>  
            <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
                AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
            </if>  
        </where>    
    </select> 


2.2: if + set 的更新语句

    <!-- if/set(判断参数) - 将实体类不为空的属性更新 -->  
    <update id="updateStudent_if_set" parameterType="liming.student.manager.data.model.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="studentPhoto != null ">  
                STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},  
            </if>  
            <if test="classId != '' ">  
                STUDENT_TBL.CLASS_ID = #{classId}  
            </if>  
        </set>  
        WHERE STUDENT_TBL.STUDENT_ID = #{studentId};      
    </update> 


2.3if + trim代替where/set标签

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


2.3.1  trim代替where

!--  if/trim代替where(判断参数) - 将实体类不为空的属性作为where条件 -->  
<select id="getStudentList_if_trim" resultMap="resultMap_studentEntity">  
    SELECT ST.STUDENT_ID,  
           ST.STUDENT_NAME,  
           ST.STUDENT_SEX,  
           ST.STUDENT_BIRTHDAY,  
           ST.STUDENT_PHOTO,  
           ST.CLASS_ID,  
           ST.PLACE_ID  
      FROM STUDENT_TBL ST   WHERE 1=1
    <trim prefix="WHERE" prefixOverrides="AND|OR">  
        <if test="studentName !=null ">  
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
        </if>  
        <if test="studentSex != null and studentSex != '' ">  
            AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
        </if>  
        <if test="studentBirthday != null ">  
            AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
        </if>  
        <if test="classId != null and classId!= '' ">  
            AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
        </if>  
        <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
            AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
        </if>   
    </trim>     
</select> 


2.3.2  trim代替set

    <!-- if/trim代替set(判断参数) - 将实体类不为空的属性更新 -->  
    <update id="updateStudent_if_trim" parameterType="liming.student.manager.data.model.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="studentPhoto != null ">  
                STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},  
            </if>  
            <if test="classId != '' ">  
                STUDENT_TBL.CLASS_ID = #{classId},  
            </if>  
            <if test="placeId != '' ">  
                STUDENT_TBL.PLACE_ID = #{placeId}  
            </if>  
        </trim>  
        WHERE STUDENT_TBL.STUDENT_ID = #{studentId}  
    </update> 


三:choose (when, otherwise)

   choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default

    <!--  choose(判断参数) - 按顺序将实体类第一个不为空的属性作为where条件 -->  
    <select id="getStudentList_choose" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">  
        SELECT ST.STUDENT_ID,  
               ST.STUDENT_NAME,  
               ST.STUDENT_SEX,  
               ST.STUDENT_BIRTHDAY,  
               ST.STUDENT_PHOTO,  
               ST.CLASS_ID,  
               ST.PLACE_ID  
          FROM STUDENT_TBL ST   WHERE 1=1
        <where>  
            <choose>  
                <when test="studentName !=null ">  
                    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
                </when >  
                <when test="studentSex != null and studentSex != '' ">  
                    AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
                </when >  
                <when test="studentBirthday != null ">  
                    AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
                </when >  
                <when test="classId != null and classId!= '' ">  
                    AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
                </when >  
                <when test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
                    AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
                </when >  
                <otherwise>  
                </otherwise>  
            </choose>  
        </where>    
    </select> 


四:foreach

对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。List 实例将使用“list”做为键,数组实例以“array” 做为键。

foreach元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。

注意:你可以传递一个List实例或者数组作为参数对象传给MyBatis。当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称在作为键。List实例将会以“list”作为键,而数组实例将会以“array”作为键。






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis动态SQL的常用标签有以下几种: 1. if标签:if标签用于判断表达式是否成立。当test条件成立时,MyBatis会将if标签之间的代码拼接到SQL语句上面。 2. choose、when、otherwise标签:choose标签类似于Java的switch语句,根据条件选择不同的分支。when标签用于定义条件分支,而otherwise标签则是当所有条件都不满足时的默认分支。 3. trim标签:trim标签用于修剪SQL语句多余的字符,如逗号或AND等。它可以在SQL语句的开头或结尾添加特定的字符,并根据条件去除多余的字符。 4. where标签:where标签用于拼接SQL语句的WHERE条件,它会根据条件自动添加WHERE关键字,并处理多个条件之间的逻辑关系。 5. set标签:set标签用于拼接SQL语句的SET部分,它会根据条件自动添加SET关键字,并处理多个更新字段之间的逗号分隔。 6. foreach标签:foreach标签用于循环遍历集合或数组,并将集合的元素拼接到SQL语句。它可以用于IN查询或批量插入等操作。 这些动态SQL标签可以根据实际需求灵活组合和使用,以实现复杂SQL语句的拼接和动态变化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行原理?](https://blog.csdn.net/weixin_42427551/article/details/105835737)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【MyBatis笔记09】MyBatis常用的几个动态SQL标签](https://blog.csdn.net/qq_39826207/article/details/126650622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KunQian_smile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值