您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“mybatis使用Integer類型查詢出現(xiàn)的問(wèn)題怎么解決”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“mybatis使用Integer類型查詢出現(xiàn)的問(wèn)題怎么解決”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
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>
mybatis為默認(rèn)為空字符串"",所以如果狀態(tài)這種類似的場(chǎng)景有0值得,查詢就不要加上x(chóng)xxx!=""這種?;蛘遫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>
需要查出狀態(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 != ''"這個(gè)是拿來(lái)判斷String的?。?!也就是說(shuō)Double,BigDecimal等數(shù)字類型也會(huì)出現(xiàn)這樣的情況
1.如果是Integer類型的話,如果變量的值是0,即 num = 0, mybatis在進(jìn)行 num != '' " 的時(shí)候會(huì)認(rèn)為 num 的值是空字符串;直接跳過(guò)判斷了
所以如果是Integer類型只需要判斷 != null 即可
2.如果String類型需要判斷不等于0,則需要寫name != '0'.toString(),否則會(huì)報(bào)錯(cuò)。
讀到這里,這篇“mybatis使用Integer類型查詢出現(xiàn)的問(wèn)題怎么解決”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。