【Mybatis】3—Mybatis映射关联关系

⭐⭐⭐⭐⭐⭐
Github主页https://github.com/A-BigTree
笔记链接https://github.com/A-BigTree/Code_Learning
⭐⭐⭐⭐⭐⭐

如果可以,麻烦各位看官顺手点个star~

如果文章对你有所帮助,可以点赞收藏⭐支持一下博主~


文章目录

  • 4 使用Mybatis映射关联关系
    • 4.1 概念
      • 4.1.1 关联关系概念
        • 数量关系
        • 关联方向
      • 4.1.2 创建模型
        • 创建实体类
        • 数据库测试数据
    • 4.2 对一关系
      • 4.2.1 OrderMapper配置文件
      • 4.2.2 关键词
    • 4.3 对多关系
      • 4.3.1 CustomerMapper配置文件
      • 4.3.2 关键词
    • 4.4 分布查询
      • 4.4.1 概念和需求
      • 4.4.2 具体操作
        • 编写查询Cutomer的SQL语句
        • 编写Order的SQL语句
        • 引用SQL语句
        • 各要素之间的关系
    • 4.5 延迟加载
      • 4.5.1 概念
      • 4.5.2 配置
        • 较低版本
        • 较高版本
      • 4.5.3 关键词总结

4 使用Mybatis映射关联关系

4.1 概念

4.1.1 关联关系概念

数量关系

主要体现在数据库表中

  • 一对一

    夫妻关系,人和身份证号

  • 一对多

    用户和用户的订单,锁和钥匙

  • 多对多

    老师和学生,部门和员工

关联方向

主要体现在Java实体类中

  • 双向:双方都可以访问到对方
    • Customer:包含Order的集合属性
    • Order:包含单个Customer的属性
  • 单向:双方中只有一方能够访问到对方
    • Customer:不包含Order的集合属性,访问不到Order
    • Order:包含单个Customer的属性

4.1.2 创建模型

创建实体类

public class Customer {

  private Integer customerId;

  private String customerName;

  private List<Order> orderList;// 体现的是对多的关系
}
public class Order {

  private Integer orderId;

  private String orderName;

  private Customer customer;// 体现的是对一的关系   
}

数据库测试数据

CREATE TABLE `t_customer` (`customer_id` INT NOT NULL AUTO_INCREMENT, `customer_name` CHAR(100), PRIMARY KEY (`customer_id`) );

CREATE TABLE `t_order` ( `order_id` INT NOT NULL AUTO_INCREMENT, `order_name` CHAR(100), `customer_id` INT, PRIMARY KEY (`order_id`) ); 

INSERT INTO `t_customer` (`customer_name`) VALUES ('c01');

INSERT INTO `t_order` (`order_name`, `customer_id`) VALUES ('o1', '1');
INSERT INTO `t_order` (`order_name`, `customer_id`) VALUES ('o2', '1');
INSERT INTO `t_order` (`order_name`, `customer_id`) VALUES ('o3', '1'); 

实际开发时,一般在开发过程中,不给数据库表设置外键约束。 原因是避免调试不方便。 一般是功能开发完成,再加外键约束检查是否有bug。

4.2 对一关系

4.2.1 OrderMapper配置文件




<resultMap id="selectOrderWithCustomerResultMap" type="com.atguigu.mybatis.entity.Order">

  
  <id column="order_id" property="orderId"/>

  <result column="order_name" property="orderName"/>

  
  
  
  <association property="customer" javaType="com.atguigu.mybatis.entity.Customer">

    
    <id column="customer_id" property="customerId"/>
    <result column="customer_name" property="customerName"/>

  association>

resultMap>


<select id="selectOrderWithCustomer" resultMap="selectOrderWithCustomerResultMap">

  SELECT order_id,order_name,c.customer_id,customer_name
  FROM t_order o
  LEFT JOIN t_customer c
  ON o.customer_id=c.customer_id
  WHERE o.order_id=#{orderId}

select>

对应关系参考下图:

【Mybatis】3—Mybatis映射关联关系_第1张图片

4.2.2 关键词

在“对一”关联关系中,我们的配置比较多,但是关键词就只有:associationjavaType

4.3 对多关系

4.3.1 CustomerMapper配置文件


<resultMap id="selectCustomerWithOrderListResultMap"

  type="com.atguigu.mybatis.entity.Customer">

  
  <id column="customer_id" property="customerId"/>

  <result column="customer_name" property="customerName"/>

  
  
  
  <collection property="orderList" ofType="com.atguigu.mybatis.entity.Order">

    
    <id column="order_id" property="orderId"/>

    <result column="order_name" property="orderName"/>

  collection>

resultMap>


<select id="selectCustomerWithOrderList" resultMap="selectCustomerWithOrderListResultMap">

  SELECT c.customer_id,c.customer_name,o.order_id,o.order_name
  FROM t_customer c
  LEFT JOIN t_order o
  ON c.customer_id=o.customer_id
  WHERE c.customer_id=#{customerId}

select>

对应关系可以参考下图:

【Mybatis】3—Mybatis映射关联关系_第2张图片

4.3.2 关键词

在“对多”关联关系中,同样有很多配置,但是提炼出来最关键的就是:collectionofType

4.4 分布查询

4.4.1 概念和需求

为了实现延迟加载,对CustomerOrder的查询必须分开,分成两步来做,才能够实现。为此,我们需要单独查询Order,也就是需要在Mapper配置文件中,单独编写查询Order集合数据的SQL语句。

4.4.2 具体操作

编写查询Cutomer的SQL语句


<select id="selectCustomerWithOrderList" resultMap="selectCustomerWithOrderListResultMap">

  select customer_id,customer_name from t_customer
  where customer_id=#{customerId}

select>

编写Order的SQL语句

<select id="selectOrderList" resultType="com.atguigu.mybatis.entity.Order">

  select order_id,order_name from t_order where customer_id=#{customer_id}

select>

引用SQL语句


<resultMap id="selectCustomerWithOrderListResultMap"

  type="com.atguigu.mybatis.entity.Customer">

  
  <id column="customer_id" property="customerId"/>
  <result column="customer_name" property="customerName"/>

  
  
  
  
  <collection
    property="orderList"
    select="com.atguigu.mybatis.mapper.CustomerMapper.selectOrderList"
    column="customer_id"/>

resultMap>

各要素之间的关系

【Mybatis】3—Mybatis映射关联关系_第3张图片

4.5 延迟加载

4.5.1 概念

查询到Customer的时候,不一定会使用Order的List集合数据。如果Order的集合数据始终没有使用,那么这部分数据占用的内存就浪费了。对此,我们希望不一定会被用到的数据,能够在需要使用的时候再去查询。

例如:对Customer进行1000次查询中,其中只有15次会用到Order的集合数据,那么就在需要使用时才去查询能够大幅度节约内存空间。

延迟加载的概念:对于实体类关联的属性到需要使用时才查询。也叫懒加载。

4.5.2 配置

较低版本


<settings>

  
  
  <setting name="lazyLoadingEnabled" value="true"/>

  
  <setting name="aggressiveLazyLoading" value="false"/>

settings>

较高版本


<settings>

  
  <setting name="lazyLoadingEnabled" value="true"/>

settings>

效果:刚开始先查询Customer本身,需要用到OrderList的时候才发送SQL语句去查询

4.5.3 关键词总结

我们是在“对多”关系中举例说明延迟加载的,在“对一”中配置方式基本一样。

关联关系 配置项关键词 所在配置文件和具体位置
对一 association标签/javaType属性 Mapper配置文件中的resultMap标签内
对多 collection标签/ofType属性 Mapper配置文件中的resultMap标签内
对一分步 association标签/select属性/column属性 Mapper配置文件中的resultMap标签内
对多分步 collection标签/select属性/column属性 Mapper配置文件中的resultMap标签内
延迟加载3.4.1版本前 lazyLoadingEnabled设置为true
aggressiveLazyLoading设置为false
Mybatis全局配置文件中的settings标签内
延迟加载3.4.1版本后 lazyLoadingEnabled设置为true Mybatis全局配置文件中的settings标签内

你可能感兴趣的:(微服务,#Mybatis,mybatis,java,开发语言)