Function: query a data list and each data contains its own sub data set
Usage scenario: 1. When multiple order data needs to be queried and order details need to be included in the order data at the same time
2. When you need to query multiple comment data and include comment reply data in the comment data at the same time
Function effect overview: (List included in List)
1. Dao layer definition
package com.ljw.dao; import java.util.List; import com.ljw.vo.Order; public interface OrderMapper { List<Order> findAllOrder(); }
2. Mybatis configuration
<!-- Entity class mapping --> <resultMap type="cn.ljw.vo.Order" id="OrderMap"> <id column="order_id" jdbcType="INTEGER" property="orderId" /> <result column="order_number" jdbcType="VARCHAR" property="orderNumber" /> <result column="order_time" jdbcType="TIMESTAMP" property="orderTime" /> <collection property="orderDetails" ofType="cn.ljw.vo.OrderDetail" javaType="java.util.List"> <id column="detail_order_detail_id" jdbcType="INTEGER" property="orderDetailId" /> <result column="detail_order_id" jdbcType="INTEGER" property="orderId" /> <result column="detail_commodity_name" jdbcType="VARCHAR" property="commodityName" /> <result column="detail_commodity_number" jdbcType="INTEGER" property="commodityNumber" /> </collection> </resultMap> <!-- Query code --> <select id="findAllOrder" resultMap="OrderMap"> SELECT order.order_id, order.order_number, order.order_time, order_detail.order_detail_id AS detail_order_detail_id, order_detail.order_id AS detail_order_id, order_detail.commodity_name AS detail_commodity_name, order_detail.commodity_number AS detail_commodity_number FROM order LEFT JOIN order_detail ON order.order_id = order_detail.order_id </select>
3. po entity class definition
/** * Order form */ public class Order implements Serializable { /** * Order ID */ private Integer orderId; /** * Order number */ private String orderNumber; /** * Order time */ private Date orderTime; /** * Order detail set */ private List<OrderDetail> orderDetails; public Integer getOrderId() { return orderId; } public void setOrderId(Integer orderId) { this.orderId = orderId; } public String getOrderNumber() { return orderNumber; } public void setOrderNumber(String orderNumber) { this.orderNumber = orderNumber; } public Date getOrderTime() { return orderTime; } public void setOrderTime(Date orderTime) { this.orderTime = orderTime; } public List<OrderDetail> getOrderDetails() { return orderDetails; } public void setOrderDetails(List<OrderDetail> orderDetails) { this.orderDetails = orderDetails; } }
/** * Order Details */ public class OrderDetail implements Serializable { /** * Order detail ID */ private Integer orderDetailId; /** * Order ID */ private Integer orderId; /** * Trade name */ private String commodityName; /** * Quantity of commodities */ private Integer commodityNumber; public Integer getOrderDetailId() { return orderDetailId; } public void setOrderDetailId(Integer orderDetailId) { this.orderDetailId = orderDetailId; } public Integer getOrderId() { return orderId; } public void setOrderId(Integer orderId) { this.orderId = orderId; } public String getCommodityName() { return commodityName; } public void setCommodityName(String commodityName) { this.commodityName = commodityName; } public Integer getCommodityNumber() { return commodityNumber; } public void setCommodityNumber(Integer commodityNumber) { this.commodityNumber = commodityNumber; } }