zoukankan      html  css  js  c++  java
  • MyBatis---动态 SQL

    1.if

    2.choose, when, otherwise

    3. where

    <select id="findActiveBlogLike"
         resultType="Blog">
      SELECT * FROM BLOG 
      <where> 
        <if test="state != null">
             state = #{state}
        </if> 
        <if test="title != null">
            AND title like #{title}
        </if>
       </where>
    </select>

    4.set

    <update id="updateAuthor">
      update Author
        <set>
          <if test="username != null">username=#{username},</if>
          <if test="password != null">password=#{password},</if>
        </set>
      where id=#{id}
    </update>

    5.trim

    select * from user 
      <trim prefix="WHERE" prefixoverride="AND |OR"><!--prefixoverride:去掉第一个and或者是or-->
        <if test="name != null and name.length()>0"> AND name=#{name}</if>
        <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
      </trim>

    假如说name和gender的值都不为null的话打印的SQL为:select * from user where  name = 'xx' and gender = 'xx'

    update user
      <trim prefix="set" suffixoverride="," suffix=" where id = #{id} "><!--suffixoverride:去掉最后一个逗号;suffix:后缀-->
        <if test="name != null and name.length()>0"> name=#{name} , </if>
        <if test="gender != null and gender.length()>0"> gender=#{gender} ,  </if>
      </trim>

    假如说name和gender的值都不为null的话打印的SQL为:update user set name='xx' , gender='xx'   where id='x'

    6.foreach

    <select id="selectPostIn" resultType="domain.blog.Post">
      SELECT *
      FROM POST P
      WHERE ID in
      <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
      </foreach>
    </select>
  • 相关阅读:
    Thinkpad R400无线网络一个都不见了!
    如果使用安卓4.4的SD卡?
    如何使用安卓4.4的SD卡?
    在IAR使用FreeRTOS出现Error[Pa045]: function "XXX" has no prototype
    DSP5509的RTC实验-第3篇
    LWM2M简介-学习记录
    DSP5509的定时器实验-第2篇
    DSP5509的XF实验-第一篇
    华为LiteOS系统使用-任务调度函数-第一篇
    2017-12-24自选的股票之春秋航空
  • 原文地址:https://www.cnblogs.com/mcahkf/p/8602726.html
Copyright © 2011-2022 走看看