Mybatis模糊查询
<select id="selectAllByNameAList" resultType="a"> select * from a where name like '%${name}%'; </select>
但是有sql注入风险
下面是写法是错误的 #{xxx}会直接以字符串拼接
实际sql就是select * from a where name like '%#{name}%'
<select id="selectAllByNameAList" resultType="a"> select * from a where name like '%#{name}%'; </select>
用CONCAT函数
SELECT * FROM a WHERE name LIKE CONCAT('%', #{name}, '%')
-
#{}
是参数绑定,不是字符串替换; -
写在
'%#{name}%'
中,MyBatis 根本不解析; -
JDBC 实际拿到的 SQL 是没有问号
?
的,所以你传了参数,它不知道往哪塞; -
所以报错:“没有参数位置,却传了参数”