跨境互联网 跨境互联网
首页
  • AI 工具

    • 绘图提示词工具 (opens new window)
    • ChatGPT 指令 (opens new window)
  • ChatGPT

    • ChatGP T介绍
    • ChatGPT API 中文开发手册
    • ChatGPT 中文调教指南
    • ChatGPT 开源项目
  • Midjourney

    • Midjourney 文档
  • Stable Diffusion

    • Stable Diffusion 文档
  • 其他

    • AIGC 热门文章
    • 账号合租 (opens new window)
    • 有趣的网站
  • Vue

    • Vue3前置
  • JAVA基础

    • Stream
    • Git
    • Maven
    • 常用第三方类库
    • 性能调优工具
    • UML系统建模
    • 领域驱动设计
    • 敏捷开发
    • Java 测试
    • 代码规范及工具
    • Groovy 编程
  • 并发编程&多线程

    • 并发编程
    • 高性能队列 Disruptor
    • 多线程并发在电商系统下的应用
  • 其他

    • 面试题
  • 消息中间中间件

    • Kafka
    • RabbitMQ
    • RocketMQ
  • 任务调度

    • Quartz
    • XXL-Job
    • Elastic-Job
  • 源码解析

    • Mybatis 高级使用
    • Mybatis 源码剖析
    • Mybatis-Plus
    • Spring Data JPA
    • Spring 高级使用
    • Spring 源码剖析
    • SpringBoot 高级使用
    • SpringBoot 源码剖析
    • Jdk 解析
    • Tomcat 架构设计&源码剖析
    • Tomcat Web应用服务器
    • Zookeeper 高级
    • Netty
  • 微服务框架

    • 分布式原理
    • 分布式集群架构场景化解决方案
    • Dubbo 高级使用
    • Dubbo 核心源码剖析
    • Spring Cloud Gateway
    • Nacos 实战应用
    • Sentinel 实战应用
    • Seata 分布式事务
  • 数据结构和算法的深入应用
  • 存储

    • 图和Neo4j
    • MongoDB
    • TiDB
    • MySQL 优化
    • MySQL 平滑扩容实战
    • MySQL 海量数据存储与优化
    • Elasticsearch
  • 缓存

    • Redis
    • Aerospike
    • Guava Cache
    • Tair
  • 文件存储

    • 阿里云 OSS 云存储
    • FastDF 文件存储
  • 基础

    • Linux 使用
    • Nginx 使用与配置
    • OpenResty 使用
    • LVS+Keepalived 高可用部署
    • Jekins
  • 容器技术

    • Docker
    • K8S
    • K8S
  • 01.全链路(APM)
  • 02.电商终极搜索解决方案
  • 03.电商亿级数据库设计
  • 04.大屏实时计算
  • 05.分库分表的深入实战
  • 06.多维系统下单点登录
  • 07.多服务之间分布式事务
  • 08.业务幂等性技术架构体系
  • 09.高并发下的12306优化
  • 10.每秒100W请求的秒杀架构体系
  • 11.集中化日志管理平台的应用
  • 12.数据中台配置中心
  • 13.每天千万级订单的生成背后痛点及技术突破
  • 14.红包雨的架构设计及源码实现
  • 人工智能

    • Python 笔记
    • Python 工具库
    • 人工智能(AI) 笔记
    • 人工智能(AI) 项目笔记
  • 大数据

    • Flink流处理框架
  • 加密区

    • 机器学习(ML) (opens new window)
    • 深度学习(DL) (opens new window)
    • 自然语言处理(NLP) (opens new window)
AI 导航 (opens new window)

Revin

首页
  • AI 工具

    • 绘图提示词工具 (opens new window)
    • ChatGPT 指令 (opens new window)
  • ChatGPT

    • ChatGP T介绍
    • ChatGPT API 中文开发手册
    • ChatGPT 中文调教指南
    • ChatGPT 开源项目
  • Midjourney

    • Midjourney 文档
  • Stable Diffusion

    • Stable Diffusion 文档
  • 其他

    • AIGC 热门文章
    • 账号合租 (opens new window)
    • 有趣的网站
  • Vue

    • Vue3前置
  • JAVA基础

    • Stream
    • Git
    • Maven
    • 常用第三方类库
    • 性能调优工具
    • UML系统建模
    • 领域驱动设计
    • 敏捷开发
    • Java 测试
    • 代码规范及工具
    • Groovy 编程
  • 并发编程&多线程

    • 并发编程
    • 高性能队列 Disruptor
    • 多线程并发在电商系统下的应用
  • 其他

    • 面试题
  • 消息中间中间件

    • Kafka
    • RabbitMQ
    • RocketMQ
  • 任务调度

    • Quartz
    • XXL-Job
    • Elastic-Job
  • 源码解析

    • Mybatis 高级使用
    • Mybatis 源码剖析
    • Mybatis-Plus
    • Spring Data JPA
    • Spring 高级使用
    • Spring 源码剖析
    • SpringBoot 高级使用
    • SpringBoot 源码剖析
    • Jdk 解析
    • Tomcat 架构设计&源码剖析
    • Tomcat Web应用服务器
    • Zookeeper 高级
    • Netty
  • 微服务框架

    • 分布式原理
    • 分布式集群架构场景化解决方案
    • Dubbo 高级使用
    • Dubbo 核心源码剖析
    • Spring Cloud Gateway
    • Nacos 实战应用
    • Sentinel 实战应用
    • Seata 分布式事务
  • 数据结构和算法的深入应用
  • 存储

    • 图和Neo4j
    • MongoDB
    • TiDB
    • MySQL 优化
    • MySQL 平滑扩容实战
    • MySQL 海量数据存储与优化
    • Elasticsearch
  • 缓存

    • Redis
    • Aerospike
    • Guava Cache
    • Tair
  • 文件存储

    • 阿里云 OSS 云存储
    • FastDF 文件存储
  • 基础

    • Linux 使用
    • Nginx 使用与配置
    • OpenResty 使用
    • LVS+Keepalived 高可用部署
    • Jekins
  • 容器技术

    • Docker
    • K8S
    • K8S
  • 01.全链路(APM)
  • 02.电商终极搜索解决方案
  • 03.电商亿级数据库设计
  • 04.大屏实时计算
  • 05.分库分表的深入实战
  • 06.多维系统下单点登录
  • 07.多服务之间分布式事务
  • 08.业务幂等性技术架构体系
  • 09.高并发下的12306优化
  • 10.每秒100W请求的秒杀架构体系
  • 11.集中化日志管理平台的应用
  • 12.数据中台配置中心
  • 13.每天千万级订单的生成背后痛点及技术突破
  • 14.红包雨的架构设计及源码实现
  • 人工智能

    • Python 笔记
    • Python 工具库
    • 人工智能(AI) 笔记
    • 人工智能(AI) 项目笔记
  • 大数据

    • Flink流处理框架
  • 加密区

    • 机器学习(ML) (opens new window)
    • 深度学习(DL) (opens new window)
    • 自然语言处理(NLP) (opens new window)
AI 导航 (opens new window)
  • Spring Data JPA
  • MyBatis

    • Mybatis高级使用

      • 自定义持久层框架
      • Mybatis相关概念
      • Mybatis基本应用
      • Mybatis配置文件深入
      • Mybatis复杂映射开发
        • 5.1.1 一对一查询的模型
        • 5.1.2一对一查询的语句
        • 5.1.3 创建Order和User实体
        • 5.1.4 创建OrderMapper接口
        • 5.1.5 配置OrderMapper.xml
        • 5.1.6 测试结果
        • 5.2.1 一对多查询的模型
        • 5.2.2 一对多查询的语句
        • 5.2.3 修改User实体
        • 5.2.4 创建UserMapper接口
        • 5.2.5 配置UserMapper.xml
        • 5.2.6 测试结果
        • 5.3.1 多对多查询的模型
        • 5.3.2 多对多查询的语句
        • 5.3.3 创建Role实体,修改User实体
        • 5.3.4 添加UserMapper接口方法
        • 5.3.5 配置UserMapper.xml
        • 5.3.6 测试结果
      • Mybatis注解开发
      • Mybatis缓存
      • Mybatis插件
      • Mybatis架构原理
      • Mybatis源码剖析
      • 设计模式
    • Mybatis源码剖析

    • Mybatis-Plus
    • 资料
  • Spring

  • SpringBoot

  • Jdk

  • Tomcat

  • Netty

  • 若依

  • Traefik

  • Openresty

  • 开源框架
  • MyBatis
  • Mybatis高级使用
Revin
2023-07-23
目录

Mybatis复杂映射开发

# 5.1 一对一查询

# 5.1.1 一对一查询的模型

用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户

一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户

Mybatis_Page27_001

# 5.1.2一对一查询的语句

对应的sql语句:

select * from orders o,user u where o.uid=u.id;
1

查询的结果如下:

Mybatis_Page27_002

# 5.1.3 创建Order和User实体

public class Order {
   private int id;
   private Date ordertime;
   private double total;
   //代表当前订单从属于哪一个客户
   private User user;
}
public class User {
   
   private int id;
   private String username;
   private String password;
   private Date birthday;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 5.1.4 创建OrderMapper接口

public interface OrderMapper {
   List<Order> findAll();
}
1
2
3

# 5.1.5 配置OrderMapper.xml

<mapper namespace="com.lagou.mapper.OrderMapper"> 
  <resultMap id="orderMap" type="com.lagou.domain.Order"> 
    <result column="uid" property="user.id"/>  
    <result column="username" property="user.username"/>  
    <result column="password" property="user.password"/>  
    <result column="birthday" property="user.birthday"/> 
  </resultMap>  
  <select id="findAll" resultMap="orderMap">select * from orders o,user u where o.uid=u.id</select> 
</mapper>
1
2
3
4
5
6
7
8
9

其中还可以配置如下:

<resultMap id="orderMap" type="com.lagou.domain.Order">
   <result property="id" column="id"></result>
   <result property="ordertime" column="ordertime"></result>
   <result property="total" column="total"></result>
   <association property="user" javaType="com.lagou.domain.User">
       <result column="uid" property="id"></result>
       <result column="username" property="username"></result>
       <result column="password" property="password"></result>
       <result column="birthday" property="birthday"></result>
   </association>
</resultMap>
1
2
3
4
5
6
7
8
9
10
11

# 5.1.6 测试结果

OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
List<Order> all = mapper.findAll();
for(Order order : all){
   System.out.println(order);
}
1
2
3
4
5

Mybatis_Page29_001

# 5.2 一对多查询

# 5.2.1 一对多查询的模型

用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户

一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单

Mybatis_Page29_002

# 5.2.2 一对多查询的语句

对应的sql语句:

select *,o.id oid from user u left join orders o on u.id=o.uid;
1

查询的结果如下:

Mybatis_Page29_003

# 5.2.3 修改User实体

public class Order {
   private int id;
   private Date ordertime;
   private double total;
   //代表当前订单从属于哪一个客户
   private User user;
}


public class User {
   
   private int id;
   private String username;
   private String password;
   private Date birthday;
   //代表当前用户具备哪些订单
   private List<Order> orderList;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 5.2.4 创建UserMapper接口

public interface UserMapper {
   List<User> findAll();
}
1
2
3

# 5.2.5 配置UserMapper.xml

<mapper namespace="com.lagou.mapper.UserMapper">
   <resultMap id="userMap" type="com.lagou.domain.User">
       <result column="id" property="id"></result>
       <result column="username" property="username"></result>
       <result column="password" property="password"></result>
       <result column="birthday" property="birthday"></result>
       <collection property="orderList" ofType="com.lagou.domain.Order">
           <result column="oid" property="id"></result>
           <result column="ordertime" property="ordertime"></result>
           <result column="total" property="total"></result>
       </collection>
   </resultMap>
   <select id="findAll" resultMap="userMap">
     
select *,o.id oid from user u left join orders o on u.id=o.uid
   </select>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 5.2.6 测试结果

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
for(User user : all){
   System.out.println(user.getUsername());
   List<Order> orderList = user.getOrderList();
   for(Order order : orderList){
       System.out.println(order);
    }
   System.out.println("----------------------------------");
}
1
2
3
4
5
6
7
8
9
10

Mybatis_Page31_001

# 5.3 多对多查询

# 5.3.1 多对多查询的模型

用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用

多对多查询的需求:查询用户同时查询出该用户的所有角色

Mybatis_Page31_002

# 5.3.2 多对多查询的语句

对应的sql语句:

select u.,r.,r.id rid from user u left join user_role ur on u.id=ur.user_id inner join role r on ur.role_id=r.id;
1

查询的结果如下:

Mybatis_Page31_003

# 5.3.3 创建Role实体,修改User实体

public class User {
   private int id;
   private String username;
   private String password;
   private Date birthday;
   //代表当前用户具备哪些订单
   private List<Order> orderList;
   //代表当前用户具备哪些角色
   private List<Role> roleList;
}


public class Role {
   private int id;
   private String rolename;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 5.3.4 添加UserMapper接口方法

List<User> findAllUserAndRole();
1

# 5.3.5 配置UserMapper.xml

<resultMap id="userRoleMap" type="com.lagou.domain.User">
   <result column="id" property="id"></result>
   <result column="username" property="username"></result>
   <result column="password" property="password"></result>
   <result column="birthday" property="birthday"></result>
   <collection property="roleList" ofType="com.lagou.domain.Role">
       <result column="rid" property="id"></result>
       <result column="rolename" property="rolename"></result>
   </collection>
</resultMap>
<select id="findAllUserAndRole" resultMap="userRoleMap">
  select u.*,r.*,r.id rid from user u left join user_role ur on u.id=ur.user_id inner join role r on ur.role_id=r.id
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13

# 5.3.6 测试结果

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAllUserAndRole();
for(User user : all){
   System.out.println(user.getUsername());
   List<Role> roleList = user.getRoleList();
   for(Role role : roleList){
       System.out.println(role);
 
  }
   System.out.println("----------------------------------");
}
1
2
3
4
5
6
7
8
9
10
11

Mybatis_Page32_001

# 5.4 知识小结

MyBatis多表配置方式:

一对一配置:使用做配置

一对多配置:使用+做配置

多对多配置:使用+做配置

上次更新: 2025/04/03, 11:07:08
Mybatis配置文件深入
Mybatis注解开发

← Mybatis配置文件深入 Mybatis注解开发→

最近更新
01
tailwindcss
03-26
02
PaddleSpeech
02-18
03
whisper
02-18
更多文章>
Theme by Vdoing | Copyright © 2019-2025 跨境互联网 | 豫ICP备14016603号-5 | 豫公网安备41090002410995号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式