功能:查询一个数据列表 且每个数据中包含各自的子数据集合
使用场景:1. 当需要查询多订单数据且同时订单数据中需要包含订单明细数据时
2. 当需要查询多评论数据且同时评论数据中需要包含评论回复数据时
功能效果概述图:
1. Dao 层定义
package com.ljw.dao;
import java.util.List;
import com.ljw.vo.Order;
public interface OrderMapper {
List<Order> findAllOrder();
}
2. Mybatis 配置
<!-- 实体类映射 -->
<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>
<!-- 查询代码 -->
<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 实体类定义
/**
* 订单表
*/
public class Order implements Serializable {
/**
* 订单ID
*/
private Integer orderId;
/**
* 订单编号
*/
private String orderNumber;
/**
* 订单时间
*/
private Date orderTime;
/**
* 订单明细集合
*/
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;
}
}
/**
* 订单明细表
*/
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;
}
}
扩展:mybatis映射文件递归查
t
上诉sql中,mybtais 支持递归查询的方式,如下:
<collection property="menuList" select="findAllMenuInfoLevel" column="{parentId=id,status=status,userId=user_id}"></collection>
属性说明:
property属性: 对应的entity里面的某个属性,如下:
public class Menu{private String name;
private List<Menu> menuList;
}
select属性: 递归调用的查询语句。
column属性: 可携带调用 slelect属性指定的 查询语句,将参数携带 进行查询。