最近在練習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}%' 也可
然后成功