mybatis if標(biāo)簽判斷不生效的解決方法
實(shí)際需求
<if test='computationRule == ’1’'> FROM app_sz_bbb a</if><if test='computationRule == ’2’'> FROM app_ccc a</if>
這種情況不生效,
原因:mybatis是用OGNL表達(dá)式來(lái)解析的,在OGNL的表達(dá)式中,’0’會(huì)被解析成字符,java是強(qiáng)類型的,char 和 一個(gè)string 會(huì)導(dǎo)致不等,所以if標(biāo)簽中的sql不會(huì)被解析。
先說(shuō)怎么解決
三種:
加 .toString()
<if test='computationRule == ’1’.toString()'> FROM app_sz_bbb a</if><if test='computationRule == ’2’.toString()'> FROM app_ccc a</if>
choose when 標(biāo)簽代替
<choose> <when test='computationRule == ’1’'> FROM app_sz_bbb a </when> <otherwise> FROM app_sz_bbb a </otherwise> </choose>
單引號(hào) 換成雙引號(hào)
<if test=’computationRule == '1'’> FROM app_sz_bbb a</if><if test=’computationRule == '2'’> FROM app_ccc a</if>MyBatis 中if 標(biāo)簽 判斷字符串不生效
異常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標(biāo)簽判斷不生效的解決方法的文章就介紹到這了,更多相關(guān)mybatis if標(biāo)簽判斷不生效內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. SQLite教程(十四):C語(yǔ)言編程實(shí)例代碼(2)2. Linux安裝MariaDB數(shù)據(jù)庫(kù)的實(shí)例詳解3. Windows下不能啟動(dòng)mysql服務(wù)--錯(cuò)誤總結(jié)4. Centos7 下mysql重新啟動(dòng)MariaDB篇5. python之sqlalchemy創(chuàng)建表的實(shí)例詳解6. Mysql InnoDB的鎖定機(jī)制實(shí)例詳解7. Spark臨時(shí)表tempView的注冊(cè)/使用/注銷/注意事項(xiàng)(推薦)8. debian10 mariadb安裝過(guò)程詳解9. 使用SQL Server分區(qū)表功能提高數(shù)據(jù)庫(kù)的讀寫(xiě)性能10. SQLite教程(六):表達(dá)式詳解
