第十三章MyBatis高级映射

news/2024/10/22 19:23:21/

多对一映射

创建数据表

  • student是主表
  • class_id关联class表的id

class表
在这里插入图片描述
student表
在这里插入图片描述

创建pojo

Class类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Class {private Long id;private String name;private List<Student> students;
}

Student类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {private Long id;private String name;private Long classId;private Class clazz;
}

映射方式

1. 级联映射

StudentMapper写入如下代码

Student selectByIdResult(Long id);

StudentMapper.xml写入如下代码

<sql id="studentSql">s.id,s.name,s.class_id,c.id as classId,c.name as className
</sql><resultMap id="studentResultResultMap" type="Student"><id property="id" column="id"/><result property="name" column="name"/><result property="classId" column="classId"/><result property="clazz.id" column="classId"/><result property="clazz.name" column="className"/>
</resultMap><select id="selectByIdResult" resultMap="studentResultResultMap" >select<include refid="studentSql"/>from student s left join class c on s.class_id = c.idwhere s.id=#{id}
</select>

2. association映射

StudentMapper写入如下代码

Student selectByIdAssociation(Long id);

StudentMapper.xml写入如下代码

<sql id="studentSql">s.id,s.name,s.class_id,c.id as classId,c.name as className
</sql>
<resultMap id="studentAssociationResultMap" type="Student"><id property="id" column="id"/><result property="name" column="name"/><result property="classId" column="class_id"/><association property="clazz" javaType="Class"><id property="id" column="classId"/><result property="name" column="className"/></association>
</resultMap>
<select id="selectByIdAssociation" resultMap="studentAssociationResultMap">select <include refid="studentSql"/>from student s left join class c on s.class_id=c.idwhere s.id = #{id}
</select>

3. 分步查询

  • 上述两种方法都是一条sql完成
  • 分步查询需要俩条sql,可复用支持延迟加载

StudentMapper.xml写入如下代码

Student selectByIdStep(Long id);

ClassMapper写入如下代码

Class selectById(Long id);

StudentMapper.xml写入如下代码

<sql id="studentSql">id,name,class_id
</sql>
<resultMap id="studentStepResultMap" type="Student"><id property="id" column="id"/><result property="name" column="name"/><result property="classId" column="class_id"/><association property="clazz" select="org.example.mapper.ClassMapper.selectById" column="class_id"/>
</resultMap>
<select id="selectByIdStep" resultMap="studentStepResultMap">select <include refid="studentSql"/>from student where id = #{id}
</select>

ClassMapper.xml写入如下代码

<sql id="classSql">id,name
</sql>
<select id="selectById"  resultType="Class">select<include refid="classSql"/>from classwhere id=#{id}
</select>

上述俩种查询每次都要查询2条sql,有时我们不需要查询2条。并且联表查询会出现笛卡尔现象,性能很差

这可以是用延迟加载来提高性能,用到属性的时候查询不用就不会查询

局部设置 fetchType=“lazy” 。只会作用域这条sql

<resultMap id="studentStepResultMap" type="Student"><id property="id" column="id"/><result property="name" column="name"/><result property="classId" column="class_id"/><association property="clazz" fetchType="lazy" select="org.example.mapper.ClassMapper.selectById" column="class_id"/>
</resultMap>

全局开启延迟加载

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

局部关闭使用 fetchType=“eager”

    <resultMap id="studentStepResultMap" type="Student"><id property="id" column="id"/><result property="name" column="name"/><result property="classId" column="class_id"/><association property="clazz" fetchType="eager" select="org.example.mapper.ClassMapper.selectById" column="class_id"/></resultMap>

一对多映射

创建pojo

Class类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Class {private Long id;private String name;private List<Student> students;
}

Student类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {private Long id;private String name;private Long classId;private Class clazz;
}

映射方式

1. collection映射

ClassMapper写入如下代码

Class selectCollection(Long id);

ClassMapper.xml写入如下代码

<sql id="classSql">c.id,c.name,s.id as studentId,s.name as studentName,s.class_id as classId
</sql>
<resultMap id="selectCollectionResultMap" type="Class"><id property="id" column="id"/><result property="name" column="name"/><collection property="students" ofType="Student"><id property="id" column="studentId"/><result property="name" column="studentName"/><result property="classId" column="classId"/></collection>
</resultMap>
<select id="selectByIdCollection" resultMap="selectCollectionResultMap">select<include refid="classSql"/>from class c left join student s on c.id=s.class_idwhere c.id=#{id}
</select>

2. 分步查询

ClassMapper写入如下代码

Class selectByIdStep(Long id);

StudentMapper写入如下代码

Student selectById(Long classId);

ClassMapper.xml写入如下代码

<sql id="classSql">id,name
</sql>
<resultMap id="selectStepResultMap" type="Class"><id property="id" column="id"/><result property="name" column="name"/><collection property="students" select="org.example.mapper.StudentMapper.selectById" column="id"/>
</resultMap>
<select id="selectByIdStep" resultMap="selectStepResultMap">select<include refid="classSql"/>from class where id=#{id}
</select>

StudentMapper.xml写入如下代码

<sql id="studentSql">id,name,class_id
</sql>
<select id="selectById" resultType="Student">select<include refid="studentSql"/>from student where class_id = #{classId}
</select>

同理可以设置延迟加载


http://www.ppmy.cn/news/1047027.html

相关文章

数据流的中位数 -- 大小根堆组合使用

295. 数据流的中位数 Java 实现 import java.util.PriorityQueue;class MedianFinder {private PriorityQueue<Integer> large;private PriorityQueue<Integer> small;public MedianFinder() {// 小顶堆large new PriorityQueue<>();// 大顶堆small new …

【数据结构】二叉树的链式结构的实现 -- 详解

一、前置说明 在学习二叉树的基本操作前&#xff0c;需先要创建一棵二叉树&#xff0c;然后才能学习其相关的基本操作。为了降低大家学习成本&#xff0c;此处手动快速创建一棵简单的二叉树&#xff0c;快速进入二叉树操作学习。 typedef char BTDataType;typedef struct Binar…

浅析Java设计模式之四策略模式

title: 浅析Java设计模式之四策略模式 date: 2018-12-29 17:26:17 categories: 设计模式 description: 浅析Java设计模式之四策略模式 1. 目录 1. 目录2. 概念 2.1. 应用场景2.2. 优缺点 2.2.1. 优点2.2.2. 缺点 3. 模式结构4. 样例 4.1. 定义策略4.2. 定义具体策略4.3. 定义…

设计模式 -- 策略模式(传统面向对象与JavaScript 的对比实现)

设计模式 – 策略模式&#xff08;传统面向对象与JavaScript 的对比实现&#xff09; 文章目录 设计模式 -- 策略模式&#xff08;传统面向对象与JavaScript 的对比实现&#xff09;使用策略模式计算年终奖初级实现缺点 使用组合函数重构代码缺点 使用策略模式重构代码传统的面…

【React学习】—组件三大核心属性: state(七)

【React学习】—组件三大核心属性: state&#xff08;七&#xff09; 2.2.2. 理解 state是组件对象最重要的属性, 值是对象(可以包含多个key-value的组合)组件被称为"状态机", 通过更新组件的state来更新对应的页面显示(重新渲染组件) 2.2.3. 强烈注意 组件中rend…

nvm安装与学习

nvm安装与学习 介绍 在前端开发过程中&#xff0c;经常遇到 node.js 版本问题&#xff0c;不同项目要求的 node.js 版本不一样。而一台电脑上&#xff0c;只能安装与使用 node.js 的某一个具体版本&#xff0c;迫切需要一个工具能快速切换不同的node版本&#xff0c;nvm就是这…

MATLAB | 七夕节用MATLAB画个玫瑰花束叭

Hey又是一年七夕节要到了&#xff0c;每年一次直男审美MATLAB绘图大赛开始hiahiahia&#xff0c;真的这些代码越写越不知道咋写&#xff0c;又不想每年把之前的代码翻出来再发一遍&#xff0c;于是今年又对我之前写的老代码进行了点优化组合&#xff0c;整了个花球变花束&#…

Matplotlib数据可视化(三)

目录 1.绘图的填充 1.1 曲线下方区域的填充 1.2 填充部分区域 1.3 两条曲线之间的区域填充 1.4 直接使用fill进行填充 1.绘图的填充 绘图的填充可以调用fill_between()或fill()进行填充。 1.1 曲线下方区域的填充 x np.linspace(0,1,500) y np.sin(3*np.pi*x)*np.exp…