Java 存储过程的结果集包含 LinkedCaseInsensitiveMap<V> 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19368520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resultset of stored procedure contains list of LinkedCaseInsensitiveMap<V>
提问by Stefan Beike
I have this stored procedure call in Java:
我在 Java 中有这个存储过程调用:
@Autowired
public ScoreDao(DataSource dataSource) {
setDataSource(dataSource);
mScoreStoredProcedure = new ScoreStoredProcedure(dataSource);
}
public List<Score> loadAllScore(String pUsername, String pUUID, int pLimit) {
return mScoreStoredProcedure.execute(pUsername, pUUID, pLimit);
}
private class ScoreStoredProcedure extends StoredProcedure {
private static final String SPROC_NAME = "loadUserScore";
public ScoreStoredProcedure(DataSource datasource) {
super(datasource, SPROC_NAME);
declareParameter(new SqlReturnResultSet("#result-set-2", mScoreMapper));
declareParameter(new SqlParameter("vusername", Types.VARCHAR));
declareParameter(new SqlParameter("vuuid", Types.VARCHAR));
declareParameter(new SqlParameter("vlimit", Types.INTEGER));
compile();
}
@SuppressWarnings("unchecked")
public List<Score> execute(String pUsername, String pUUID, int pLimit){
Map<String,Object> lAllScoreResult = super.execute(pUsername, pUUID, pLimit);
List<Score> lAllScore = ((List<Score>)lAllScoreResult.get("#result-set-2"));
return lAllScore;
}
}
and this mapper class:
和这个映射器类:
public class ScoreMapper implements RowMapper<Score> {
private String suffix = "";
@Autowired
private ScoreCreator scoreCreator;
@Autowired
private QuestionMapper questionMapper;
public ScoreMapper(String pSuffix) {
suffix = pSuffix;
}
public Score mapRow(ResultSet rs, int rowNum) throws SQLException {
Score lScore = scoreCreator.createScore(rs.getLong(suffix+"id"),
rs.getTimestamp(suffix+"stempel"), rs.getString(suffix+"username"),
rs.getInt(suffix+"points"), rs.getInt(suffix+"level"),
rs.getString(suffix+"comment"),
questionMapper.mapRow(rs, rowNum), rs.getString(suffix+"uuid"));
return lScore;
}
}
The problem I have is that the result of my StoredProcedure
is never cast to List<Score>
.
我遇到的问题是 my 的结果StoredProcedure
永远不会转换为List<Score>
.
Instead it contains a List of LinkedCaseInsensitiveMap<V>
. Each entry covers a value from the database.
相反,它包含一个列表LinkedCaseInsensitiveMap<V>
。每个条目都包含数据库中的一个值。
The mapper is correctly wired via Spring.
映射器通过 Spring 正确连接。
Briefly: I expect as result a List<Score>
. I thought I handle this with the methods shown in the code. How can I cast the result directly to my List<Score>
?
简而言之:我希望结果 a List<Score>
. 我以为我用代码中显示的方法来处理这个问题。如何将结果直接投射到我的List<Score>
?
I followed this tutorial http://www.jtmelton.com/2007/10/30/using-springs-storedprocedure-and-rowmapper-mechanisms/
我跟着这个教程http://www.jtmelton.com/2007/10/30/using-springs-storedprocedure-and-rowmapper-mechanisms/
Could you help me find the problem?
你能帮我找出问题吗?
采纳答案by flup
Can you rename the result set parameter to, say, "ScoreResultSet"?
您能否将结果集参数重命名为“ScoreResultSet”?
So
所以
declareParameter(new SqlReturnResultSet("ScoreResultSet", mScoreMapper));
and
和
List<Score> lAllScore = ((List<Score>)lAllScoreResult.get("ScoreResultSet"));
The name you are currently using also gets used by spring when it auto generates names for result sets.
您当前使用的名称也会在 spring 自动为结果集生成名称时使用。
I think you may therefore now be returning a different, unprocessed, result set than the mapped one from the SqlReturnResultSet parameter you declared.
因此,我认为您现在返回的结果集可能不同于您声明的 SqlReturnResultSet 参数的映射结果集。
======
======
EDIT:
编辑:
I just found this other question of yours: Java StoredProcedure with SqlReturnResultSet not workingand assuming that we are talking about the same stored procedure:
我刚刚发现了你的另一个问题:Java StoredProcedure with SqlReturnResultSet 不工作并假设我们正在谈论相同的存储过程:
I suspect you have two selects in the stored procedure and it returns two result sets. You should declare two (properly named) SqlReturnResultSet parameters instead of one. The order of the declaration matters, so you cannot declare just one and give it the name usually assigned to the second one.
我怀疑您在存储过程中有两个选择,它返回两个结果集。您应该声明两个(正确命名的)SqlReturnResultSet 参数而不是一个。声明的顺序很重要,因此您不能只声明一个并为其指定通常分配给第二个的名称。
回答by aglassman
Try casting to LinkedList
rather than List<Score>
.
尝试强制转换为LinkedList
而不是List<Score>
。
The tutorial shows as follows, and is the only thing I can think of that you have done differently from the tutorial:
本教程如下所示,这是我唯一能想到的您所做的与本教程不同的事情:
List resultList = (LinkedList)results.get(Constants.RESULTSET);
//iterate of results list and print
for (Iterator it=resultList.iterator(); it.hasNext(); ) {
User user1 = (User)it.next();
System.out.println(user1);
}
return resultList;