Mybatis在注解上如何實現(xiàn)動態(tài)SQL
更新時間:2022年06月13日 11:57:20 作者:k55
這篇文章主要介紹了Mybatis在注解上如何實現(xiàn)動態(tài)SQL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
在注解上實現(xiàn)動態(tài)SQL
使用Mybatis注解實現(xiàn)sql語句,但是有些時候有些字段是空的,這時候這個空的字段就要從條件查詢語句中刪除,這個時候就需要用到動態(tài)Sql。
注解的動態(tài)語句支持以下
trim
where
set
foreach
if
choose
when
otherwise
bind
@Select({"<script> " + "select * from t_user " + "where 1=1 " + "<if test='userId!=null'> and id = #{userId}</if> " + "</script>"})
要加上標簽就可以實現(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)文章
詳解在spring boot中配置多個DispatcherServlet
本篇文章主要介紹了詳解在spring boot中配置多個DispatcherServlet,具有一定的參考價值,有興趣的可以了解一下。2017-03-03SSH框架網(wǎng)上商城項目第18戰(zhàn)之過濾器實現(xiàn)購物登錄功能的判斷
這篇文章主要為大家詳細介紹了SSH框架網(wǎng)上商城項目第18戰(zhàn):過濾器實現(xiàn)購物登錄功能的判斷,感興趣的小伙伴們可以參考一下2016-06-06Java struts2 validate用戶登錄校驗功能實現(xiàn)
這篇文章主要為大家詳細介紹了Java struts2 validate用戶登錄校驗功能實現(xiàn)的具體步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05