Mybatis 查询一个对象包含多个子对象 (List 包含 List)

功能:查询一个数据列表 且每个数据中包含各自的子数据集合

使用场景:1. 当需要查询多订单数据且同时订单数据中需要包含订单明细数据时

                 2. 当需要查询多评论数据且同时评论数据中需要包含评论回复数据时

功能效果概述图:(List 中包含 List)

Mybatis 查询一个对象包含多个子对象 (List 包含 List)_第1张图片

1. Dao 层定义

package com.ljw.dao;

import java.util.List;
import com.ljw.vo.Order;

public interface OrderMapper {
    List findAllOrder();
}

2. Mybatis 配置



  
  
  
  
    
    
    
    
  



3. po 实体类定义

/**
 * 订单表
 */
public class Order implements Serializable {
    /**
     * 订单ID
     */
    private Integer orderId;
    /**
     * 订单编号
     */
    private String orderNumber;
    /**
     * 订单时间
     */
    private Date orderTime;
    /**
     * 订单明细集合
     */
    private List 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 getOrderDetails() {
        return orderDetails;
    }
    public void setOrderDetails(List orderDetails) {
        this.orderDetails = orderDetails;
    }
}
/**
 * 订单明细表
 */
public class OrderDetail implements Serializable {
    /**
     * 订单明细ID
     */
    private Integer orderDetailId;
    /**
     * 订单ID
     */
    private Integer orderId;
    /**
     * 商品名称
     */
    private String commodityName;
    /**
     * 商品数量
     */
    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;
    }
}

 

你可能感兴趣的:(MySQL,SpringMVC,Java,MyBatis,MySQL)