【MyBatis】高级映射多对一,一对多和延迟加载

简介: 【MyBatis】高级映射多对一,一对多和延迟加载

数据库准备:

1. 多对一:

多个学生对应一个班级(学生表是主表, 班级表是副表)

多种实现方式, 常见的包括三种

  1. 第一种方式: 一条sql语句, 级联属性映射
// StudentMapper.xml
// 一条sql语句, 级联属性映射
<resultMap id="studentResultMap" type="Student">
    <id property="sid" column="sid"/>
    <result property="sname" column="sname"/>
    <result property="clazz.cid" column="cid"/>
    <result property="clazz.cname" column="cname"/>
</resultMap>
<select id="selectById" resultMap="studentResultMap">
    select
        s.sid,s.sname,c.cid,c.cname
    from
        stu s left join clazz c on s.cid = c.cid
    where
        s.sid = #{sid}
</select>
// 接口
public interface StudentMapper{
    // 根据id获取学生信息, 同时获取学生关联的班级信息
    // 返回一个学生对象, 但是学生对象当中含有班级对象
    Student selectById(Integer id);
} 
// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    CarMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student student = mapper.selectById(6);
    System.out.println(student);
    sqlSession.close();
}
  1. 第二种方式: 一条sql语句, association标签
// StudentMapper.xml
// 一条sql语句, association(关联)
<resultMap id="studentResultMapAssociation" type="Student">
    <id property="sid" column="sid"/>
    <result property="sname" column="sname"/>
    // association翻译为关联, 一个Student对象关联一个Clazz对象
    // property: 提供要映射的POJO类的属性名
    // javaType: 用来指定要映射的java类型
    <association property="Clazz" javaType="Clazz">
        <id property="cid" column="cid"/>
        <result property="cname" column="cname"/>
    </association>
</resultMap>
<select id="selectByIdAssociation" resultMap="studentResultMapAssociation">
    select
        s.sid,s.sname,c.cid,c.cname
    from
        stu s left join clazz c on s.cid = c.cid
    where
        s.sid = #{sid}
</select>
// 接口
public interface StudentMapper{
    // 根据id获取学生信息, 同时获取学生关联的班级信息
    // 返回一个学生对象, 但是学生对象当中含有班级对象
    // 使用association
    Student selectByIdAssociation(Integer id);
} 
// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    CarMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student student = mapper.selectByIdAssociation(6);
    System.out.println(student);
    sqlSession.close();
}
  1. 第三种方式: 俩条sql语句, 分布查询(这种方式常用: 优点可复用,支持懒加载)
// StudentMapper.xml
// 一条sql语句, association(关联)
<resultMap id="studentResultMapByStep" type="Student">
    <id property="sid" column="sid"/>
    <result property="sname" column="sname"/>
  // 会将column传给select这条sql语句
    <association property="Clazz"
                 select="ClazzMapper.selectByIdStep2"
                 column="cid"/>
</resultMap>
<select id="selectByIdStep1" resultMap="studentResultMapByStep">
    select sid,sname,cid from stu where sid = #{sid}
</select>
-------------------------------------------------------------------------------------
// ClazzMapper.xml
<select id="selectByIdStep2" resultType="Clazz">
    select cid,cname from clazz where cid = #{cid}
</select>
-------------------------------------------------------------------------------------
// 接口
public interface StudentMapper{
  // 分布查询第一步, 先根据id查询出学生信息
    Student selectByIdStep1(Integer id);
} 
public interface ClazzMapper{
  // 分布查询第二步, 根据cid获取班级信息
  Clazz selectByIdStep2(Integer cid);
}
// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    CarMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student student = mapper.selectByIdStep1(6);
    System.out.println(student);
    sqlSession.close();
}

2. 延迟加载:

  • 分布查询的好处:
  • 复用性强, 可以重复使用, 大步拆成多个小碎步, 每一个小碎步更加可以重复利用.
  • 可以充分利用他妈的延迟加载/懒加载机制
  • 什么是延迟加载(懒加载), 有什么用?
  • 延迟加载的核心原理是: 用的时候再执行查询语句, 不用的时候不查询.
  • 作用: 提高性能
  • 在mybatis中怎么开启延迟加载?
  • 默认情况下是没有开启懒加载的
  • association标签中添加fetchType="lazy"
  • 这种在association标签中配置fetchType="lazy"是局部的设置, 只对当前的association关联的sql语句起作用
<resultMap id="studentResultMapByStep" type="Student">
    <id property="sid" column="sid"/>
    <result property="sname" column="sname"/>
    <association property="Clazz"
                 // 这条sql语句用到的时候再查询
                 select="ClazzMapper.selectByIdStep2"
                 column="cid"
                 fetchType="lazy"/>
</resultMap>
// 如果只需要查看学生的名字
//    那么就不会使用到ClazzMapper.selectByIdStep2语句
// 如果想看班级的名字
//    那么就会执行ClazzMapper.selectByIdStep2语句了

3. 一对多:

一个班级对应多个学生(班级表是主表, 学生表是副表)

一对多的实现, 通常是在一的一方中有List集合属性

一对多的实现通常包括俩种实现方式:

  1. collection

// ClazzMapper.xml
<resultMap id="clazzResultMap" type="Clazz">
    <id property="cid" column="cid"/>
    <result property="cname" column="cname"/>
    // 一对多, 这里的collection是集合的意思
    // ofType属性用来指定集合当中的元素类型
  <collection property="stus //list集合名" ofType="Student">
      <id property="sid" column="sid">
      <result property="sname" column="sname">
  </collection>
</resultMap>
<select id="selectByCollection" resultMap="clazzResultMap">
    select c.cid,c.cname,s.sid,s.sname 
      from clazz c left join stu s on c.cid = s.cid
    where c.cid = #{cid}
</select>
// 接口
public interface ClazzMapper{
  // 根据班级编号查询班级信息
  Clazz selectByCollection(Integer cid);
}
// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    CarMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Clazz clazz = mapper.selectByCollection(6);
    System.out.println(clazz);
    sqlSession.close();
}
  1. 分布查询(常用)

// StudentMapper.xml
<select id="selectByIdStep2" resultType="Student">
    select * from stu where cid = #{cid}
</select>
-------------------------------------------------------------------------------------
// ClazzMapper.xml
<resultMap id="clazzResultMapStep" type="Clazz">
    <id property="cid" column="cid"/>
    <result property="cname" column="cname"/>
  // 会将column传给select这条sql语句
    <collection property="stus"
                 select="StudentMapper.selectByCidStep2"
                 column="cid"/>
</resultMap>
<select id="selectByIdStep1" resultMap="clazzResultMapStep">
    select cid,cname from clazz where cid = #{cid}
</select>
-------------------------------------------------------------------------------------
// 接口
public interface StudentMapper{
  // 分布查询第二步, 先根据班级编号查询出学生信息
    List<Student> selectByCidStep2(Integer id);
} 
public interface ClazzMapper{
  // 分布查询第一步, 根据班级编号获取班级信息
  Clazz selectByIdStep1(Integer cid);
}
// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    CarMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Clazz clazz = mapper.selectByIdStep1(6);
    System.out.println(clazz);
    sqlSession.close();
}


相关文章
|
5月前
|
SQL Java 数据库连接
MyBatis 的映射关系
MyBatis 核心功能之一是映射关系,支持一对一、一对多和多对多三种 ORM 映射。通过实体类与配置文件结合,开发者可灵活实现数据关联,提升数据库操作效率。
362 4
|
9月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
943 5
|
11月前
|
SQL XML Java
七、MyBatis自定义映射resultMap
七、MyBatis自定义映射resultMap
337 6
|
12月前
|
SQL XML Java
八、(了解即可)MyBatis懒加载(或者叫延迟加载)
八、(了解即可)MyBatis懒加载(或者叫延迟加载)
381 1
|
XML Java 数据库连接
Mybatis一对一,一对多关联查询
## MyBatis一对一、一对多关联查询详解 MyBatis是一款优秀的持久层框架,提供了灵活的SQL映射功能,支持复杂的数据库操作。本文将详细介绍MyBatis中一对一和一对多关联查询的实现。 ### 一对一关联查询 一对一关联关系指的是一个表中的一条记录与另一个表中的一条记录相关联。例如,一个用户有一个地址信息。 #### 数据库表设计 假设有两个表:`user`和 `address`。 ``` CREATE TABLE user ( id INT PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE address
429 18
|
11月前
|
Java 数据库连接 mybatis
MyBatis篇-映射关系(1-1 1-n n-n)
本文介绍了MyBatis中四种常见关系映射的配置方法,包括一对一、一对多、多对一和多对多。**一对一**通过`resultMap`实现属性与字段的映射;**一对多**以用户-角色为例,使用`&lt;collection&gt;`标签关联集合数据;**多对一**以作者-博客为例,利用`&lt;association&gt;`实现关联;**多对多**则通过引入第三方类(如UserForDept)分别在User和Dept类中添加集合属性,并配置对应的`&lt;collection&gt;`标签完成映射。这些方法解决了复杂数据关系的处理问题,提升了开发效率。
|
XML Java 数据库连接
Mybatis映射关系
简介:本文介绍了MyBatis框架中四种常见的关系映射方式,包括一对一、一对多、多对一及多对多。一对一通过简单属性映射实现;一对多通过在主对象中添加集合属性并使用`&lt;collection&gt;`标签映射子对象集合;多对一则利用`&lt;association&gt;`标签在主对象中映射单个子对象;多对多需引入第三方类,分别在两个主对象中添加对方的集合属性,并通过`&lt;collection&gt;`标签实现映射。
256 32
|
SQL 缓存 Java
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
|
8月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
1404 1
Spring boot 使用mybatis generator 自动生成代码插件
|
11月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
872 0