Mybatis的mapper文件中trim標簽詳解


轉自》:https://blog.csdn.net/QQ727338622/article/details/84308020

0、背景

parameterType參數類型student是別名,里面的字段有id,name,age,sex被封裝成bean對象,跟數據庫中student表中字段一一對應,以下案例只為一個SQL語句。(初入SSM坑,請多多指教)

update student set name='aa',age=20,sex='男' where id=1; 
  • 1

1、prefix屬性:在trim開始部分添加內容

例,在trim前面加上set

<update id="updateStudent2" parameterType="student"> update student <trim prefix="set"> <if test="name!=null and name!=''">name=#{name},</if> <if test="age!=null and age!=''">age=#{age},</if> <if test="sex!=null and age!=''">sex=#{sex}</if> </trim> <where>id=#{id}</where> </update> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2、suffix屬性:在trim結束部分添加內容

例,在后面添加上where內容

<update id="updateStudent2" parameterType="student"> update student set <trim suffix="where id=#{id}"> <if test="name!=null and name!=''">name=#{name},</if> <if test="age!=null and age!=''">age=#{age},</if> <if test="sex!=null and age!=''">sex=#{sex}</if> </trim> </update> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.prefixOverrides屬性:去除trim開始部分的內容

例,刪掉name前面的set

<update id="updateStudent2" parameterType="student"> update student set <trim prefixOverrides="set" > <if test="name!=null and name!=''">set name=#{name},</if> <if test="age!=null and age!=''">age=#{age},</if> <if test="sex!=null and age!=''">sex=#{sex}</if> </trim> <where>id=#{id}</where> </update> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4、suffixOverrides屬性:去除trim結束部分的內容

例,刪掉最后一個逗號

<update id="updateStudent2" parameterType="student"> update student set <trim suffixOverrides=","> <if test="name!=null and name!=''">name=#{name},</if> <if test="age!=null and age!=''">age=#{age},</if> <if test="sex!=null and age!=''">sex=#{sex},</if> </trim> <where>id=#{id}</where> </update> 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM