传递参数和检索键值结果集时出错
Error passing parameters and retrieving key value result set
我无法理解 MyBatis 如何映射传入的参数,然后将结果编组回调用者。我想传入一个 POJO,使用其中设置的属性作为查询的一部分,然后以地图形式传回结果。
我的Mapping文件函数(不使用参数):
<select id="getTotalUniqueUserDetails"
resultType="map"
parameterType="RequestFilter">
select KEY,sum(SINGLE_VALUE) as TOTAL from (
select schut.user_type_name as KEY, schuc.usage_count AS SINGLE_VALUE
from usage_count schuc
left join users schu
on schuc.user_key=schu.user_key
left join user_types schut
on schut.user_type_id=schu.user_type_id
)
GROUP by KEY;
</select>
我的Mapping文件函数(带参数)
<select id="getTotalUniqueUserDetails"
resultType="map"
parameterType="RequestFilter">
select KEY,sum(SINGLE_VALUE) as TOTAL from (
select schut.user_type_name as KEY, schuc.usage_count AS SINGLE_VALUE
from usage_count schuc
left join users schu
on schuc.user_key=schu.user_key
left join user_types schut
on schut.user_type_id=schu.user_type_id
where schuc.year_month between
to_date('#{fromDate}','yyyy-mm-dd')
and to_date('#{toDate}','yyyy-mm-dd')
)
GROUP by KEY;
</select>
我的映射器界面
public interface TotalUniqueUsers {
Object getTotalUniqueUserDetails(RequestFilter filter);
}
public class RequestFilter {
private String fromDate;
private String toDate;
... getters and setters for both above
}
The code that calls the query:
SqlSession sqlSession = DBConnection.getInstance().getSqlSession();
System.out.println("@@@@"+filter.getFromDate());
System.out.println("@@@@"+filter.getToDate());
TotalUniqueUsers testMapper = sqlSession.getMapper(TotalUniqueUsers.class);
return testMapper.getTotalUniqueUserDetails(filter);
With no parameters used in mapping file I get this error:
## The error occurred while setting parameters
### SQL: select KEY,sum(SINGLE_VALUE) as TOTAL from ( select schut.user_type_name as KEY, schuc.usage_count AS SINGLE_VALUE from usage_count schuc left join users schu on schuc.user_key=schu.user_key left join user_types schut on schut.user_type_id=schu.user_type_id ) GROUP by KEY;
### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
With parameters referenced in mapping file I get this error:
### Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='fromDate', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Invalid column index
If you've read this much, thank you from the Woodsman.
In short, if I use the parameter passed in the object, it says it can't use a null value. If I don't then it gives me some other error.
The result set should be something like
KEY TOTAL
GROUP1 33
GROUP2 55
I was hoping I could just let it stuff the result set into a map, with the keys being "GROUP1", "GROUP2", with their respective values.
Please tell me how to properly refer to the properties in the object in the map and how to marshall it back. Should I use a custom object?
您需要删除...
- 语句末尾的分号。尽管并非所有驱动程序都拒绝它,但这是使用 Oracle JDBC 驱动程序时出现 ORA-00933 的常见原因。
- 参数占位符周围的单引号,例如
'#{toDate}'
到 #{toDate}
.
使用单引号时,to_date
函数的第一个参数变为文字而不是 java.sql.PreparedStatement
占位符(例如 to_date('?', 'yyyy-mm-dd')
而不是 to_date(?, 'yyyy-mm-dd')
)。当 MyBatis 尝试调用 PreparedStatement#setString(int parameterIndex, String parameterValue)
时,语句中没有占位符,驱动程序抛出异常。
我无法理解 MyBatis 如何映射传入的参数,然后将结果编组回调用者。我想传入一个 POJO,使用其中设置的属性作为查询的一部分,然后以地图形式传回结果。
我的Mapping文件函数(不使用参数):
<select id="getTotalUniqueUserDetails"
resultType="map"
parameterType="RequestFilter">
select KEY,sum(SINGLE_VALUE) as TOTAL from (
select schut.user_type_name as KEY, schuc.usage_count AS SINGLE_VALUE
from usage_count schuc
left join users schu
on schuc.user_key=schu.user_key
left join user_types schut
on schut.user_type_id=schu.user_type_id
)
GROUP by KEY;
</select>
我的Mapping文件函数(带参数)
<select id="getTotalUniqueUserDetails"
resultType="map"
parameterType="RequestFilter">
select KEY,sum(SINGLE_VALUE) as TOTAL from (
select schut.user_type_name as KEY, schuc.usage_count AS SINGLE_VALUE
from usage_count schuc
left join users schu
on schuc.user_key=schu.user_key
left join user_types schut
on schut.user_type_id=schu.user_type_id
where schuc.year_month between
to_date('#{fromDate}','yyyy-mm-dd')
and to_date('#{toDate}','yyyy-mm-dd')
)
GROUP by KEY;
</select>
我的映射器界面
public interface TotalUniqueUsers {
Object getTotalUniqueUserDetails(RequestFilter filter);
}
public class RequestFilter {
private String fromDate;
private String toDate;
... getters and setters for both above
}
The code that calls the query:
SqlSession sqlSession = DBConnection.getInstance().getSqlSession();
System.out.println("@@@@"+filter.getFromDate());
System.out.println("@@@@"+filter.getToDate());
TotalUniqueUsers testMapper = sqlSession.getMapper(TotalUniqueUsers.class);
return testMapper.getTotalUniqueUserDetails(filter);
With no parameters used in mapping file I get this error:
## The error occurred while setting parameters
### SQL: select KEY,sum(SINGLE_VALUE) as TOTAL from ( select schut.user_type_name as KEY, schuc.usage_count AS SINGLE_VALUE from usage_count schuc left join users schu on schuc.user_key=schu.user_key left join user_types schut on schut.user_type_id=schu.user_type_id ) GROUP by KEY;
### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
With parameters referenced in mapping file I get this error:
### Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='fromDate', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Invalid column index
If you've read this much, thank you from the Woodsman.
In short, if I use the parameter passed in the object, it says it can't use a null value. If I don't then it gives me some other error.
The result set should be something like
KEY TOTAL
GROUP1 33
GROUP2 55
I was hoping I could just let it stuff the result set into a map, with the keys being "GROUP1", "GROUP2", with their respective values.
Please tell me how to properly refer to the properties in the object in the map and how to marshall it back. Should I use a custom object?
您需要删除...
- 语句末尾的分号。尽管并非所有驱动程序都拒绝它,但这是使用 Oracle JDBC 驱动程序时出现 ORA-00933 的常见原因。
- 参数占位符周围的单引号,例如
'#{toDate}'
到#{toDate}
.
使用单引号时,to_date
函数的第一个参数变为文字而不是java.sql.PreparedStatement
占位符(例如to_date('?', 'yyyy-mm-dd')
而不是to_date(?, 'yyyy-mm-dd')
)。当 MyBatis 尝试调用PreparedStatement#setString(int parameterIndex, String parameterValue)
时,语句中没有占位符,驱动程序抛出异常。