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

使用场景: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 查询一个对象包含多个子对象 (List 包含 List)的更多相关文章

  1. mybatis 一对多的注入 指的是连表查询时候 将不同的查询结果以列表存储对象形式 注入进去 多对一指的是 查询多条结果但都是一样的 只需注入一条

    mybatis 一对多的注入 指的是连表查询时候 将不同的查询结果以列表存储对象形式 注入进去 多对一指的是 查询多条结果但都是一样的 只需注入一条

  2. 云笔记项目-MyBatis返回自增类型&堆栈对象补充理解

    在云笔记项目中,讲到了MySql的自增,MyBatis查询到自增类型数据后可以设置返回到参数属性,其中学习了MySql的自增写法,堆栈对象等知识. MySql数据类型自增 建立一张Person表,其中 ...

  3. MyBatis之ResultMap简介,关联对象

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...

  4. mybatis查询语句的背后之封装数据

    转载请注明出处... 一.前言 继上一篇mybatis查询语句的背后,这一篇主要围绕着mybatis查询的后期操作,即跟数据库交互的时候.由于本人也是一边学习源码一边记录,内容难免有错误或不足之处,还 ...

  5. spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象)

    spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象) 它的出现原因: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用 ...

  6. MySQL和mybatis查询相关

    0.mybatis的xml文件头 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapp ...

  7. MyBatis Generator 自动生成的POJO对象的使用(二)

    四.Example Class使用说明 示例类指定如何构建动态where子句. 表中的每个非BLOB列都可以选择包含在where子句中. 示例是演示此类用法的最佳方法. 示例类可用于生成几乎无限制的w ...

  8. MyBatis Generator 自动生成的POJO对象的使用(一)

    MyBatis Generator 会自动生成以下几种类型的对象(除非你使用MyBatis3DynamicSql 的运行环境): Java Model Objects(总是生成) SQL Map Fi ...

  9. django 过滤器-查询集-比较运算符-FQ对象-mysql的命令窗口

    """ 返回查询集的方法称为过滤器 all() 返回查询集中所有数据 filter() 返回符合条件的数据 一.filter(键=值) 二.filter(键=值,键=值) ...

随机推荐

  1. 【Codeforces】Gym 101608G WiFi Password 二分+线段树

    题意 给定$n$个数,求有最长的区间长度使得区间内数的按位或小于等于给定$v$ 二分区间长度,线段树处理出区间或,对每一位区间判断 时间复杂度$O(n\log n \log n)$ 代码 #inclu ...

  2. Python: scikit-image 彩色图像滤波

    一般的滤波器都是针对灰度图像的,scikit-image 库提供了针对彩色图像滤波的decorator:adapt_rgb,adapt_rgb 提供两种形式的滤波,一种是对rgb三个通道分别进行处理, ...

  3. 1131 Subway Map(30 分)

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  4. 【Lintcode】120.Word Ladder

    题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  5. 【LeetCode】017. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  6. BZOJ4016:[FJOI2014]最短路径树问题

    浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...

  7. POJ2182:Lost Cows

    浅谈线段树和树状数组:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:http://poj.org/problem?id=2182 线段树,倒着确 ...

  8. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  9. JAVA 编程思想二

    1: java  单根继承的优点? 方便垃圾回收: 垃圾回收的设计会方便实现.   多重继承的函数重名的问题. 2: 向下转型和向上转型?    向下转型不安全,向上转型安全. 3: system.g ...

  10. 如何分析一个QT类

    作者:gnuhpc  出处:http://www.cnblogs.com/gnuhpc/ 我们以QLineEdit这个类为例来看看如何学习分析一个QT类. 1.Public Types: 这是一个在这 ...