【Java】模糊查询 like 语句该怎么写?(约83字)

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

concat拼接like
  • 1 ’%${question}%’ 可能引起 SQL 注入,不推荐
  • 2 "%"#{question}"%" 注意:因为#{…}解析成 sql 语句时候,会在变量外侧自动加单引号’ ‘,所以这里 % 需要使用双引号” “,不能使用单引号 ’ ‘,不然会查不到任何结果。
  • 3 CONCAT('%',#{question},'%') 使用 CONCAT()函数,(推荐 ✨)
  • 4 使用 bind 标签(不推荐)
<select id="listUserLikeUsername" resultType="com.jourwon.pojo.User">
&emsp;&emsp;<bind name="pattern" value="'%' + username + '%'" />
&emsp;&emsp;select id,sex,age,username,password from person where username LIKE #{pattern}
</select>

THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容