Java实战_Mapper.xml中的trim

2022-07-16 08:48:30

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‘

  • 作者:健超还在敲代码
  • 原文链接:https://blog.csdn.net/qq_41916378/article/details/108265311
    更新时间:2022-07-16 08:48:30