1、一对多,以订单与订单项为例:
1-1:将数据导入数据库
1-2:通过mybatis-generator插件生成dao、mapper、model
1-3:实体类建立关联关系:
1-3-1、一对多,订单的实体类:一个订单对应多个订单项
private Set<OrderItem> orderItems = new HashSet<>();
1-3-2、多对一,订单项的实体类:多个订单项对应一个订单
private Order order;
1-4:配置mybatis关联映射:
1-4-1:一对多:订单中的Mapper.xml配置
<collection property= oftype=“类的全路径名”>
<!--配置,与数据库保持一致-->
<!--订单配置-->
<resultMap id="orderMap" type="com.zking.ssm.model.Order">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<!--一对多 订单项配置-->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="orderItems" ofType="com.zking.ssm.model.OrderItem">
<result property="orderItemId" column="order_item_id"></result>
<result property="productId" column="product_id"></result>
<result property="quantity" column="quantity"></result>
<result property="oid" column="oid"></result>
</collection>
</resultMap>
1-4-2:多对一:订单项中的Mapper.xml配置: