mybatis中的动态sql

来源:互联网 发布:方便面 数据报告 编辑:程序博客网 时间:2024/05/01 04:00
mybatis中的动态sql: 


1、

<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>


prefix:在trim标签内sql语句加上前缀。
suffix:在trim标签内sql语句加上后缀。
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
prefixOverrides:指定去除多余的前缀内容



举例 :


下面是一个往购物车表中插入数据的mybatis语句


<insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart">  
        insert into cart  
        <trim prefix="(" suffix=")" suffixOverrides=",">   
            <if test="id != null">  
                id,  
            </if>  
            <if test="userId != null">  
                user_id,  
            </if>  
            <if test="dealId != null">  
                deal_id,  
            </if>  
            <if test="dealSkuId != null">  
                deal_sku_id,  
            </if>  
            <if test="count != null">  
                count,  
            </if>  
            <if test="createTime != null">  
                create_time,  
            </if>  
            <if test="updateTime != null">  
                update_time,  
            </if>  
        </trim>  
        <trim prefix="values (" suffix=")" suffixOverrides=",">  
            <if test="id != null">  
                #{id,jdbcType=BIGINT},  
            </if>  
            <if test="userId != null">  
                #{userId,jdbcType=BIGINT},  
            </if>  
            <if test="dealId != null">  
                #{dealId,jdbcType=BIGINT},  
            </if>  
            <if test="dealSkuId != null">  
                #{dealSkuId,jdbcType=BIGINT},  
            </if>  
            <if test="count != null">  
                #{count,jdbcType=INTEGER},  
            </if>  
            <if test="createTime != null">  
                #{createTime,jdbcType=TIMESTAMP},  
            </if>  
            <if test="updateTime != null">  
                #{updateTime,jdbcType=TIMESTAMP},  
            </if>  
        </trim>  
</insert>


suffixOverrides = "," 代指去除了最后一个逗号 


<if test="updateTime != null">  
#{updateTime,jdbcType=TIMESTAMP},  
</if>   
  
假设没有指定
suffixOverrides=","  


执行的sql语句也许是这样的:insert into cart (id,user_id,deal_id,) values(1,2,1,);显然是错误的
指定之后语句就会变成insert into cart (id,user_id,deal_id) values(1,2,1);这样就将“,”去掉了。
前缀也是一个道理这里就不说了。


注意: 如果需要前缀或者后缀去除多个,需要使用管道 | 


suffixOverrides=",|;"  








2、if


<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</select>


在使用if的时候,需要注意


SELECT * FROM BLOG
WHERE


或者


SELECT * FROM BLOG
WHERE
AND title like ‘someTitle’


这种情况,所以一般都会加上 
1=1这种恒成立的方法




3、choose (when, otherwise)
多种条件语句


<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>


也可以根据查询语句中传入进来的参数进行判断,组成不同的sql语句


 <select id="findMyTopicList" resultMap="BaseResultMap" parameterType="com.pa.market.common.vo.TopicListVO">
        select
        <include refid="Base_Column_List"/>
        from gsaledata.T_FORUM_TOPIC
        where CREATION_BY =#{creationBy,jdbcType=VARCHAR}
        <choose>
            <when test="sortByTime == 'asc'">
                order by CREATION_DATE asc
            </when>
            <otherwise>
                order by CREATION_DATE desc
            </otherwise>
        </choose>
    </select>
sortByTime 这个变量是通过接口传入进来的参数。


接口如下:其中sortByTime是TopicListVO中的属性值
List<ForumTopic> findMyTopicList(TopicListVO topicListVO); 




4、set


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


update tablename 
set tablecolum.age = 10 , tablecolum.name = "c"
where id = #{id}
需要指定where查询条件,不然会更新表中所有的数据。


<update id="updateAuthorIfNecessary">
  update Author
    <trim prefix="set" suffixOverrides=",">
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio},</if>
    </trim>
  where id=#{id}
</update>


5、foreach


一般都是使用在in的集合条件中。
<!-- 批量修改推送表报案状态 -->
<update id="modifyStatus" parameterType="java.util.List">
<foreach collection="list" index="index" item="item" open="begin" close=";end;" separator=";">
update gsaledata.t_push_record_customer_info p 
<set>
p.push_record_status= 'SUCCESS',
<if test="item.lastUpdateDate != null" >
      p.LAST_UPDATE_DATE = #{item.lastUpdateDate},
     </if>
    <if test="item.lastUpdatedBy != null" >
       p.LAST_UPDATED_BY = #{item.lastUpdatedBy}
       </if>
</set>
where p.business_id = #{item.businessId} and p.user_id = #{l.userId} 
  </foreach>
</update>

批量更新,直接在sql中一次性全部更新,通过begin和end分开sql执行的块。


还有一种方法是,在代码中for循环多次update语句。


<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>


原创粉丝点击