MyBatis XML去除多余AND|OR前綴或逗號(hào)等后綴的操作
1.通過trim格式化標(biāo)記set或where功能
2.對(duì)于set自動(dòng)刪除最后一個(gè)“,”,對(duì)于where自動(dòng)刪除最后一個(gè)“and|or”
使用示例如下:
1、
select * from user <trim prefix='WHERE' prefixOverride='AND |OR'><if test='userName != null and userName.length()>0'> AND user_name=#{userName}</if><if test='loginName != null and loginName.length()>0'> AND login_name=#{loginName}</if></trim>
如果userName 為空則最終SQL為:
select * from user where login_name = ’xx’
prefix:前綴
prefixOverride:去掉第一個(gè)and或者是or
2、
update user<trim prefix='set' suffixOverride=',' suffix=' where user_id = #{userId} '><if test='userName != null and userName.length()>0'> user_name=#{userName} , </if><if test='loginName != null and loginName.length()>0'> login_name=#{loginName} , </if></trim>
如果userName 為空則最終SQL為:
update user set login_name=’xx’ where user_id=’xx’
suffixOverride:去掉最后一個(gè)逗號(hào)(也可以是其他的標(biāo)記,就像是上面前綴中的and一樣)
suffix:后綴
補(bǔ)充:mybatis去除多余的and或者or
啥也不多說了,大家還是直接看代碼吧~
<select resultType='xxx.UserInfo'>select<include refid='Base_Column_List' />from uc_user<trim prefix='WHERE (' suffix=')' prefixOverrides='AND |OR '><if test='userName != null' >user_name = #{userName}</if><if test='email != null' >or email = #{email}</if><if test='phone != null' >or phone = #{phone}</if><if test='weiboId != null' >or weibo_id = #{weiboId}</if><if test='wxId != null' >or wx_id = #{wxId}</if> <if test='qqId != null' >or qq_id = #{qqId}</if></trim>and status = 1</select>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 什么是Access數(shù)據(jù)庫2. 從舊版本SQL Server中重新存儲(chǔ)數(shù)據(jù)3. mysql-bin.000001文件的來源及處理方法4. Mysql入門系列:MYSQL服務(wù)器內(nèi)部安全性-安全數(shù)據(jù)目錄訪問5. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫的備份與恢復(fù)7. MySQL 字符串函數(shù):字符串截取8. MYSQL(電話號(hào)碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)9. MySQL分區(qū)的優(yōu)點(diǎn)10. Mysql入門系列:MYSQL表達(dá)式求值和MYSQL類型轉(zhuǎn)換
1. Mysql入門系列:MYSQL表達(dá)式求值和MYSQL類型轉(zhuǎn)換
網(wǎng)公網(wǎng)安備