emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

<like> dynamic sql tag #85

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
myBatis 3.0.1

Please describe the problem.  Unit tests are best!
It would be nice to be able to have a <like> tag to use when dynamically 
constructing sql in mapper file.  The tag would automatically add '%' to end of 
strings for searches.
ex: AND title <like>#{title}</like>
Perhaps attributes to the tag could control where the '%' goes ... in front, 
behind, or on both ends.

What is the expected output? What do you see instead?
ex: AND title <like>#{title}</like>
  would result in a query of
   AND title '%#{title}%'

Please provide any additional information below.

Original issue reported on code.google.com by q211222...@yahoo.com on 17 Aug 2010 at 8:47

GoogleCodeExporter commented 9 years ago
78888888

Original comment by zyqpost...@gmail.com on 3 Aug 2011 at 5:49

GoogleCodeExporter commented 9 years ago
i think it could be 
<decorate before="%" after="%">#{title}</decorate> or
<pattern>%#{title}%</pattern>

Original comment by mnesa...@gmail.com on 1 Sep 2011 at 4:52

GoogleCodeExporter commented 9 years ago
Let me throw a few more ideas out there...

<field>%#{title}%</field>

or param which matches the annotation name...

<param>%#{title}%</param>
<param before="%" after="%">#{title}</param>
<param before="%" after="%" name="title"/>

Original comment by brhur...@gmail.com on 27 Oct 2011 at 12:10

GoogleCodeExporter commented 9 years ago
String statement = "afei.person.selectPersons" ;
Person param = new Person();
param.setUsername("%CD%");
param.setPassword("%11%");
List<Person> persons = session.selectList(statement,param) ;

<!-- 模糊查询Person -->
    <select  id="selectPersons" resultType="person" parameterType="person">
        select id,sex,age,username,password from person where true 
            <if test="username!=null"> AND username LIKE #{username}</if>
            <if test="password!=null">AND password LIKE #{password}</if>

    </select>

Original comment by 404961...@qq.com on 23 Dec 2011 at 5:07

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
What about a generic wrapper:

class SQLUtil<T> {
  private T m;
  private String patternPrefix = "%";
  private String patternSuffix = "%";
  public SQLUtil(T m) { this.m = m }
  public T getM() { return m }
  public String pattern(String s) { return patternPrefix + s + patternSuffix }
} 

String statement = "afei.person.selectPersons" ;
Person param = new Person();
param.setUsername("CD");
param.setPassword("11");
List<Person> persons = session.selectList(statement,new SQLUtil<Person>(param)) 
;

<select id="selectPersons" resultType="person" parameterType="sqlutil">
  select id,sex,age,username,password from person
  <where>
    <if test="m.username = null">username LIKE #{_parameter.pattern(m.username)}</if>
  </where>
</select>

Is it possible? i have not tested yet.

Original comment by mnesa...@gmail.com on 26 Mar 2012 at 3:19

GoogleCodeExporter commented 9 years ago
According to issue 210, it can be a lot easier:

1. Put a class in default package with static methods like this:

class Util {
  public static String pattern(String str, String p, String s) {
    return p + str + s;
  }
}

2. Then use it with '@':

String statement = "afei.person.selectPersons" ;
Person param = new Person();
param.setUsername("CD");
param.setPassword("11");
List<Person> persons = session.selectList(statement,param) ;

<select id="selectPersons" resultType="person" parameterType="person">
  select id,sex,age,username,password from person
  <where>
    <if test="username != null">username LIKE #{@Util@pattern(username, '%', '%')}</if>
  </where>
</select>

I have not tested it.

Original comment by mnesa...@gmail.com on 26 Mar 2012 at 4:26

GoogleCodeExporter commented 9 years ago
Sorry, It does not work.
OGNL is not supported in param positions #{...}

:S

Original comment by mnesa...@gmail.com on 26 Mar 2012 at 4:47

GoogleCodeExporter commented 9 years ago
3.2 will support this:

String statement = "afei.person.selectPersons" ;
Person param = new Person();
param.setUsername("CD");
param.setPassword("11");
List<Person> persons = session.selectList(statement,param) ;

<select id="selectPersons" resultType="person" parameterType="person">
  <bind name="pattern" value="'%' + _parameter.username + '%'" />
  select id,sex,age,username,password 
  from person
  where username LIKE #{pattern}
</select>

Original comment by mnesa...@gmail.com on 13 Jun 2012 at 7:41

GoogleCodeExporter commented 9 years ago
Use the new bind tag:

<select ...>

  <bind name="x" value=" '%' + _parameter.name + '%' " />

  SELECT * FROM person WHERE name LIKE #{x}

</select>

Original comment by mnesa...@gmail.com on 14 Jun 2012 at 4:08

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
fix in r5326

Original comment by eduardo.macarron on 16 Jun 2012 at 3:42