如何从 MyBatis 获取 Map<String, Integer>?原因:java.sql.SQLException:getInt() 的无效值 - 'NONE'
How do I get a Map<String, Integer> from MyBatis? Cause: java.sql.SQLException: Invalid value for getInt() - 'NONE'
我试着关注 Return HashMap in mybatis and use it as ModelAttribute in spring MVC (Option 1) and Mybatis ResultMap is HashMap<String,Object>。我有
@Select("select sources.host as 'key', count(*) total ... group by host")
@MapKey("key")
Map<String, Integer> getSourcesById(@Param("id")String id, @Param("hitDate")Date hitDate);
它returns一个错误
Error attempting to get column 'key' from result set. Cause: java.sql.SQLException: Invalid value for getInt() - 'NONE'
; SQL []; Invalid value for getInt() - 'NONE'; nested exception is java.sql.SQLException: Invalid value for getInt() - 'NONE'
查询在 MySQL 和 returns
中工作正常
| key | total |
+-------------+-------+
| NONE | 33 |
| twitter.com | 1 |
好像没有使用@MapKey
注解
我试过了
List<AbstractMap.SimpleEntry<String, Integer>> getSourcesById(...)
但它给了
nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in java.util.AbstractMap$SimpleEntry matching [java.lang.String, java.lang.Long]
也试过同样的错误。
List<AbstractMap.SimpleEntry<String, Long>>
https://docs.oracle.com/javase/7/docs/api/java/util/AbstractMap.SimpleEntry.html#constructor_summary
MyBatis 3.4.6,MyBatis-Spring1.3.2
我必须自己构建 class,但我不喜欢它。愚蠢的是,我必须添加这么多代码才能从数据库中获取简单的 key/value 对。
Pair.java
public class Pair<T1, T2> {
public T1 key;
public T2 value;
public Pair(T1 k, T2 v) {
key = k;
value = v;
}
public Pair(String k, Long v) {
key = (T1) k;
value = (T2) v;
}
public T1 getKey() {
return key;
}
public T2 getValue() {
return value;
}
}
Mapper.java
List<Pair<String, Long>> getSourcesById(@Param("id")String id, @Param("hitDate")Date hitDate);
Page.jsp
<c:forEach items="${sources}" var="s">${s.value},</c:forEach>
你应该这样写,
@Select("select sources.host as 'key', count(*) total ... group by host")
@MapKey("key")
Map<String, Map<String, Object>> getSourcesById(@Param("id")String id, @Param("hitDate")Date hitDate);
这是选项 1 的方式。
我试着关注 Return HashMap in mybatis and use it as ModelAttribute in spring MVC (Option 1) and Mybatis ResultMap is HashMap<String,Object>。我有
@Select("select sources.host as 'key', count(*) total ... group by host")
@MapKey("key")
Map<String, Integer> getSourcesById(@Param("id")String id, @Param("hitDate")Date hitDate);
它returns一个错误
Error attempting to get column 'key' from result set. Cause: java.sql.SQLException: Invalid value for getInt() - 'NONE' ; SQL []; Invalid value for getInt() - 'NONE'; nested exception is java.sql.SQLException: Invalid value for getInt() - 'NONE'
查询在 MySQL 和 returns
中工作正常| key | total |
+-------------+-------+
| NONE | 33 |
| twitter.com | 1 |
好像没有使用@MapKey
注解
我试过了
List<AbstractMap.SimpleEntry<String, Integer>> getSourcesById(...)
但它给了
nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in java.util.AbstractMap$SimpleEntry matching [java.lang.String, java.lang.Long]
也试过同样的错误。
List<AbstractMap.SimpleEntry<String, Long>>
https://docs.oracle.com/javase/7/docs/api/java/util/AbstractMap.SimpleEntry.html#constructor_summary
MyBatis 3.4.6,MyBatis-Spring1.3.2
我必须自己构建 class,但我不喜欢它。愚蠢的是,我必须添加这么多代码才能从数据库中获取简单的 key/value 对。
Pair.java
public class Pair<T1, T2> {
public T1 key;
public T2 value;
public Pair(T1 k, T2 v) {
key = k;
value = v;
}
public Pair(String k, Long v) {
key = (T1) k;
value = (T2) v;
}
public T1 getKey() {
return key;
}
public T2 getValue() {
return value;
}
}
Mapper.java
List<Pair<String, Long>> getSourcesById(@Param("id")String id, @Param("hitDate")Date hitDate);
Page.jsp
<c:forEach items="${sources}" var="s">${s.value},</c:forEach>
你应该这样写,
@Select("select sources.host as 'key', count(*) total ... group by host")
@MapKey("key")
Map<String, Map<String, Object>> getSourcesById(@Param("id")String id, @Param("hitDate")Date hitDate);
这是选项 1 的方式。