mybatis之关联关系
一对多
-
准备好数据库,将数据导入库中
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
-
通过mybatis-generator插件生成dao、mapper、model
代码
实体类
OrderVo.java
package com.xzy.model.vo;
import com.xzy.model.Order;
import com.xzy.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-09-26 11:09
*/
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.java
package com.xzy.model.vo;
import com.xzy.model.Order;
import com.xzy.model.OrderItem;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-09-26 11:10
*/
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.xzy.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.xzy.model.OrderItem">
<result property="orderItemId" column="order_item_id"></result>
<result property="oid" column="oid"></result>
<result property="productId" column="product_id"></result>
<result property="quantity" column="quantity"></result>
</collection>
</resultMap>
<select id="selectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid
and o.order_id = #{oid}
</select>
OrderMapper.java
OrderVo selectByOid(@Param(“oid”) Integer oid);
package com.xzy.mapper;
import com.xzy.model.