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

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


注解方式動態(tài)sql寫法和注意事項
@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);判斷字符串為空串 用單引號

大于等于用

小于等于用

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

