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>當state這個值為0的時候
mybatis為默認為空字符串"",所以如果狀態(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進去
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在進行 num != '' " 的時候會認為 num 的值是空字符串;直接跳過判斷了
所以如果是Integer類型只需要判斷 != null 即可
2.如果String類型需要判斷不等于0,則需要寫name != '0'.toString(),否則會報錯。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
關于SpringBoot獲取IOC容器中注入的Bean(推薦)
本文通過實例代碼給大家詳解了springboot獲取ioc容器中注入的bean問題,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05
idea使用pagehelper實現(xiàn)后端分頁功能的步驟詳解
這篇文章主要介紹了idea使用pagehelper實現(xiàn)后端分頁功能的步驟,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
探索分析Redis?AOF日志與數(shù)據(jù)持久性
這篇文章主要為大家介紹了探索分析Redis?AOF日志與數(shù)據(jù)持久性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
SpringCloud Config統(tǒng)一配置中心問題分析解決與客戶端動態(tài)刷新實現(xiàn)
springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應用2022-10-10
Java 動態(tài)模擬操作系統(tǒng)進程調(diào)度算法
這篇文章主要介紹了采用java語言編程模擬N個進程采用動態(tài)高優(yōu)先權(quán)優(yōu)先進程調(diào)度算法。文中代碼具有一定的學習價值,感興趣的小伙伴可以了解一下2021-12-12

