错误代码:
<select id ="findPageListByConditon" resultType="model.hosp.HospitalSet">
select * from hospital_set
<where>
<if test="queryVo.hosname != null and queryVo.hosname != ''">
and hosname = #{queryVo.hosname,jdbcType=VARCHAR}
</if>
<if test="queryVo.hoscode != null and queryVo.hoscode != ''">
and hoscode = #{queryVo.hoscode,jdbcType=VARCHAR}
</if>
</where>
</select>
原因:
以前的MyBatis可以直接判断对象属性是否为null,现在要先判断queryVo是否为null才行,否则即使queryVo不为null也会报source is null for getProperty(null, “hosname”)异常,即可能会有空指针异常。
解决方法:
在外面再加一个<if>标签外判断传入对象是否为空即可
代码如下:
<select id ="findPageListByConditon" resultType="model.hosp.HospitalSet">
select * from hospital_set
<where>
<if test="queryVo!=null">
<if test="queryVo.hosname != null and queryVo.hosname != ''">
and hosname = #{queryVo.hosname,jdbcType=VARCHAR}
</if>
<if test="queryVo.hoscode != null and queryVo.hoscode != ''">
and hoscode = #{queryVo.hoscode,jdbcType=VARCHAR}
</if>
</if>
</where>
</select>