您的位置:首页 > 编程语言 > Java开发

Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。

2015-02-12 13:38 936 查看
Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。

错误
List<Post> listPage(Integer categoryId);
在测试时报错:There is no getter for property named 'categoryId' in 'class java.lang.Integer'

问题分析:Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取string.value值,引起报错。
解决方法: List<Post> listPage(@Param("categoryId")Integer categoryId); 说明参数值。

sql语句
<select id="listPage" resultMap="PostResultMap">
select id,title,summary,create_time from p2p_post
where status=0
<if test="categoryId != null">
and category_id=#{categoryId}
</if>
order by id
desc
</select>

最让人郁闷的是,以前在只有1个参数的时候,都是不用@Param注解的,一般只有在多个参数的时候,才需要用。
为什么这次,只有1个参数,也必须用@Params注解呢?
-----------------------
第2天早上,问了下boss,感觉还是有道理的。

正解一:
@Select("select * from ..")
List<Post> listPage(Integer categoryId);

正解二:
List<Post> listPage(@Param("categoryId")Integer categoryId);
<select>..</select>

正解三:
List<Post> listPage(Integer categoryId);
<select id="listPage" parameterType="java.lang.Integer" resultMap="PostResultMap">

</select>

昨天遇到的那个问题,问题关键就是:xml文件中的select映射语句,默认参数类型是map,从map里取属性,所以总是找不到。
或者是当作对象类型吧。
因此,用@Param注解或手动指定参数类型。

理论上是这样,没有去一一校验。

另外需要说明,多个参数,必须使用@Param,或者用Map,或者对象。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐