MyBatis like报错
错误的likeSQL语句是这么写的
select * from student name like '%#{name}%'
下面是错误信息
Parameter index out of range (1 > number of parameters, which is 0).
#{}编译后是一个占位符,下面是编译后的结果
elect * from student WHERE name like '%?%'
?存在于引号内,Mybatis找不到占位符报以上错误
正确写法
1、有可能会引起SQL注入
使用 ${...} 代替 #{...}
SELECT * FROM tableName WHERE name LIKE '%${text}%';
2、
<select id="selectPersons" resultType="person" parameterType="person">
<bind name="pattern" value="'%' + _parameter.username + '%'" />
select id,sex,age,username,password
from person
where username LIKE #{pattern}
</select>
3、上层解决、参数带上%%
selectExample.setName("%光%");