There is no getter for property named 'id' in 'class java.lang.Integer'以及@Param参数的使用

使用mybatis传入参数, 当参数类型是String ,Integer 等这些时。如果用他的<if test="year != null and year != ''">标签判断该参数是否为空,通常会爆There is no getter for property named ‘year’ in ‘class java.lang.Integer异常。

也就是说如果我们不使用if标签进行判断的时候,只传单个这样的参数,是不会报这个错误的,已经亲测过。

 

但是实际上实体类中写了相关的方法,最后找到问题出现在Mapper接口中,

List<EneElectricity> getProAndLiveByMonth(Integer year);

注意只需要在mapper层中修改,其它层无需改动,将代码改为:

List<EneElectricity> getProAndLiveByMonth(@Param(value="year") Integer year);

问题解决。

 

总结:

1.mapper文件中使用if判断

如果使用了< if test="codee != null and codee !=''"> if判断,那么接口参数就需要加@Param注解。

2.mapper文件中没有使用if判断
这个时候分两种情况:
(1)接口参数只有一个,不管接口参数名是什么,这个时候#{xxx}没有限制,可以是0,param1,也可以是aaa,bbb。可以不加注解。
(2)当接口参数大于一个的时候,mybatis的参数集就是上边说的默认值[0, 1, param1, param2],如果你不用默认值,就需要加上@Param注解起别名。一旦加了注解,mybatis的参数集就和第一种情况一样了,这个时候也就不再区分是否使用if判断。

 

posted @ 2020-07-10 11:08  万里哥  阅读(908)  评论(0编辑  收藏  举报