模糊查询like语句该怎么写?
模糊查询like语句该怎么写?

- 1 使用
%${question}%可能会导致SQL注入,不建议使用。
- 2 使用
"%"#{question}"%"需要注意,因为#{...}解析为SQL语句时会自动在变量外加上单引号',所以这里的%需要使用双引号" ",而不能使用单引号',否则可能导致无法查询到结果。 - 3 推荐使用
CONCAT('%',#{question},'%'),通过CONCAT()函数拼接字符串来实现模糊查询。 - 4 使用
<bind>标签是一种方法,但不推荐使用。
<select id="listUserLikeUsername" resultType="com.jourwon.pojo.User">
  <bind name="pattern" value="'%' + username + '%'" />
  select id,sex,age,username,password from person where username LIKE #{pattern}
</select>