码迷,mamicode.com
首页 > 移动开发 > 详细

My Batis mapper.xml中 动态SQL中使用trim标签 if end的场景

时间:2015-12-17 12:57:30      阅读:8040      评论:0      收藏:0      [点我收藏+]

标签:

trim标签有点类似于replace效果。

 trim 属性

                prefix:前缀覆盖并增加其内容

                suffix:后缀覆盖并增加其内容

                prefixOverrides:前缀判断的条件

                suffixOverrides:后缀判断的条件

 

<!-- 修改 -->
 <update id="updateTest" >
        UPDATE test
        <trim prefix="SET" suffixOverrides=",">
         <if test="name!=null and name!=‘‘">
         name = #{name},
         </if>
         <if test="phone!=null and phone!=‘‘">
         phone = #{phone},
         </if>
         <if test="address!=null and address!=‘‘">
         address = #{address},
         </if>
       </trim> 
        WHERE
         id = #{id}
 </update>

输出sql

update test set name = #{name}, phone = #{phone}, address = #{address}   WHERE
         id = #{id}

 

 

<select id="checkUserByPhone" parameterType="User" resultMap="UserMap">
  select * from user
  <trim prefix="WHERE" prefixOverrides="AND | OR">
   <if test="userId!=null and userId!=‘‘">
         and user_id != #{userId}
         </if>
   <if test="phone!=null and phone!=‘‘ and state!=‘All‘">
         and phone = #{phone} and state!=‘X‘
         </if>
           
  </trim>
 </select>

 

输出sql   select * from user  WHERE user_id != #{userId}  and phone = #{phone} and state!=‘X‘  

My Batis mapper.xml中 动态SQL中使用trim标签 if end的场景

标签:

原文地址:http://www.cnblogs.com/welcometowyy/p/5053495.html

(3)
(10)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!