mybatis if標簽判斷不生效的解決方法
實際需求
<if test="computationRule == '1'"> FROM app_sz_bbb a </if> <if test="computationRule == '2'"> FROM app_ccc a </if>
這種情況不生效,
原因:mybatis是用OGNL表達式來解析的,在OGNL的表達式中,'0'會被解析成字符,java是強類型的,char 和 一個string 會導(dǎo)致不等,所以if標簽中的sql不會被解析。
先說怎么解決
三種:
加 .toString()
<if test="computationRule == '1'.toString()"> FROM app_sz_bbb a </if> <if test="computationRule == '2'.toString()"> FROM app_ccc a </if>
choose when 標簽代替
<choose> <when test="computationRule == '1'"> FROM app_sz_bbb a </when> <otherwise> FROM app_sz_bbb a </otherwise> </choose>
單引號 換成雙引號
<if test='computationRule == "1"'> FROM app_sz_bbb a </if> <if test='computationRule == "2"'> FROM app_ccc a </if>
MyBatis 中if 標簽 判斷字符串不生效
異常sql 的mapper 文件:
<if test="isBound != null and isBound !='' and isBound == '1'"> and box_sid is not null </if> <if test="isBound != null and isBound !='' and isBound == '2'"> and box_sid is null </if>
正確sql 的mapper 文件
<if test="isBound != null and isBound !='' and isBound == '1'.toString()"> and box_sid is not null </if> <if test="isBound != null and isBound !='' and isBound == '2'.toString()"> and box_sid is null </if>
到此這篇關(guān)于mybatis if標簽判斷不生效的解決方法的文章就介紹到這了,更多相關(guān)mybatis if標簽判斷不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java @Value("${xxx}")取properties時中文亂碼的解決
這篇文章主要介紹了Java @Value("${xxx}")取properties時中文亂碼的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot?自定義注解實現(xiàn)涉密字段脫敏
關(guān)于數(shù)據(jù)脫敏,網(wǎng)上的文章都是硬編碼規(guī)則,比如對身份證,手機號,郵件地址等固定寫法脫敏。本文在此基礎(chǔ)上,拓展動態(tài)從數(shù)據(jù)庫查出涉密關(guān)鍵字執(zhí)行脫敏操作。感興趣的同學(xué)可以參考閱讀2023-03-03使用Spring-Retry解決Spring Boot應(yīng)用程序中的重試問題
重試的使用場景比較多,比如調(diào)用遠程服務(wù)時,由于網(wǎng)絡(luò)或者服務(wù)端響應(yīng)慢導(dǎo)致調(diào)用超時,此時可以多重試幾次。用定時任務(wù)也可以實現(xiàn)重試的效果,但比較麻煩,用Spring Retry的話一個注解搞定所有,感興趣的可以了解一下2023-04-04Spring中的SpringApplicationRunListener詳細解析
這篇文章主要介紹了Spring中的SpringApplicationRunListener詳細解析,SpringApplicationRunListener是一個監(jiān)聽SpringApplication中run方法的接口,在項目啟動過程的各個階段進行事件的發(fā)布,需要的朋友可以參考下2023-11-11