mybatis动态sql:2020-09-17

  • 本文只介绍动态mybatis动态sql的使用


  • if元素------------------------------------

<! --  id:跟持久层对应的方法名称      parameterType:持久层传来数据的类型       resultType:返回的数据类型-->
<select id="getByName" parameterType="com.qinglin.entity.Yang" resultType="com.qinglin.entity.Yang">
        select id,name,age from yang as y
        <where>
            <! --判断在业务层传来的数据是否为空-->
            <if test="name!=null">
            <! --  #{这里面写的是从业务层传来的属性的名称}-->
                y.name=#{name}
            </if>
            <if test="id!=null">
                and y.id=#{id}
            </if>
        </where>
    </select>


  • where元素(自动拼接关键字)------------------------------------


<! --  id:跟持久层对应的方法名称      parameterType:持久层传来数据的类型       resultType:返回的数据类型-->
<select id="getByName" parameterType="com.qinglin.entity.Yang" resultType="com.qinglin.entity.Yang">
        select id,name,age from yang as y
        <where>
            <! --判断在业务层传来的数据是否为空-->
            <if test="name!=null">
            <! --  #{这里面写的是从业务层传来的属性的名称}-->
                y.name=#{name}
            </if>
            <if test="id!=null">
                and y.id=#{id}
            </if>
        </where>
    </select>



  • choose元素------------------------------------

  • 注意:when判断不能用中文判断,只能用英文跟数字


<! --  id:跟持久层对应的方法名称      parameterType:持久层传来数据的类型       resultType:返回的数据类型-->
<select id="getByAge" parameterType="com.qinglin.entity.Yang" resultMap="yangPojo">
        select * from yang as y
        <where>
<! --  在choose标签中的结果只能有一条  -->
            <choose>
<! -- 如果第一个条件成立,就结束choose判断 -->
                <when test="name!=null">
                    y.name=#{name}
                </when>
                <when test="age!=null">
                    and y.age=#{age}
                </when>
                <otherwise>
                    y.age=20
                </otherwise>
            </choose>
        </where>
    </select>



  • set元素------------------------------------


<!--  id:跟持久层对应的方法名称      parameterType:持久层传来数据的类型   -->
 <update id="updateUser_if_set" parameterType="com.edu.domain.User">
        UPDATE user
<! -- 使用set标签表明接下来的sql拼接得事修改的sql -->
        <set>
            <if test="username!= null and username != '' ">
                username = #{username},
            </if>
            <if test="sex!= null and sex!= '' ">
                sex = #{sex},
            </if>
            <if test="birthday != null ">
                birthday = #{birthday},
            </if>
        </set>
        WHERE id = #{id};
    </update>


  • trim元素------------------------------------

  • ​ trim 是更灵活用来去处多余关键字的标签,它可以用来实现 where 和 set 的效果。
  • trim代替where


 <!-- 使用 if/trim 代替 where(判断参数) - 将 User 类不为空的属性作为 where 条件 -->
    <select id="getUsertList_if_trim" resultMap="userResultMap">
        SELECT *
        FROM user u
        <! --   prefix:代替where , prefixOverrides:覆盖前缀的名称, | 是或者的意思 -->
        <trim prefix="WHERE" prefixOverrides="AND|OR">
            <if test="username !=null ">
                u.username=#{username}
            </if>
            <if test="sex != null and sex != '' ">
                AND u.sex = #{sex}
            </if>
            <if test="birthday != null ">
                AND u.birthday = #{birthday}
            </if>
        </trim>
    </select>

  • ​ trim 代替 set


<!-- if/trim代替set(判断参数) - 将 User 类不为空的属性更新 -->  
<update id="updateUser_if_trim" parameterType="com.qf.pojo.User">  
    UPDATE user  
<! --   prefix:代替set,prefixOverrides:覆盖后缀的名称,如果sql语句最后面多了个逗号,就将此逗号删除 -->
    <trim prefix="SET" suffixOverrides=",">  
        <if test="username != null and username != '' ">  
            username = #{username},  
        </if>  
        <if test="sex != null and sex != '' ">  
            sex = #{sex},  
        </if>  
        <if test="birthday != null ">  
            birthday = #{birthday},  
        </if>  
    </trim>  
    WHERE user_id = #{user_id}  
</update>


  • foreach元素------------------------------------

  • 单参数List的类型


 <select id="dynamicForeachTest" resultType="user">
        select * from user where id in
<! -- collection:需要迭代的数据的类型,index:迭代每个数据的下标,item:迭代每个元素的元素本身,
      open:循环开始的位置,separator:每个元素之间的分隔符,close:循环结束的位置-->
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

  • 单参数array数组的类型


<select id="dynamicForeach2Test" resultType="user">
     select * from user where id in
<! -- collection:array表示的是数组 -->
     <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
          #{item}
     </foreach>
 </select>

  • 自己把参数封装成Map的类型


<select id="dynamicForeach3Test" resultType="user">
        select * from user where username like "%"#{username}"%" and id in
<! -- 如果有多中不同的类型,使用以下这种形式,ids这个名称是我从业务层传来的对象的类型,
      比如我传过来的是有个map集合,ids写的就是map集合的key -->
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。