目录:
1.一对多
2.多对多
1.一对多
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
<table schema="" tableName="t_hibernate_order" domainObjectName="Order"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
当我们生成后,就可以看到实体类了
OrderVo:
package com.ly.model.vo;
import com.ly.model.Order;
import com.ly.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @author李烨
* @site www.yilufa.com
* @company xxx公司
* @create 2019-09-26 11:37
*/
public class OrderVo extends Order {
private List<OrderItem> orderItems = new ArrayList<>();
public List<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}
}
OrderItemVo(这里是我们的多对一)
package com.ly.model.vo;
import com.ly.model.Order;
import com.ly.model.OrderItem;
/**
* @author李烨
* @site www.yilufa.com
* @company xxx公司
* @create 2019-09-26 11:40
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
接下来就是配置映射文件:
OrderMapper.xml:
<resultMap id="OrderVoMap" type="com.ly.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.ly.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>