我的mybatis注释查询存在问题,但有以下错误。
org.apache.ibatis.binding.BindingException:找不到参数'strDateStart'。可用参数为[0,1,param1,param2]
以下是mapper类中的代码。
`@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);`
使用查询中的参数和方法中的参数给出相同的名称,我可以在具有相同参数的条件下多次使用吗?
答案 0 :(得分:1)
问题解决了 1.删除变量#{strDateXXXX}周围的单引号 2.为以下代码创建该条件的类。
`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.通过传递条件类
`@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)`
答案 1 :(得分:0)
你试过这个吗?
@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);
答案 2 :(得分:0)
我使用@SelectProvider来设置params Map,这对我有用:
Mapper类:
@SelectProvider(type = MyBatisSqlProvider.class, method = "getSqlString")
public List<Map<String, Object>> select(@Param("sql") String sqlString, @Param("sqlParams") Map<String, Object> sqlParams);
提供者类:
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;
我的表:
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}