mybatis 注释 select 查询条件中多次使用的参数
mybatis annotation select query with parameter which is use multiple times in conditions
mybatis 注释查询出现问题,出现以下错误。
org.apache.ibatis.binding.BindingException: Parameter 'strDateStart' not found. Available parameters are [0, 1, param1, param2]
下面是mapper中的代码class.
`@Select("SELECT * FROM Result WHERE"AND proc_date >= '#{strDateStart}'"+
"AND proc_date >= '#{strDateEnd}'"+
"AND update_date <= '#{strDateStart}'"+
"AND update_date <= '#{strDateEnd}'")
public ArrayList<ResultDao> select(String strDateStart,String strDateEnd);`
为查询中的参数和方法中的参数指定相同的名称,我可以在具有相同参数的条件下多次使用吗?
你试过这个吗?
@Select("SELECT * FROM Result WHERE"AND proc_date >= '#{strDateStart}'"+
"AND proc_date >= '#{strDateEnd}'"+
"AND update_date <= '#{strDateStart}'"+
"AND update_date <= '#{strDateEnd}'")
public ArrayList<ResultDao> select( @Param("strDateStart") String strDateStart, @Param("strDateEnd") String strDateEnd);
问题已解决
1. 删除变量#{strDateXXXX}
两边的单引号
2. 按照以下代码为该条件创建 class。
`public class SelectConditions {
String strDateStart;
String strDateEnd;
public String getStrDateStart() {
return strDateStart;
}
public void setStrDateStart(String strDateStart) {
this.strDateStart = strDateStart;
}
public String getStrDateEnd() {
return strDateEnd;
}
public void setStrDateEnd(String strDateEnd) {
this.strDateEnd = strDateEnd;
}
public SelectConditions(String strDateStart, String strDateEnd) {
this.strDateStart = strDateStart;
this.strDateEnd = strDateEnd;
}`
3.通过传递条件class
改变映射器class
`@Select("SELECT * FROM Result WHERE"AND proc_date >= #{strDateStart}"+
"AND proc_date >= #{strDateEnd}"+
"AND update_date <= #{strDateStart}"+
"AND update_date <= #{strDateEnd}")
public ArrayList select(SelectConditions conditions)`
我使用@SelectProvider 来设置 params Map,这对我有用:
映射器class:
@SelectProvider(type = MyBatisSqlProvider.class, method = "getSqlString")
public List<Map<String, Object>> select(@Param("sql") String sqlString, @Param("sqlParams") Map<String, Object> sqlParams);
供应商class:
String sqlString = (String) params.get(SQL_PATH);
Map<String, Object> sqlParams = (Map<String, Object>) params.get(SQL_PARAM);
if (sqlParams != null) {
params.putAll(sqlParams);
}
return sqlString;
我的table:
create table TBLSAMPLE (id int primary key auto_increment, name varchar, name1 varchar);
insert into TBLSAMPLE (name,name1) values ('test','test');
insert into TBLSAMPLE (name,name1) values ('test','test');
insert into TBLSAMPLE (name,name1) values ('test','test');
insert into TBLSAMPLE (name,name1) values ('test','test');
insert into TBLSAMPLE (name,name1) values ('qa','test');
并使用 sql 进行测试:SELECT * FROM TBLSAMPLE WHERE name= #{name} AND name1= #{name}
mybatis 注释查询出现问题,出现以下错误。
org.apache.ibatis.binding.BindingException: Parameter 'strDateStart' not found. Available parameters are [0, 1, param1, param2]
下面是mapper中的代码class.
`@Select("SELECT * FROM Result WHERE"AND proc_date >= '#{strDateStart}'"+
"AND proc_date >= '#{strDateEnd}'"+
"AND update_date <= '#{strDateStart}'"+
"AND update_date <= '#{strDateEnd}'")
public ArrayList<ResultDao> select(String strDateStart,String strDateEnd);`
为查询中的参数和方法中的参数指定相同的名称,我可以在具有相同参数的条件下多次使用吗?
你试过这个吗?
@Select("SELECT * FROM Result WHERE"AND proc_date >= '#{strDateStart}'"+
"AND proc_date >= '#{strDateEnd}'"+
"AND update_date <= '#{strDateStart}'"+
"AND update_date <= '#{strDateEnd}'")
public ArrayList<ResultDao> select( @Param("strDateStart") String strDateStart, @Param("strDateEnd") String strDateEnd);
问题已解决
1. 删除变量#{strDateXXXX}
两边的单引号
2. 按照以下代码为该条件创建 class。
`public class SelectConditions {
String strDateStart;
String strDateEnd;
public String getStrDateStart() {
return strDateStart;
}
public void setStrDateStart(String strDateStart) {
this.strDateStart = strDateStart;
}
public String getStrDateEnd() {
return strDateEnd;
}
public void setStrDateEnd(String strDateEnd) {
this.strDateEnd = strDateEnd;
}
public SelectConditions(String strDateStart, String strDateEnd) {
this.strDateStart = strDateStart;
this.strDateEnd = strDateEnd;
}`
3.通过传递条件class
`@Select("SELECT * FROM Result WHERE"AND proc_date >= #{strDateStart}"+
"AND proc_date >= #{strDateEnd}"+
"AND update_date <= #{strDateStart}"+
"AND update_date <= #{strDateEnd}")
public ArrayList select(SelectConditions conditions)`
我使用@SelectProvider 来设置 params Map,这对我有用:
映射器class:
@SelectProvider(type = MyBatisSqlProvider.class, method = "getSqlString")
public List<Map<String, Object>> select(@Param("sql") String sqlString, @Param("sqlParams") Map<String, Object> sqlParams);
供应商class:
String sqlString = (String) params.get(SQL_PATH);
Map<String, Object> sqlParams = (Map<String, Object>) params.get(SQL_PARAM);
if (sqlParams != null) {
params.putAll(sqlParams);
}
return sqlString;
我的table:
create table TBLSAMPLE (id int primary key auto_increment, name varchar, name1 varchar);
insert into TBLSAMPLE (name,name1) values ('test','test');
insert into TBLSAMPLE (name,name1) values ('test','test');
insert into TBLSAMPLE (name,name1) values ('test','test');
insert into TBLSAMPLE (name,name1) values ('test','test');
insert into TBLSAMPLE (name,name1) values ('qa','test');
并使用 sql 进行测试:SELECT * FROM TBLSAMPLE WHERE name= #{name} AND name1= #{name}