有时候在写完代码之后,运行程序会碰到一些让人觉得奇怪的问题,也是需要对框架去多多了解,下面遇到的也是一个经常会遇到问题。
严重: Servlet.service() for servlet [springMVC] in context with path [/MA_IBA] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class com.vo.ParamVO'] with root cause
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class com.vo.ParamVO'
在DAO层的代码是
public interface AccountDao {
/** 查询所有账户 **/
public List<AccountVO> listAccount(ParamVO param);
/** 查询账户个数 **/
public int getAccountNumByParam(ParamVO param);
}
Mapper文件的SQL语句:
<select id="listAccount" resultType="java.util.List" resultMap="accountMap">
select id,account_id,account_name,password from m_account
where ( account_name like #{param.search} ESCAPE '/' )
order by ${param.sort} ${param.order} LIMIT #{param.limit} OFFSET #{param.offset};
</select>
<select id="getAccountNumByParam" resultType="java.lang.Integer">
select count(*) from m_account
where ( account_name like #{param.search} ESCAPE '/' )
order by #{param.sort} #{param.order};
</select>
在实体类中没有 param 这个变量,却报了 param 没有 getter 这个方法,这就很纳闷,后来在百度上看到别人的博客,才发现了为什么会出现这个问题,因为
Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取 paramVO.param值,引起报错。
解决办法: 在Dao层接口中给相应的方法加上参数说明,如下:
public interface AccountDao {
/** 查询所有账户 **/
public List<AccountVO> listAccount(@Param("param") ParamVO param);
/** 查询账户个数 **/
public int getAccountNumByParam(@Param("param") ParamVO param);
}
至此,就解决了程序报错问题。
转载的一篇文章:
https://blog.csdn.net/Edison_03/article/details/77163792