溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mybatis使用Integer類型查詢出現(xiàn)的問(wèn)題怎么解決

發(fā)布時(shí)間:2022-03-14 12:01:55 來(lái)源:億速云 閱讀:227 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(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í)吧。

使用Integer類型查詢出現(xiàn)的問(wè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這個(gè)值為0的時(shí)候

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>

mybatis判斷Integer遇到的bug

場(chǎng)景產(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 != ''"這個(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è)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI