MyBatis动态SQL

<![CDATA[ ]]> 的作用

 <![CDATA[  ]]>是xml语法,在<![CDATA[ ... ]]>的所有内容都会被解析器忽略(特殊字符不转译)

<if test="">   <where>  <choose> <trim>  这些标签都不会被解析,所有要尽量缩小<![CDATA[ ]]>的使用范围
  • if

      <select id="dynamicFindBook1" resultMap="BookResult" parameterType="Map">
          select * from book where 1=1 
          <if test="bookName!=null">
          <![CDATA[and bname like #{bookName}]]>
          </if>
          <if test="maxPrice!=null">
          <![CDATA[ and price <= #{maxPrice} ]]>
          </if>
          <if test="studentId!=null">
          and student_id =#{studentId}
          </if>
      </select>
  • choose/when/otherwise

      <select id="dynamicFindBook2" resultMap="BookResult" parameterType="Map">
        select * from book
        <choose>
                   <!--  注意要加上 ' ' 单引号-->
        <when test="searchBy=='bookName'">
        where bname like#{bookName}
        </when>
        <when test="searchBy=='studentId'">
        where student_id=#{studentId} 
        </when>
        <otherwise>
        <![CDATA[where price<=#{price} ]]>
        </otherwise>
        </choose>
      </select>
  • where

    如果所有if不成立,则不会加where,如果第一条if不成立,那么后面成立的第一个if会剔除and

      <select id="dynamicFindBook3" resultMap="BookResult" parameterType="Map">
        select * from book
        <where>
        <if test="bookName!=null">
          bname like #{bookName}
          </if>
          <if test="maxPrice!=null">
          <![CDATA[ and price <= #{maxPrice} ]]>
          </if>
          <if test="studentId!=null">
          and student_id =#{studentId}
          </if>
        </where>
      </select>
  • trim

    trim和where类似,更灵活,当基句中已存在where时,可以将改为 prefix=”and”

      <select id="dynamicFindBook4" resultMap="BookResult" parameterType="Map">
        select * from book 
        <trim prefix="where" prefixOverrides="and|or"> 
        <if test="bookName!=null">
          bname like #{bookName}
          </if>
          <if test="maxPrice!=null">
          <![CDATA[ and price <= #{maxPrice} ]]>
          </if>
          <if test="studentId!=null">
          and student_id =#{studentId}
          </if>
        </trim>
      </select>
  • foreach

      <select id="dynamicFindBook5" resultMap="BookResult" parameterType="Map">
        select * from book 
        <if test="studentIds!=null">
        <where>
        <foreach item="studentId" collection="studentIds">
        or student_id=#{studentId} 
        </foreach>
        </where>
        </if>
      </select>

    上面的例子也可使用in

    但是当集合中没有数据时,将会出错,而用or不会

      <select id="dynamicFindBook6" resultMap="BookResult" parameterType="Map">
        select * from book 
        <if test="studentIds!=null">
        <where>
        student_id in 
        <foreach item="studentId" collection="studentIds" open="(" separator="," close=")">
        #{studentId} 
        </foreach>
        </where>
        </if>
      </select>
  • set 相对于update修改的

    注意在if的内容中 后面有一个 , 逗号

      <update id="dynamicFindBook7" parameterType="com.z.entity.Book">
        update book 
        <set>
        <if test="name!=null"> bname=#{name}, </if>
          <if test="price!=null"> price=#{price}, </if>
        </set>
        where bid=#{id}
      </update>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值