做本地的分类信息网站,网站 白名单,处理营销型网站建设策划的几个误区,微商商城官网上文总结了一对一关联关系中的映射方法。那么#xff0c;如果存在一对多关系#xff0c;又该定义映射关系呢#xff1f;
1. 需求说明
假设目前存在顾客表与订单表#xff0c;一个顾客与多个订单对应。因此#xff0c;在顾客实体类中#xff0c;应该有一个订单类型的lis…上文总结了一对一关联关系中的映射方法。那么如果存在一对多关系又该定义映射关系呢
1. 需求说明
假设目前存在顾客表与订单表一个顾客与多个订单对应。因此在顾客实体类中应该有一个订单类型的list列表。如下所示
public class Customer {private Integer customerId;private String customerName;private ListOrder orderList;// 体现的是对多的关系
}public class Order {private Integer orderId;private String orderName;
}2. CustomerMapper接口
定义一个方法接口根据顾客id查询顾客信息及对应的订单信息。
public interface CustomerMapper {Customer selectCustomerWithOrderList(Integer customerId);}3. CustomerMapper.xml文件
!-- 配置resultMap实现从Customer到OrderList的“对多”关联关系 --
resultMap idselectCustomerWithOrderListResultMap typecustomer!-- 映射Customer本身的属性 --id columncustomer_id propertycustomerId/result columncustomer_name propertycustomerName/!-- collection标签映射“对多”的关联关系 --!-- property属性在Customer类中关联“多”的一端的属性名 --!-- ofType属性集合属性中元素的类型 --collection propertyorderList ofTypeorder!-- 映射Order的属性 --id columnorder_id propertyorderId/result columnorder_name propertyorderName//collection/resultMap!-- Customer selectCustomerWithOrderList(Integer customerId); --
select idselectCustomerWithOrderList resultMapselectCustomerWithOrderListResultMapSELECT c.customer_id,c.customer_name,o.order_id,o.order_nameFROM t_customer cLEFT JOIN t_order oON c.customer_ido.customer_idWHERE c.customer_id#{customerId}
/select总结
一对多映射时在resultMap中使用标签collection与一对一映射中association一样property属性值为被关联的实体类属性名相同。在上述例子中顾客类中订单类型的集合属性名为orderList。ofType与association中的javaType一样属性值为被关联的实体类的全类名。这里是list列表因此填列表的泛型类型也就是订单类。