错误的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 >
  <bind name="pattern" value="'%' + _parameter.username + '%'" />
  select id,sex,age,username,password 
  from person
  where username LIKE #{pattern}
</select>

3、上层解决、参数带上%%

selectExample.setName("%光%");

相关文章:

  • 2022-01-08
  • 2022-03-04
  • 2021-09-01
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
猜你喜欢
  • 2021-08-22
  • 2021-07-01
  • 2022-02-16
  • 2021-05-08
  • 2022-01-16
  • 2021-11-08
  • 2022-12-23
相关资源
相似解决方案