MyBatis字符串替换$ {}因SelectProvider

时间:2016-08-09 12:10:20

标签: java mybatis

我有一张包含以下数据的表格

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)

和相应的POJO

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;
    }
}

任务是查找名称与给定模式匹配且ID大于某个值的用户

此查询可实现所需的结果:

@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]]

但是,当我使用selectProvider时字符串替换失败。

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

select provider中的方法如下:

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

但是使用selectProvider,参数替换工作正常(#{})但是   字符串替换($ {})

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
[]

有人可以将豆子溢出为何会发生这种情况吗?

1 个答案:

答案 0 :(得分:0)

连接值,如下所示:

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