Mybatis 注入老是為null


今天遇到個很弱智的問題,以此記錄!時刻提醒自己

    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;
    }

發現老是執行 delete 的時候 ,老是報 空指針異常

然后嘗試使用:  @Param  給參數加上命令     

int delete(@Param("id") String id);  

結果還是不行,

然后在嘗試使用:對象傳參 ,  這樣總該注入進去了吧 

        int delete(Object dx);

結果還是不行,

然后在嘗試使用:Mybatis 單個參數動態語句引用:

是當我們的參數為String時,在sql語句中#{id} 會去我們傳進來的參數調getId()方法獲取參數,很明顯,String沒有對應的方法,所以報錯了,那我們這里要如何引用id呢,需要采用下面的寫法:

<delete id="delete" parameterType="java.lang.String" >  
         SELECT * FROM table
         <where>  
                   <if test="_parameter != null">  
                            AND id= #{id}  
                   </if>  
         </where>  
</select>

單Mybatis傳參為單個參數時,在sql語句中需要使用  _parameter 來引用這個參數

結果還是不行。  

終於過了一會兒,看代碼時突然頓悟。

 

    public int delExhibion(List<String> ids){
        Integer result = null;
        for (String id : ids){
             result += exhibitionManagerMapper.delete(id);
        }
        return result;
    }

Integer  result  我給他設置默認值  為null,   並且還讓  reuslt  這個對象   result +=   

加等於在執行數據庫操作返回結果時 用 result 去接收。 這不就一定是空指針了嗎。

我給 Integer result = 0;  設置個默認值為0   那也不會出現這種情況!

或者 result =   給result  初始化賦值     也不會報  空指針異常!  不過這樣做在我的業務中是不對的哦。  只是不會報錯. 但是獲取不到我想要的執行成功條數

Integer result = null;
result =  exhibitionManagerMapper.delete(id);

真的是被自己氣到了。以此記錄!時刻提醒自己   

 public int delExhibion(List<String> ids){
        Integer result = 0;
        for (String id : ids){
             result += exhibitionManagerMapper.delete(id);
        }
        return result;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM