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

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‘  

文章来自:http://www.cnblogs.com/welcometowyy/p/5053495.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3