mybatis?傳入null值的解決方案
mybatis 傳入null值解決
前端傳入兩個(gè)值,如果其中一個(gè)為null時(shí),很多時(shí)候我們都很困惑,明明傳入的是null,為啥mybatis 的xml文件中的if條件判斷無效?
public String getPersonInfo(@PathParam("Name") String Name, @PathParam("IDCard") String IDCard)dao層
public List<Map> getPersonInfo(@Param("name") String name, @Param("idcard") String idcard);xml文件內(nèi)容
<if test="name == null or name == '0'.toString()">?
? ? <![CDATA[ and (b.identity_id = #{idcard,javaType=String,jdbcType=VARCHAR})
? ? group by name,id_card,birthTime,sex ?]]>?
</if>每次執(zhí)行都是失敗的,網(wǎng)上很多都說要在dao層添加@param注解和xml文件內(nèi)容中要加入jdbcType類型就能解決,最后還是沒解決
其實(shí)還有一個(gè)點(diǎn)忽略了,就是傳入時(shí)的null值其實(shí)是個(gè)字符串null,根本就不是null,它是個(gè)字符串null
? ? ? ? if (Name == null || "null".equalsIgnoreCase(Name)) {
? ? ? ? ? ? Name = null;
? ? ? ? }
? ? ? ? if (IDCard == null || "null".equalsIgnoreCase(IDCard)) {
? ? ? ? ? ? IDCard = null;
? ? ? ? }問題解決!
mybatis 注入老是為null
今天遇到個(gè)很弱智的問題,以此記錄!時(shí)刻提醒自己
public int delExhibion(List<String> ids){
Integer result = null;
ExhibitionManager exhibitionManager = new ExhibitionManager();
for (String id : ids){
exhibitionManager.setId(id);
exhibitionManager.setDelFlag("1");
result += exhibitionManagerMapper.delete(id);
}
return result;
}發(fā)現(xiàn)老是執(zhí)行 delete 的時(shí)候 ,老是報(bào) 空指針異常

然后嘗試使用: @Param 給參數(shù)加上命令
int delete(@Param("id") String id); ?結(jié)果還是不行,
然后在嘗試使用:對(duì)象傳參,這樣總該注入進(jìn)去了吧
int delete(Object dx);
結(jié)果還是不行,
然后在嘗試使用:Mybatis 單個(gè)參數(shù)動(dòng)態(tài)語句引用:
是當(dāng)我們的參數(shù)為String時(shí),在sql語句中#{id} 會(huì)去我們傳進(jìn)來的參數(shù)調(diào)getId()方法獲取參數(shù),很明顯,String沒有對(duì)應(yīng)的方法,所以報(bào)錯(cuò)了,那我們這里要如何引用id呢,需要采用下面的寫法:
<delete id="delete" parameterType="java.lang.String" > ?
? ? ? ? ?SELECT * FROM table
? ? ? ? ?<where> ?
? ? ? ? ? ? ? ? ? ?<if test="_parameter != null"> ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? AND id= #{id} ?
? ? ? ? ? ? ? ? ? ?</if> ?
? ? ? ? ?</where> ?
</select>單Mybatis傳參為單個(gè)參數(shù)時(shí),在sql語句中需要使用 _parameter 來引用這個(gè)參數(shù)
結(jié)果還是不行。
終于過了一會(huì)兒,看代碼時(shí)突然頓悟
? ? public int delExhibion(List<String> ids){
? ? ? ? Integer result = null;
? ? ? ? for (String id : ids){
? ? ? ? ? ? ?result += exhibitionManagerMapper.delete(id);
? ? ? ? }
? ? ? ? return result;
? ? }Integer result 我給他設(shè)置默認(rèn)值 為null, 并且還讓 reuslt 這個(gè)對(duì)象 result +=
加等于在執(zhí)行數(shù)據(jù)庫操作返回結(jié)果時(shí) 用 result 去接收。 這不就一定是空指針了嗎。
我給 Integer result = 0; 設(shè)置個(gè)默認(rèn)值為0 那也不會(huì)出現(xiàn)這種情況!
或者 result = 給result 初始化賦值 也不會(huì)報(bào) 空指針異常! 不過這樣做在我的業(yè)務(wù)中是不對(duì)的哦。 只是不會(huì)報(bào)錯(cuò). 但是獲取不到我想要的執(zhí)行成功條數(shù)
Integer result = null; result = ?exhibitionManagerMapper.delete(id);
真的是被自己氣到了。以此記錄!時(shí)刻提醒自己
?public int delExhibion(List<String> ids){
? ? ? ? Integer result = 0;
? ? ? ? for (String id : ids){
? ? ? ? ? ? ?result += exhibitionManagerMapper.delete(id);
? ? ? ? }
? ? ? ? return result;
? ? }以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java中如何使用BufferedImage判斷圖像通道順序并轉(zhuǎn)RGB/BGR
這篇文章主要介紹了java中如何BufferedImage判斷圖像通道順序并轉(zhuǎn)RGB/BGR的相關(guān)資料,需要的朋友可以參考下2017-03-03
從Mybatis-Plus開始認(rèn)識(shí)SerializedLambda的詳細(xì)過程
這篇文章主要介紹了從Mybatis-Plus開始認(rèn)識(shí)SerializedLambda,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
skywalking源碼解析javaAgent工具ByteBuddy應(yīng)用
這篇文章主要為大家介紹了skywalking源碼解析javaAgent工具ByteBuddy應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03

