【mybatis】MyBatis报OgnlException: source is null for getProperty(null, “id”)异常

先看下面的mapper中的写法


    

            
                and tb.id = #{requestStudent.id}
            

            
                and tb.name = #{requestStudent.name}
            
            
                and tb.age = #{requestStudent.age}
            
            
                and tb.height = #{requestStudent.height}
            
    

MyBatis报OgnlException: source is null for getProperty(null, “id”)异常的原因如下:

以前的MyBatis可以像上面那样直接”requestStudent.id“判断是否为null,现在不行了,要先判断requestStudent是否为null才行(因为requestStudent也是个对象也可能为NULL),否则即使requestStudent不为null也会报source is null for getProperty(null, “id”)异常,即可能会有NPE空指针异常。

解决办法:在外层加一个if判断就行了,看下面的改写

 
        
            
                and tb.id = #{requestStudent.id}
            

            
                and tb.name = #{requestStudent.name}
            
            
                and tb.age = #{requestStudent.age}
            
            
                and tb.height = #{requestStudent.height}
            
        
    

 

 

你可能感兴趣的:(开发中遇到的问题)