简体   繁体   中英

MyBatis String substitution ${} fails with SelectProvider

I have a table with following data

ee2=> select * from emp;
 id |     name      
----+---------------
  1 | Hemant
  2 | Hem-Awesome
  3 | John A
  4 | John B
  5 | Hem-Awesome 2
  6 | Hem-Awesome 3
  7 | Hem-Awesome 4
(7 rows)

And the corressponding POJO as

public class Employee {
    private int id;
    private String name;    
    public int getId() {
        return id;
    }    
    public void setId(int id) {
        this.id = id;
    }    
    public String getName() {
        return name;
    }    
    public void setName(String name) {
        this.name = name;
    }
}

The task was to find users with name matching the given pattern and id greater than some value

This query achieves the desired result:

@Select("SELECT id, name from emp where name ilike '%${filter}%' and id > #{id}")
    public List<Employee> listEmpByName(@Param("filter")String filter, @Param("id") int id);

17:28:40.742 [main] DEBUG c.e.d.m.FeedbackMapper.listEmpByName - ooo Using Connection [org.postgresql.jdbc4.Jdbc4Connection@1e257dea]
17:28:40.753 [main] DEBUG c.e.d.m.FeedbackMapper.listEmpByName - ==>  Preparing: SELECT id, name from emp where name ilike '%hem%' and id > ? 
17:28:40.801 [main] DEBUG c.e.d.m.FeedbackMapper.listEmpByName - ==> Parameters: 2(Integer)
17:28:40.836 [main] DEBUG c.e.d.m.FeedbackMapper.listEmpByName - <==      Total: 3
[Employee [id=5, name=Hem-Awesome 2], Employee [id=6, name=Hem-Awesome 3], Employee [id=7, name=Hem-Awesome 4]]

However the string substitution fails when I use selectProvider instead.

@SelectProvider(type = com.hem.MapperSQLBuilder.class, method = "listEmpByNameProvider")
    public List<Employee> listEmpByNameProvider(@Param("filter")String filter, @Param("id") int id);

The method in select provider is as follows:

public String listEmpByNameProvider(Map<String, Object> parameters) {
        System.out.println(parameters);
        return "SELECT id, name from emp where name ilike '%${filter}%' and id > #{id}";
    }

But using selectProvider, parameter substitution works fine (#{}) but String substitution (${})

17:35:40.146 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection
17:35:40.202 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [org.postgresql.jdbc4.Jdbc4Connection@7c6daa]
17:35:40.204 [main] DEBUG c.e.d.m.F.listEmpByNameProvider - ooo Using Connection [org.postgresql.jdbc4.Jdbc4Connection@7c6daa]
17:35:40.215 [main] DEBUG c.e.d.m.F.listEmpByNameProvider - ==>  Preparing: SELECT id, name from emp where name ilike '%${filter}%' and id > ? 
17:35:40.293 [main] DEBUG c.e.d.m.F.listEmpByNameProvider - ==> Parameters: 2(Integer)
17:35:40.338 [main] DEBUG c.e.d.m.F.listEmpByNameProvider - <==      Total: 0
[]

can someone spill the beans as why this may be happening?

连接值,如下所示:

SELECT id, name from emp where name like '%"+${filter}+"%' and id > #{id}.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM