JdbcTemplate多个结果集 [英] JdbcTemplate multiple result sets
问题描述
我试图找到一种简单的方法来处理存储过程/SQL返回多个结果集.我一直在使用SimpleJdbcOperations#queryForList()
方法,但是这只会将第一个结果集返回为List<Map<String, Object>>
.我需要能够获得多个结果集,理想情况下是List<Map<String, Object>>
的Collection
之类的东西.我正在编写的程序是一个中间件组件,因此我不知道SQL是什么,也不知道结果集的形式.
I am trying to find an easy way to deal with Stored Procedures / SQL returning multiple result sets. I have been using the SimpleJdbcOperations#queryForList()
method however this will only return the first result set as a List<Map<String, Object>>
. I need to be able to get multiple result sets, ideally as a Collection
of List<Map<String, Object>>
or something. The program I am writing is a middleware component so I don't know what the SQL will be, or the form of the result set.
我认为我必须使用JdbcOperations
类,该类使我可以访问更多方法,包括execute(CallableStatementCreator csc, CallableStatementCallback<T> action)
,但现在我被困住了.
I think I have to use the JdbcOperations
class which gives me access to more methods, including execute(CallableStatementCreator csc, CallableStatementCallback<T> action)
but now I am stuck.
CallableStatementCallback<T> callback = new CallableStatementCallback<T>() {
@Override
public T doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException
{
boolean results = cs.execute(request);
while(results)
{
ResultSet result = cs.getResultSet();
results = cs.getMoreResults();
}
return null;
}
};
我不太确定如何使用该方法,或者不确定如何使用ResultSet
来获得通用的List<Map<String, Object>>
.
I am not really sure how to use the method though, or what to do with the ResultSet
to get my generic List<Map<String, Object>>
s.
推荐答案
我设法使用此代码获得了Set<ResultSet>
,
I managed to get a Set<ResultSet>
using this code,
private Set<ResultSet> executeProcedure(final String sql)
{
return jdbc.execute(new CallableStatementCreator() {
@Override
public CallableStatement createCallableStatement(Connection con) throws SQLException
{
return con.prepareCall(sql);
}
}, new CallableStatementCallback<Set<ResultSet>>() {
@Override
public Set<ResultSet> doInCallableStatement(CallableStatement cs) throws SQLException
{
Set<ResultSet> results = new HashSet<>();
boolean resultsAvailable = cs.execute();
while (resultsAvailable)
{
results.add(cs.getResultSet());
resultsAvailable = cs.getMoreResults();
}
return results;
}
});
}
只需要研究将ResultSet
转换为List<Map<String, Object>>
.
这篇关于JdbcTemplate多个结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!