最近在练习MyBatis时 进行姓名的模糊查询时候出现
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'name' in 'class java.lang.String' ### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'name' in 'class java.lang.String'
我的mapper中的select如下
<select id="listAllByName" resultType="edu.ifel.mybatis_test.entitys.Student"> SELECT sno,sname,ssex,sage FROM Student WHERE sname LIKE '%${name}%'
</select>
测试类中这样调用 :
String sname = "李"; List<Student> studentList = studentDAO.listAllByName(sname);
经过一番查找发现 此处将参数映射为String类型完全正确,但是在映射到<select>中时 '%${name}%' 相当于调用了 sname.name (即参数.name)
解决方案如下 :
方案一 : 只需将 '%${name}%' 变为 '%${_parameter}%' 即可
方案二 : 将 '%${name}%' 变为 '%${value}%' 也可
然后成功