Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。
2015-10-22 00:00
951 查看
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,或者对象。
版权声明:本文为博主原创文章,未经博主允许不得转载。
错误
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,或者对象。
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- 浅析Mybatis 在CS程序中的应用
- Java Mybatis框架入门基础教程
- Java简单实现SpringMVC+MyBatis分页插件
- mybatis的动态sql详解(精)
- MyBatis入门学习教程(一)-MyBatis快速入门
- 解决springmvc+mybatis+mysql中文乱码问题
- SpringMVC+MyBatis项目总结(一)
- SpringMVC+MyBatis项目总结(二)
- MyBatis返回结果不稳定
- MyBatis通过反射建立一个对象的过程。
- springMVC+spring+Mybatis
- SpringMVC+mybatis+Spring框架整合+简单实现的demo
- Spring+mybatis+shiro+freemarker+ehcache+ldap+mongo
- 继续钟爱JdbcTemplate
- 基于全注解的Spring3.1 mvc、myBatis3.1、Mysql的轻量级项目
- Mybatis的几点小结
- 懒到极致:对mybatis的进一步精简
- [续] 懒到极致:对mybatis的进一步精简