Mapper.xml中的trim

H2oooooooooooooooooo發表於2018-05-10
 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‘  

相關文章