RowMapper返回列表,但执行返回值会将列表大小返回1吗?

时间:2014-11-03 18:24:52

标签: spring stored-procedures

  1. 请在下面找到我的示例代码。行映射器返回一个列表。打印时,它会给我DB中的大小,但是当我检查时
  2.   

    (List)employeeDaomap .get(" allEmployees")我的列表大小为1,整行为一项?为什么实施中的错误

    Spring doc也说不使用rs.next(),我们如何获取列表     数据库中的值

    public class MyTestDAO extends StoredProcedure {
    
    /** The log. */
    static Logger log = Logger.getLogger(MyTestDAO.class);
    private static final String SPROC_NAME = "TestSchema.PKG_Test.prc_get_employee_list";
    TestRowMapper mapper=new TestRowMapper();
    
    public MyTestDAO(DataSource dataSource){
        super(dataSource, SPROC_NAME);
        declareParameter(new SqlOutParameter("allEmployees", OracleTypes.CURSOR, mapper));
        compile();
    }
    
    /**
     * Gets the myemplist data from the DB
     * 
     */
    public  List<EmployeeDAO> getEmployeeList()
            throws Exception {
    
        Map<String,Object> employeeDaomap =new HashMap<String,Object>(); 
        employeeDaomap =execute();
        log.info("employeeDaomap after execute ="+employeeDaomap);
        log.info("employeeDaomap after execute size ="+employeeDaomap.size()); // expected 1
        List<EmployeeDAO> list = (List<EmployeeDAO>) employeeDaomap .get("allEmployees");
        log.info("size of the list ="+list.size()); // need to get the size of the list ,
        return list;
    }
    
    
    private Map<String, Object> execute() {
        return super.execute(new HashMap<String, Object>());
    }
    }
    
    public class TestRowMapper implements RowMapper<List<EmployeeDAO>> {
    static Logger log = Logger.getLogger(TestRowMapper.class);
    
    @Override
    public List<EmployeeDAO> mapRow(ResultSet rs, int rowNum)
            throws SQLException {
        // TODO Auto-generated method stub
        rs.setFetchSize(3000);
        List<EmployeeDAO> responseItems = new ArrayList<EmployeeDAO>();
        EmployeeDAO responseItem = null;
        log.info("row num "+rowNum);
        while (rs.next()) {
    
            responseItem = new EmployeeDAO();
            responseItem.setID(rs.getString("id"));
            responseItem.setName(rs.getString("name"));
            responseItem.setDesc(rs.getString("desc"));
            responseItems.add(responseItem);
    
        }
        log.info("TestRowMapper items ="+responseItems);
        return responseItems;
    }
    }
    

1 个答案:

答案 0 :(得分:0)


       解决方案是使用implements ResultSetExtractor而不是RowMapper,并为extractData提供实现。

 


   public class TestRowMapper implements ResultSetExtractor<List<EmployeeDAO>> {
        static Logger log = Logger.getLogger(TestRowMapper.class);

    @Override
    public List<EMAccountResponse> extractData(ResultSet rs)
			throws SQLException, DataAccessException {
	    rs.setFetchSize(3000);
        List<EmployeeDAO> responseItems = new ArrayList<EmployeeDAO>();
        EmployeeDAO responseItem = null;
        log.info("row num "+rowNum);
        while (rs.next()) {

            responseItem = new EmployeeDAO();
            responseItem.setID(rs.getString("id"));
            responseItem.setName(rs.getString("name"));
            responseItem.setDesc(rs.getString("desc"));
            responseItems.add(responseItem);

         }
        log.info("TestRowMapper items ="+responseItems);
        return responseItems;
	 }
}