gpt4 book ai didi

java - 将对象列表映射到对象的 ParameterizedRowMapper

转载 作者:行者123 更新时间:2023-11-30 11:34:38 25 4
gpt4 key购买 nike

我正在尝试在 ParameterizedRowMapper 中设置父列表,这是如何编写或处理的。我有两个对象,一个用于父对象,一个用于子对象,但是子对象包含一个 ListThe 每个子对象的父对象都存储在数据库中的一个单独表中,并且映射为 1 - 许多。

parent 记录的选择将在单独的结果集中完成。映射是否必须单独完成(单独的 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,但是我对它的使用有疑问。我是否在设置父级时将它集成到类(class)中?有人可以指导我如何以正确的方式完成吗

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
}

最佳答案

我将展示如何针对规范的一对多示例执行此操作,您可以将其调整为您的 vo 类/表。

订单类

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

项目类

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

使用两个 rowmapper 每个类一个,然后使用结果集提取器将多行转换为一个订单 + 订单项

订单库

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

});
}

关于java - 将对象列表映射到对象的 ParameterizedRowMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15555163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com