了ParameterizedRowMapper映射对象列表到对象(ParameterizedRowM

2019-07-22 10:51发布

我想设置在了ParameterizedRowMapper父列表这是怎么写的或接近。 我有两个对象之一父母和一个儿童但是儿童包含ListThe父母为每个孩子都存储在一个单独的数据库中的表和映射是1 - 许多。

为记录父母的选择将在一个单独的ResultSet来完成。 将映射必须单独( 分开了ParameterizedRowMapper)完成的,如果是的话我将如何有写了ParameterizedRowMapper这是主要问题了ParameterizedRowMapper是怎么写的,以适应一个列表项。

了ParameterizedRowMapper

public static class ChildrenMapper implements ParameterizedRowMapper<Children>{         

         public Children mapRow(ResultSet rs, int rowNum) throws SQLException {
                Children child = new Children();
                child.setFirstName(rs.getString("firstName"));
                child.setLastName(rs.getString("lastName"));
                    //a child can have many Parents or gaurdians
                child.setParent(List<Parent>);                  

                return child;
            }       
    }

根据我的研究,我发现,我需要使用ResultSetExtractor类 ,但是我已经在使用的一个问题。 我在设置家长的角度它融入班? 可有人指导我如何可以做到正确的方法

Children.java

Public class Children(){
int cid;
String firstName;
String lastName;
List<Parent>parents;
..

//getters/setters

}

Parent.java

Public class Parent(){
 int pid;
String firstName;
String lastName;
..

//setters/getters
}

Answer 1:

我将展示如何为一个规范的1对多的例子做到这一点,你可以使其适应你的VO类/表。

Order类

public class Order {
    private Long orderId;
    private String user;
    private List<LineItem> items;
    // Getter / setter omitted
}

Item类

public class LineItem {
    private Long lineItemId;
    private String product;
    private int quantity;
    // Getter / setter omitted
}

用两个rowmappers为每个类,然后使用一个结果集提取到多行转换成一个订单+线项目

OrderRepository

public final static RowMapper<Order> orderMapper = ParameterizedBeanPropertyRowMapper.newInstance(Order.class);
public final static RowMapper<LineItem> lineItemMapper = ParameterizedBeanPropertyRowMapper.newInstance(LineItem.class);

public Order findOrderWithItems(Long orderId) {
    return jdbcTemplate.query("select * from orders, line_item "
            + " where orders.order_id = line_item.order_id and orders.order_id = ?", 
            new ResultSetExtractor<Order>() {
        public Order extractData(ResultSet rs) throws SQLException, DataAccessException {
            Order order = null;
            int row = 0;
            while (rs.next()) {
                if (order == null) {
                    order = orderMapper.mapRow(rs, row);
                }
                order.addItem(lineItemMapper.mapRow(rs, row));
                row++;
            }
            return order;
        }

    }, orderId);
}

public List<Order> findAllOrderWithItmes() {
    return jdbcTemplate.query("select * from orders, line_item "
            + " where orders.order_id = line_item.order_id order by orders.order_id",
            new ResultSetExtractor<List<Order>>() {
                public List<Order> extractData(ResultSet rs) throws SQLException, DataAccessException {
                    List<Order> orders = new ArrayList<Order>();
                    Long orderId = null;
                    Order currentOrder = null;
                    int orderIdx = 0;
                    int itemIdx = 0;
                    while (rs.next()) {
                        // first row or when order changes
                        if (currentOrder == null || !orderId.equals(rs.getLong("order_id"))) {
                            orderId = rs.getLong("order_id");
                            currentOrder = orderMapper.mapRow(rs, orderIdx++);
                            itemIdx = 0;
                            orders.add(currentOrder);
                        }
                        currentOrder.addItem(lineItemMapper.mapRow(rs, itemIdx++));
                    }
                    return orders;
                }

            });
}


文章来源: ParameterizedRowMapper That Maps Object List to Object