MyBatis報OgnlException: source is null for getProperty(null, "id")異常
阿新 • • 發佈:2019-02-20
MyBatis報OgnlException: source is null for getProperty(null, “id”)異常
- 程式碼如下
<sql id="where">
<if test="requestStudent.id!=null and requestStudent.id!=''">
and tb.id = #{requestStudent.id}
</if>
<if test="requestStudent.name!=null and requestStudent.name!=''" >
and tb.name = #{requestStudent.name}
</if>
<if test="requestStudent.age!=null and requestStudent.age!=''">
and tb.age = #{requestStudent.age}
</if>
<if test="requestStudent.height!=null and requestStudent.height!=''" >
and tb.height = #{requestStudent.height}
</if>
</sql>
原因:
- 以前的MyBatis可以像上面那樣直接”“判斷是否為null,現在不行了,要先判斷requestStudent是否為null才行,否則即使requestStudent不為null也會報source is null for getProperty(null, “id”)異常,即可能會有NPE空指標異常。
解決辦法:在外層加一個if判斷就行了
<sql id="where" >
<if test="requestStudent!=null">
<if test="requestStudent.id!=null and requestStudent.id!=''">
and tb.id = #{requestStudent.id}
</if>
<if test="requestStudent.name!=null and requestStudent.name!=''">
and tb.name = #{requestStudent.name}
</if>
<if test="requestStudent.age!=null and requestStudent.age!=''">
and tb.age = #{requestStudent.age}
</if>
<if test="requestStudent.height!=null and requestStudent.height!=''">
and tb.height = #{requestStudent.height}
</if>
</if>
</sql>