Mybatis在注解上如何實(shí)現(xiàn)動(dòng)態(tài)SQL
在注解上實(shí)現(xiàn)動(dòng)態(tài)SQL
使用Mybatis注解實(shí)現(xiàn)sql語(yǔ)句,但是有些時(shí)候有些字段是空的,這時(shí)候這個(gè)空的字段就要從條件查詢語(yǔ)句中刪除,這個(gè)時(shí)候就需要用到動(dòng)態(tài)Sql。
注解的動(dòng)態(tài)語(yǔ)句支持以下
trimwheresetforeachifchoosewhenotherwisebind
@Select({"<script> " +
"select * from t_user " +
"where 1=1 " +
"<if test='userId!=null'> and id = #{userId}</if> " +
"</script>"})要加上標(biāo)簽就可以實(shí)現(xiàn)條件判斷

但是在無(wú)法使用大于號(hào) 、小于號(hào),那如何解決這問(wèn)題呢,其實(shí)只要把大于號(hào)、小于號(hào)轉(zhuǎn)義即可


注解方式動(dòng)態(tài)sql寫法和注意事項(xiàng)
@Select({"<script>" +
"select * from tb_examine" +
"<where> 1 = 1" +
"<if test = \" employeeId != null and employeeId != '' \"> AND employee_id = #{employeeId} </if>" +
"<if test = \" gradeId != null and gradeId != '' \"> AND grade_id = #{gradeId} </if>" +
"<if test = \" year != null and year != '' \"> AND year like #{year} </if>" +
"<if test = \" (statrMonth != null and statrMonth != '') and (endMonth == null or endMonth == '') \"> AND month >= #{statrMonth} </if>" +
"<if test = \" (statrMonth == null or statrMonth == '') and (endMonth != null and endMonth != '') \"> AND month <= #{endMonth} </if>" +
"<if test = \" (statrMonth != null and statrMonth != '') and (endMonth != null and endMonth != '') \">AND month >= #{statrMonth} AND month <= #{endMonth} </if>" +
"</where>" +
"</script>"})
public List<Examine> getName(Examine examine);判斷字符串為空串 用單引號(hào)

大于等于用

小于等于用

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中關(guān)于MouseWheelListener的鼠標(biāo)滾輪事件詳解
這篇文章主要介紹了Java中關(guān)于MouseWheelListener的鼠標(biāo)滾輪事件詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Hadoop源碼分析四遠(yuǎn)程debug調(diào)試
本篇是Hadoop源碼分析系列文章第四篇,主要介紹一下Hadoop的遠(yuǎn)程debug調(diào)試步驟,后續(xù)本系列文章會(huì)持續(xù)更新,有需要的朋友可以借鑒參考下2021-09-09
Springboot接口項(xiàng)目如何使用AOP記錄日志
這篇文章主要介紹了Springboot接口項(xiàng)目如何使用AOP記錄日志,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
關(guān)于JDK15的新特性之TextBlocks文本塊的引入和使用
這篇文章主要介紹了關(guān)于JDK15的新特性之文本塊的引入和使用,如果具有一種語(yǔ)言學(xué)機(jī)制,可以比多行文字更直觀地表示字符串,而且可以跨越多行,而且不會(huì)出現(xiàn)轉(zhuǎn)義的視覺(jué)混亂,那么這將提高廣泛Java類程序的可讀性和可寫性,需要的朋友可以參考下2023-07-07
SpringBoot框架aop切面的execution表達(dá)式解讀
這篇文章主要介紹了SpringBoot框架aop切面的execution表達(dá)式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
記一次springboot服務(wù)凌晨無(wú)故宕機(jī)問(wèn)題的解決
這篇文章主要介紹了記一次springboot服務(wù)凌晨無(wú)故宕機(jī)問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Springboot整合RabbitMq測(cè)試TTL的方法詳解
這篇文章主要介紹了Springboot整合RabbitMq測(cè)試TTL的設(shè)置,設(shè)置TTL一般由兩種設(shè)置方法,設(shè)置整個(gè)隊(duì)列的過(guò)期時(shí)間另一種設(shè)置單個(gè)消息的過(guò)期時(shí)間,通過(guò)示例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03

