JdbcTemplate多个结果集

我试图找到一个简单的方法来处理返回多个结果集的存储过程/ SQL。 我一直在使用SimpleJdbcOperations#queryForList()方法,但是这只会返回第一个结果集为List<Map<String, Object>> 。 我需要能够获得多个结果集,理想情况下是一个List<Map<String, Object>>或某物的Collection 。 我正在编写的程序是一个中间件组件,所以我不知道SQL会是什么,或者结果集的形式。

我想我必须使用JdbcOperations类,它允许我访问更多的方法,包括execute(CallableStatementCreator csc, CallableStatementCallback<T> action)但现在我卡住了。

    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>>


我设法使用这个代码得到一个Set<ResultSet>

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>>


您可以使用resultSet.getMetaData()方法来计算数据中的列:

ResultSetMetaData meta = resultSet.getMetaData();
int colcount = meta.getColumnCount();
for (int i = 1; i <= colcount; i++) 
{
    String name = meta.getColumnLabel(i); // This is the name of the column
    int type = meta.getColumnType(i);     // from java.sql.Types
   // Maybe add to a Map,List, etc...
}

然后您可以像其他评论者所说的那样执行循环,通过ResultSet抽出您需要的数据:

while (resultSet.hasNext())
{
     resultSet.next();
     // Find the columns you want to extract (via the above method maybe) and add to your row.
}

我使用下面的方法以List<Map<String, Object>>形式获取ResultSet的List<Map<String, Object>>

public List<List<Map<String, Object>>> executeProcedure(final String sql) {
        return jdbcTemplate.execute(new CallableStatementCreator() {
            @Override
            public CallableStatement createCallableStatement(Connection con) throws SQLException {
                return con.prepareCall(sql);
            }
        }, new CallableStatementCallback<List<List<Map<String, Object>>>>() {
            @Override
            public List<List<Map<String, Object>>> doInCallableStatement(CallableStatement cs) throws SQLException {
                boolean resultsAvailable = cs.execute();
                List<List<Map<String, Object>>> list = new ArrayList<List<Map<String, Object>>>();
                while (resultsAvailable) {
                    ResultSet resultSet = cs.getResultSet();
                    List<Map<String, Object>> subList = new ArrayList<Map<String, Object>>();
                    while (resultSet.next()) {
                        ResultSetMetaData meta = resultSet.getMetaData();
                        int colcount = meta.getColumnCount();
                        Map<String, Object> map = new HashMap<String, Object>();
                        for (int i = 1; i <= colcount; i++) {
                            String name = meta.getColumnLabel(i);
                            map.put(name, resultSet.getString(i));
                        }
                        subList.add(map);
                    }
                    list.add(subList);
                    resultsAvailable = cs.getMoreResults();
                }
                return list;
            }
        });
    }
链接地址: http://www.djcxy.com/p/12909.html

上一篇: JdbcTemplate multiple result sets

下一篇: Code Contracts warn of "ensures unproven" when locks are involved