使用SSM框架做项目时,使用到了XML中的判断条件查询方式,代码如下:
<if test="machineInfo.name != null and machineInfo.name != ''"> AND b.name like CONCAT('%',#{machineInfo.name},'%') </if>也就是如果有值,按条件查询,如果没有传入条件,则展示所有,运行项目时报错:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 'machineInfo.name != null and machineInfo.name != '''. Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "name")
source is null for getProperty(null, "name")
这个相当于Mybatis封装的空指针异常,单没有传入条件时,machineInfo这个对象即为空,而进行条件判断machineInfo.name,自然会报空指针。
解决方法是加一层判断,先判断machineInfo是不是空:
<if test="machineInfo != null and machineInfo != ''"> <if test="machineInfo.name != null and machineInfo.name != ''"> AND b.name like CONCAT('%',#{machineInfo.name},'%') </if> </if>这样,没有传入任何参数时,也不会报错了,这里记录一下这个错误