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 id="getByName" parameterType="string" resultType="users">
select id,username,birthday,sex,address
from users where username like '%${name}%'
</select>
测试类代码:
@Test
public void testGetByName(){
List<Users>list = uMapper.getByName("小");
list.forEach(users -> System.out.println(users));
解决方法:
方法一:将 '%${name}%' 变为 '%${_parameter}%'
修改后的代码
<select id="getByName" parameterType="string" resultType="users">
select id,username,birthday,sex,address
from users where username like '%${_parameter}%'
</select>
方法二:优化后的迷糊查询: '%${name}%' 变为
concat('%',#{name},'%')
修改后的代码:
<select id="getByNameGood" parameterType="string" resultType="users">
select id,username,birthday,sex,address from users where username like concat('%',#{name},'%')
</select>
问题解决。