栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

多个一对多关系ResultSetExtractor

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多个一对多关系ResultSetExtractor

根据您的问题,我假设您有三个表。客户,品牌,订单。如果要将“客户”的“品牌和订单”属性获取到客户对象(“品牌”和“订单”之间没有关系),我建议使用UNIOn查询。像这样:

TBL_CUSTOMER------------CUSTOMER_IDCUSTOMER_ACCOUNT_NOCUSTOMER_NAMETBL_CUSTOMER_BRANDS-------------------CUSTOMER_BRAND_ID - UKBRAND_NAMECUSTOMER_ID       - FKTBL_ORDERS-------------------ORDER_ID          - UKCUSTOMER_ID       - FK

查询:

SELECt CUS.*, BRANDS.CUSTOMER_BRAND_ID COL_A, BRANDS.BRAND_NAME COL_B, 1 IS_BRAND FROM TBL_CUSTOMER CUS JOIN TBL_CUSTOMER_BRANDS BRANDS ON (CUS.CUSTOMER_ID = BRANDS.CUSTOMER_ID)UNIOn ALLSELECt CUS.*, ORDERS.ORDER_ID, '', 0 IS_BRAND FROM TBL_CUSTOMER CUS JOIN TBL_ORDERS ORDERS ON (CUS.CUSTOMER_ID = ORDERS.CUSTOMER_ID)

您的ResultSetExtractor将变为:

private class MyObjectExtractor implements ResultSetExtractor{    public Object extractData(ResultSet rs) throws SQLException, DataAccessException { Map<Long, Customer> map = new HashMap<Long, Customer>();        while (rs.next()) { Long id = rs.getLong("CUSTOMER_ID"); Customer customer = map.get(id); if(customer == null){     customer = new Customer();     customer.setId(id);     customer.setName(rs.getString("CUSTOMER_NAME"));     customer.setAccountNumber(rs.getLong("CUSTOMER_ACCOUNT_NO"));     map.put(id, customer);         } int type = rs.getInt("IS_BRAND"); if(type == 1) {     List brandList = customer.getBrands();     if(brandsList == null) {         brandsList = new ArrayList<Brand>();         customer.setBrands(brandsList);     }     Brand brand = new Brand();     brand.setId(rs.getLong("COL_A"));     brand.setName(rs.getString("COL_B"));     brandsList.add(brand); } else if(type == 0) {     List ordersList = customer.getOrders();     if(ordersList == null) {         ordersList = new ArrayList<Order>();         customer.setOrders(ordersList);     }     Order order = new Order();     order.setId(rs.getLong("COL_A"));     ordersList.add(order); }        }        return new ArrayList<Customer>(map.values());    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/494915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号