我在Spring Framework中创建MVC Web应用程序,我需要将Apache DBUtils结果集中的行转换为由嵌套对象组成的JavaBeans。
关于极少数示例,我发现我创建了这个RowProcessor实现。
public class MonthOrderCountHandler extends BasicRowProcessor {
@Override
public Object toBean(ResultSet rs, Class type) throws SQLException {
// Year
Year year = new Year();
year.setYearNo(rs.getInt("yearNo"));
year.setYear4(rs.getString("year4"));
year.setYear2(rs.getString("year2"));
// Quarter
Quarter quarter = new Quarter();
quarter.setQuarter(rs.getInt("quarter"));
// Month
Month m = new Month();
m.setYear(year);
m.setQuarter(quarter);
m.setMonthAbbreviation(rs.getString("monthAbbreviation"));
m.setMonthName(rs.getString("monthName"));
m.setMonthNo(rs.getInt("monthNo"));
// Final bean
MonthOrderCount result = new MonthOrderCount();
result.setMonth(m);
result.setOrderCount(rs.getInt("orderCount"));
return result;
}
}
问题:我想知道如何在我的DAO对象中使用此行处理器,以及此实现是否正确?
通常我会以这种方式将行转换为JavaBeans:
ResultSetHandler<List<MonthOrderCount>> listUrlHandler = new BeanListHandler<>(MonthOrderCount.class);
但在我的情况下,首先需要创建嵌套对象,然后创建一个最终的JavaBean,所以我假设我需要自定义行处理器。
我的域对象的结构是:
MonthOrderCount类:
public class MonthOrderCount {
private Month month;
private int orderCount;
}
月级课程:
public class Month {
private Quarter quarter;
private Year year;
private int monthNo;
private String monthName;
private String monthAbbreviation;
}
季度课程:
public class Quarter {
private int quarter;
private String abbreviation;
}
年级:
public class Year {
private int yearNo;
private String year2;
private String year4;
}
编辑:我在问,因为我的结果看起来像这样。 orderCount变量已正确填充,但在所有实例中月份为空。对我来说最奇怪的是 - 从不调用toBean()方法。
2013-03-10 17:09:46 INFO ChartDataService:29 - [MonthOrderCount {month = null,orderCount = 1863}, MonthOrderCount {month = null,orderCount = 2262}, MonthOrderCount {month = null,orderCount = 2531}, MonthOrderCount {month = null,orderCount = 2379}, MonthOrderCount {month = null,orderCount = 2106}, MonthOrderCount {month = null,orderCount = 1498}, MonthOrderCount {month = null,orderCount = 1300}, MonthOrderCount {month = null,orderCount = 1578}, MonthOrderCount {month = null,orderCount = 2385}, MonthOrderCount {month = null,orderCount = 2991}, MonthOrderCount {month = null,orderCount = 2219}, MonthOrderCount {month = null,orderCount = 1943}, MonthOrderCount {month = null,orderCount = 264}]
答案 0 :(得分:4)
如果要将结果集转换为JavaBeans列表,则需要覆盖toBeanList()而不是toBean()方法。
最终处理程序类覆盖了BasicRowProcessor,如下所示:
public class MonthOrderCountHandler extends BasicRowProcessor {
@Override
public List toBeanList(ResultSet rs, Class clazz) {
try {
List newlist = new LinkedList();
while (rs.next()) {
newlist.add(toBean(rs, clazz));
}
return newlist;
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
@Override
public Object toBean(ResultSet rs, Class type) throws SQLException {
// Year
Year year = new Year();
year.setYearNo(rs.getInt("yearNo"));
year.setYear4(rs.getString("year4"));
year.setYear2(rs.getString("year2"));
// Quarter
Quarter quarter = new Quarter();
quarter.setQuarterNo(rs.getInt("quarterNo"));
// Month
Month m = new Month();
m.setYear(year);
m.setQuarter(quarter);
m.setMonthAbbreviation(rs.getString("monthAbbreviation"));
m.setMonthName(rs.getString("monthName"));
m.setMonthNo(rs.getInt("monthNo"));
// Final bean
MonthOrderCount result = new MonthOrderCount();
result.setMonth(m);
result.setOrderCount(rs.getInt("orderCount"));
return result;
}
}
我希望它有所帮助。