mybatis使用Integer類型查詢可能出現(xiàn)的問題
使用Integer類型查詢出現(xiàn)的問題
mapper.xml :
<select id="count" parameterType="com.pinyu.system.web.page.Page" resultType="java.lang.Integer">
?? ??? ?select count(m.id) from hr_push_msg_model as m
?? ??? ?<where>
?? ??? ??? ?<if test="page.keyword != null and page.keyword != ''">
?? ??? ??? ??? ?m.text like '%${page.keyword}%'
?? ??? ??? ?</if>
?? ??? ??? ?<if test="page.entity != null">
?? ??? ??? ??? ?<if test="page.entity.text != null and page.entity.text != ''">
?? ??? ??? ??? ??? ?and ?m.text like '%${page.entity.text}%'
?? ??? ??? ??? ?</if>
?? ??? ??? ??? ?<if test="page.entity.title != null and page.entity.title != ''">
?? ??? ??? ??? ??? ?and ?m.title like '%${page.entity.title}%'
?? ??? ??? ??? ?</if>
?? ??? ??? ??? ?<if test="page.entity.state != null and page.entity.state != ''">
?? ??? ??? ??? ??? ?and ?m.state = #{page.entity.state}
?? ??? ??? ??? ?</if>
?? ??? ??? ??? ?<if test="page.entity.type != null and page.entity.type != ''">
?? ??? ??? ??? ??? ?and ?m.type = #{page.entity.type}
?? ??? ??? ??? ?</if>
?? ??? ??? ?</if>
?? ??? ?</where>
?? ?</select>比如:
<if test="page.entity.state != null and page.entity.state != ''">
? ? ? ? ? ? ? ? ? ? and ?m.state = #{page.entity.state}
? ? ? ? ? ? ? ? </if>當(dāng)state這個值為0的時候
mybatis為默認(rèn)為空字符串"",所以如果狀態(tài)這種類似的場景有0值得,查詢就不要加上xxxx!=""這種?;蛘遫r xxx==0
代碼示例:
1、
<if test="page.entity.state != null">
? ? ?and ?m.state = #{page.entity.state}
</if>2、
<if test="page.entity.state != null and page.entity.state != '' or page.entity.state==0">
? ? and ?m.state = #{page.entity.state}
</if>mybatis判斷Integer遇到的bug
場景產(chǎn)出
需要查出狀態(tài)為0的所有用戶
我是這樣寫的
1.mapper:
BaseUser selectUserByStatus(@parm("status") Integer status);這里傳了0進(jìn)去
2.sql:
SELECT * FROM base_user WHERE status=0
3.xml片段
<if test="status != null and status != ''">
status = #{status},
</if>4.結(jié)果真正執(zhí)行的sql
SELECT * FROM base_user
小結(jié)一下:
test="status != null and status != ''"這個是拿來判斷String的?。?!也就是說Double,BigDecimal等數(shù)字類型也會出現(xiàn)這樣的情況
1.如果是Integer類型的話,如果變量的值是0,即 num = 0, mybatis在進(jìn)行 num != '' " 的時候會認(rèn)為 num 的值是空字符串;直接跳過判斷了
所以如果是Integer類型只需要判斷 != null 即可
2.如果String類型需要判斷不等于0,則需要寫name != '0'.toString(),否則會報錯。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于SpringBoot獲取IOC容器中注入的Bean(推薦)
本文通過實(shí)例代碼給大家詳解了springboot獲取ioc容器中注入的bean問題,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05
idea使用pagehelper實(shí)現(xiàn)后端分頁功能的步驟詳解
這篇文章主要介紹了idea使用pagehelper實(shí)現(xiàn)后端分頁功能的步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
探索分析Redis?AOF日志與數(shù)據(jù)持久性
這篇文章主要為大家介紹了探索分析Redis?AOF日志與數(shù)據(jù)持久性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Java之JFrame輸出Helloworld實(shí)例
這篇文章主要介紹了Java之JFrame輸出Helloworld的方法,以輸出Helloworld的實(shí)例分析了JFrame的簡單入門技巧,需要的朋友可以參考下2015-02-02
springboot+vue實(shí)現(xiàn)驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了springboot+vue實(shí)現(xiàn)驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
SpringCloud Config統(tǒng)一配置中心問題分析解決與客戶端動態(tài)刷新實(shí)現(xiàn)
springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用2022-10-10
深入理解Java定時調(diào)度(Timer)機(jī)制
這篇文章主要介紹了深入理解Java定時調(diào)度(Timer)機(jī)制,本節(jié)我們主要分析 Timer 的功能。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
Java 動態(tài)模擬操作系統(tǒng)進(jìn)程調(diào)度算法
這篇文章主要介紹了采用java語言編程模擬N個進(jìn)程采用動態(tài)高優(yōu)先權(quán)優(yōu)先進(jìn)程調(diào)度算法。文中代碼具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下2021-12-12

