比如有这样一个方法:
List<Order> selectListById(Integer id);
只有一个数字类型的参数,而且id参数有可能是null。
如果在xml文件里这样写:
<select id="selectListById" resultMap="baseResultMap" parameterType="java.lang.Integer">
select *
from `order` a
where a.status=0
<if test="id != null">
and id = #{id}
</if>
</select>
会引发异常:
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.Integer'
异常原因应该是test中的参数只能是parameterType类中的属性,而不是parameterType本身。
解决方案有以下几种:
1,方法中加入加@param注解,然后就可以在test中使用名字是id的参数了:
List<Order> selectListById(@param("id") Integer id);
2,如果是Integer类型参数,可以在test里用value参数:
<select id="selectListById" resultMap="baseResultMap" parameterType="java.lang.Integer">
select *
from `order` a
where a.status=0
<if test="value != null">
and id = #{id}
</if>
</select>
因为value就是Integer类里的参数,记录Integer的数字,算是个投机取巧的写法。