Skip to content

Instantly share code, notes, and snippets.

@codethereforam
Created January 30, 2019 06:19
Show Gist options
  • Save codethereforam/66e3911bb419772e694e1a241a562e3f to your computer and use it in GitHub Desktop.
Save codethereforam/66e3911bb419772e694e1a241a562e3f to your computer and use it in GitHub Desktop.
mybaits模糊查询使用<bind>标签
<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>

模糊查询一般有三种方式:

  1. Java代码里拼接匹配符: 代码和SQL耦合度高;查看xml不能直接看出查询条件,降低开 发效率;有可能在service层多次加%_
  2. SQL里用concat拼接匹配符:增加数据库运算
  3. 使用<bind>:Java 代码做连接,推荐使用
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment