Mybatis——一对一映射

一对一映射

预置条件

在某网络购物系统中,一个用户只能拥有一个购物车,用户与购物车的关系可以设计为一对一关系

数据库表结构(唯一外键关联)

创建两个实体类和映射接口
java 复制代码
package org.example.demo;

import lombok.Data;

import java.util.List;

@Data
public class User {
    private Integer userId;
    private String userName;
    private String password;
    private Cart cart;
    private List<Order> orderList;
}
java 复制代码
package org.example.demo;

import lombok.Data;

@Data
public class Cart {
    private Integer cartId;
    private double price;
}
实现根据用户id查询出所有用户信息,包括该用户的购物车信息
java 复制代码
package org.example.mapper;

import org.example.demo.User;

import java.util.List;

public interface UserMapper {

    User findUserAndCartByUserId(Integer userId);
}
MyBatis中处理一对一关联关系的方法有四种
使用自动映射处理一对一关系

使用自动映射处理一对一关系,也就是通过别名自动将值匹配到对应的字段上

html 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.UserMapper">
    <select id="findUserAndCartByUserId" resultType="com.mybatis.entity.User">
        select
            *
        from t_user u
        inner join t_cart c on u.user_id = c.user_id
        where u.user_id = #{userId};
    </select>
</mapper>

复杂的属性映射时,可以多层嵌套,比如将cart表中的cart_id字段映射到Cart.id属性上

使用resultMap配置一对一映射

使用这种方式同自动映射方式相似之处为,Cart中的属性配置部分使用了"Cart."前缀

html 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.UserMapper"> 
    <resultMap id="userMap" type="org.example.demo.User">
        <id property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="password" column="password"/>
        <result property="cart.cartId" column="cart_id"/>
        <result property="cart.price" column="price"/>
    </resultMap>   
    <select id="findUserAndCartByUserId" resultMap="userMap">
        select
            *
        from t_user u
        inner join t_cart c on u.user_id = c.user_id
        where u.user_id = #{userId};
    </select>
</mapper>
使用association元素配置一对一映射

association元素用于和一个复杂的类型进行关联

association元素包含以下属性

property:对应实体类中的属性名,必填项

javaType:属性对应的java类型,可选项

resultMap:可以直接使用现有的resultMap,而不需要在这里配置,可选项

html 复制代码
    <resultMap id="userMap" type="org.example.demo.User">
        <id property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="password" column="password"/>
        <association property="cart">
            <id property="cartId" column="cart_id"/>
            <result property="price" column="price"/>
        </association>
    </resultMap>
    <select id="selectUserById" resultMap="userMap">
        select * from t_user where user_id = #{userId};
    </select>
association元素的嵌套查询

association元素的嵌套查询常用的属性如下

select:另一个查询映射的statement id,MyBatis会额外执行这个查询获取嵌套对象

column:列名,将主查询中列的结果作为嵌套查询的参数,column="{prop1=col1,prop2=col2}",其中prop1和prop2将作为嵌套查询的参数

fetchType:数据加载方式,可选值为lazy和eager,分别为延迟加载和积极加载,会覆盖全局的lazyLoadingEnable配置

html 复制代码
    <resultMap id="userMap" type="org.example.demo.User">
        <id property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="password" column="password"/>
        <association property="cart" column="user_id" select="findCartbyUserId"
        fetchType="lazy"/>
    </resultMap>

    <resultMap id="cartMap" type="org.example.demo.Cart">
        <id property="cartId" column="cart_id"/>
        <result property="price" column="price"/>
    </resultMap>
    
    <select id="findUserAndCartByUserId" resultMap="userMap">
        select * from t_suer u where user_id = #{user_id};
    </select>
    <select id="findCartbyUserId" resultMap="cartMap">
        select * from t_cart c where c.user_id = #{user_id};
    </select>

嵌套查询会多执行SQL,当查询N条数据时,就会出现N+1次查询问题

为了解决N+1次查询问题,可以设置延迟加载策略,将association元素的fetchType属性设置为lazy

设置为延迟加载的对象,默认情况下,当调用该对象的equals、hashCode、toString、clone方法时,就会加载该对象的全部数据

对比四种方式

前面的三种方式都属于"关联的嵌套结果映射",即通过一次SQL查询根据表或指定的属性映射到不同的对象中

最后一种方式属于"关联的嵌套查询",利用简单的SQL语句,通过多次查询得到想要的结果,可实现延迟加载效果

相关推荐
小徐敲java4 小时前
通用mybatis-plus查询封装(QueryGenerator)
mybatis
OEC小胖胖5 小时前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web
计算机学姐6 小时前
基于SpringBoot+Vue的在线投票系统
java·vue.js·spring boot·后端·学习·intellij-idea·mybatis
落落落sss6 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
罗曼蒂克在消亡9 小时前
2.3MyBatis——插件机制
java·mybatis·源码学习
cyt涛11 小时前
MyBatis 学习总结
数据库·sql·学习·mysql·mybatis·jdbc·lombok
OLDERHARD15 小时前
Java - MyBatis(上)
java·oracle·mybatis
计算机学姐1 天前
基于SpringBoot+Vue的高校运动会管理系统
java·vue.js·spring boot·后端·mysql·intellij-idea·mybatis
我是浮夸1 天前
MyBatisPlus——学习笔记
java·spring boot·mybatis
编程、小哥哥1 天前
手写mybatis之Mapper XML的解析和注册使用
xml·java·mybatis