模糊查询 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>
THE END
暂无评论内容