MyBatis怎么写like查询

来源:互联网 发布:企业logo标志设计软件 编辑:程序博客网 时间:2024/05/01 12:58

原文地址:https://github.com/mybatis/mybatis-3/wiki/FAQ

How do I code an SQL LIKE?

There are two methods. In the first (and preferred) method, you append the SQL wildcards(通配符) in your Java code.
For example:

String wildcardName = "%Smi%";List<Name> names = mapper.selectLike(wildcardName);
<select id="selectLike">  select * from foo where bar like #{value}</select>

Another method is to concatenate the wildcards in your SQL. This method is less safe than the method above because of possible SQL injection.
For example:

String wildcardName = "Smi";List<Name> names = mapper.selectLike(wildcardName);
<select id="selectLike">  select * from foo where bar like '%' || '${value}' || '%'</select>

Important: Note the use of $ vs. # in the second example!

0 0
原创粉丝点击