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

时间:2015-12-17 12:57:30   收藏:0   阅读:8040

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‘  

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!