ParameterizedRowMapper That Maps Object List to Object

前端 未结 1 1142
盖世英雄少女心
盖世英雄少女心 2020-12-19 20:56

I am trying to set the Parent List in a ParameterizedRowMapper how is this written or approached. I have two Objects one for parent and one for children however children con

相关标签:
1条回答
  • 2020-12-19 21:25

    I will show how to do this for a canonical 1-to-many example, you can adapt it to your vo class / table.

    Order class

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

    Item class

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

    Use two rowmappers one for each class and then use a result set extractor to convert multiple rows into one order + line items

    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;
                    }
    
                });
    }
    
    0 讨论(0)
提交回复
热议问题