mybatis框架,在进行测试类测试时候一直报错,如下:
org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'roleCode ’ of ‘class cn.smbms.entity.Role’ with value ‘SMBMS_EMPLOYEE’ Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'roleCode ’ in ‘class cn.smbms.entity.Role’
The error may exist in cn/smbms/dao/user/UserMapper.xml
The error may involve cn.smbms.dao.user.UserMapper.getUserListByRoleId-Inline
The error occurred while setting parameters
SQL: select u.*,r.id as r_id,r.roleCode,r.roleName from smbms_user u ,smbms_role r where u.userRole=? and u.userRole=r.id
Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'roleCode ’ of ‘class cn.smbms.entity.Role’ with value ‘SMBMS_EMPLOYEE’ Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'roleCode ’ in ‘class cn.smbms.entity.Role’
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
前前后后找了好几表代码也没错呀,报的是UserMapper.xml中可能错了,说roleCode没找到,无法赋值,在里面找了好几遍,也没发信错误呀。
这是UserMapper.xml里面的错误的sql
<resultMap type="user" id="userRoleResult">
<id property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<result property="userRole" column="userRole"/>
<association property="role" javaType="Role">
<id property="id" column="r_id"/>
<result property="roleCode " column="roleCode"/>
<result property="roleName" column="roleName"/>
</association>
</resultMap>
<select id="getUserListByRoleId" resultMap="userRoleResult" parameterType="Integer">
select u.*,r.id as r_id,r.roleCode,r.roleName
from smbms_user u ,smbms_role r
where u.userRole=#{userRole} and u.userRole=r.id
</select>
仔仔细细看了好几遍才发现原来是property="roleCode " 中多了个空格,把空格去掉,property="roleCode",就没问题了,就是一个空格,不小心还真不容易发现,写代码一定认真,马虎不得呀。