Mybatis一对多和多对一处理的深入讲解

目录
  • 建表
  • 多对一处理
    • mapper
    • 实体类
    • 按照查询嵌套处理
    • 按照结果嵌套处理
    • 回顾Mysql多对一查询方式
  • 一对多处理
    • mapper
    • 实体类
    • 按照查询嵌套处理
    • 按照查询嵌套处理
  • 结果映射
  • 面试高频点
  • 总结

建表

SQL:

create table teacher(
    id int not null,
    name varchar(30) default null,
    primary key (id)
);

insert into teacher (id, name) values (1, '蔡老师');

create table student(
    id int not null ,
    name varchar(30) default null,
    tid int default null,
    constraint fk_tid foreign key (tid) references teacher(id)
);

insert into student(id, name, tid) VALUES (1, '小名', 1);
insert into student(id, name, tid) VALUES (2, '小红', 1);
insert into student(id, name, tid) VALUES (3, '小亮', 1);
insert into student(id, name, tid) VALUES (4, '小兰', 1);
insert into student(id, name, tid) VALUES (5, '笑笑', 1);

多对一处理

  • 多个学生对应一个老师
  • 对于学生这边而言,关联。即多个学生关联一个老师【多对一】
  • 对于老师这边而言,集合。即一个老师有很多的学生【一对多】

mapper

//查询所有的学生信息以及对应的老师的信息
List<Student> queryStudentAndTeacher();

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;

    //学生需要关联一个老师
    private Teacher teacher;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
    private int id;
    private String name;
}

按照查询嵌套处理

<!--思路:
	1.查询所有的学生
	2.根据查询出来的学生的tid寻找对应的老师  寻找对应的老师,子查询
-->
<resultMap id="rStuAndTea" type="student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <!-- 复杂的属性我们需要单独处理
							指定属性的类型
		 对象使用association  javaType
		 集合使用collection   ofType
	-->
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="queryStudentAndTeacher" resultMap="rStuAndTea">
    select * from student
</select>
<select id="getTeacher" resultType="teacher">
    select * from teacher where id = #{id}
</select>

按照结果嵌套处理

<!--方式二  按照结果嵌套处理-->
<resultMap id="rStuAndTea2" type="student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>
<select id="queryStudentAndTeacher2" resultMap="rStuAndTea2">
    select s.id sid, s.name sname, t.name tname
    from student s, teacher t
    where s.tid = t.id
</select>

回顾Mysql多对一查询方式

  • 子查询
  • 联表查询

一对多处理

  • 一个老师有多个学生
  • 对于老师这边而言,集合。即一个老师有很多的学生【一对多】

mapper

//查询指定老师的信息及其所有的学生
Teacher queryTeaAndStu(@Param("tid") int id);

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
    private int id;
    private String name;

    //一个老师拥有多个学生
    private List<Student> students;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;
    private int tid;
}

按照查询嵌套处理

<!--按照结果嵌套查询-->
<resultMap id="rTeaAndStu" type="teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="students" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>
<select id="queryTeaAndStu" resultMap="rTeaAndStu">
    select s.id sid, s.name sname, t.name tname, t.id tid
    from student s, teacher t
    where s.tid = t.id and t.id = #{tid}
</select>

按照查询嵌套处理

<!--按照查询嵌套处理-->
<select id="queryTeaAndStu2" resultMap="rTeaAndStu2">
    select * from teacher where id = #{tid}
</select>
<resultMap id="rTeaAndStu2" type="teacher">
    <collection property="students" javaType="ArrayList" ofType="Student"
                select="queryStudentByTeacherId" column="id"/>
</resultMap>
<select id="queryStudentByTeacherId" resultType="Student">
    select * from student where tid = #{tid}
</select>

结果映射

面试高频点

  • MySQL引擎
  • InnoDB底层原理
  • 索引
  • 索引优化

小结

  1. 关联 - association 【多对一】
  2. 集合 - collection 【一对多】
  3. javaType & ofType
    1. javaType 用来指定实体类中属性的类型
    2. ofType 用来指定映射到List或者集合中的entity类型,泛型中的约束类型

注意点:

  • 保证SQL的可读性,尽量保证通俗易懂
  • 注意一对多和多对一中属性名和字段的问题
  • 如果问题不好排查错误,可以使用LOG4J日志

总结

到此这篇关于Mybatis一对多和多对一处理的文章就介绍到这了,更多相关Mybatis一对多和多对一处理内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Mybatis 一对多和多对一关联查询问题

    首先  数据库量表之间字段关系(没有主外键) studentmajor表的id字段对应student表里major字段 两个实体类 package com.model; import java.util.Date; public class Student { private Integer sno; private String sname; private String ssex; private Integer sclass; private StudentMajor studentmaj

  • Mybatis关联查询之一对多和多对一XML配置详解

    平时在开发过程中dao.bean和XML文件都是自动生成的,很少写XML的配置关系,今天记录一下mybatis的关联查询中的多对一和一对多的情况. 首先是有两张表(学生表Student和老师Teacher表,注:这里只是为了演示一对多和多对一的情况,请不要杠),为了更易懂,这里只设置了最简单的几个必要字段.表结构如下图 Student表: Teacher表: 创建实体bean Teacher.java: import java.util.List; public class Teacher {

  • MyBatis如何实现多表查询(多对一、一对多)

    MyBatis实现多表查询 一.多对一查询 数据库的准备 创建两张表,一张老师表,一张学生表 将老师主键id关联学生外键tid 创建sql的语句 create table teacher( id int primary key, teacher_name varchar(30) not null ) insert into teacher(id,teacher_name) values (1,'毛老师') create table student( id int primary key, stu

  • MyBatisPlus 一对多、多对一、多对多的完美解决方案

    在学习MyBatisPlus 时,简单的查询非常简单,只需继承了相关类,就能够进行增删改.但是在实际运用时,对象之间的关系非常复杂,一对多.多对一.多对多.网上查询了大量i资料都无法解决此问题. 难道要把所有的用Mybatis的重写一次? 重写一次Plus的方法还能不能用? 实在没办只能查看官网https://mp.baomidou.com/guide/在注解处找到了可能的解决方案 @TableName注解可以设置对应的resultMap 看到这里我想是不是,在Mapper中设置好resultM

  • Mybatis一对多与多对一查询处理详解

    要点 主要还是结果集映射(resultMap) association标签: 一个复杂类型的关联:许多结果将包装成这种类型(JavaBean)嵌套结果映射,关联可以是 resultMap 元素,或是对其它结果映射的引用 collection标签: 一个复杂类型的集合(List)嵌套结果映射,集合可以是resultMap元素,或是对其它结果映射的引用 一对多(association) 数据库结构 tid是student的外键,是teacher表的id JavaBean public class S

  • mybatis关系映射之一对多和多对一

    本实例使用用户和订单的例子做说明: 一个用户可以有多个订单, 一个订单只对应一个用户.(其中应用到注释) 1.代码的结构 2. 建表语句: CREATE DATABASE test; USE test; CREATE TABLE person( personId VARCHAR(36) PRIMARY KEY, personName VARCHAR(64), personAddress VARCHAR(128), personTel VARCHAR(11) ); CREATE TABLE ord

  • Mybatis一对多和多对一处理的深入讲解

    目录 建表 多对一处理 mapper 实体类 按照查询嵌套处理 按照结果嵌套处理 回顾Mysql多对一查询方式 一对多处理 mapper 实体类 按照查询嵌套处理 按照查询嵌套处理 结果映射 面试高频点 总结 建表 SQL: create table teacher( id int not null, name varchar(30) default null, primary key (id) ); insert into teacher (id, name) values (1, '蔡老师'

  • Mybatis中的高级映射一对一、一对多、多对多

    学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下mybatis中的高级映射,包括一对一.一对多.多对多,希望多有需要的小伙伴有帮助,小编主要从四个方面进行介绍,订单商品数据模型.一对一查询.一对多查询.多对多查询. 一.订单商品数据模型 1.数据库执行脚本,如下所示: <span style="font-family:Comic Sans MS;font-size:18px;">CREATE

  • mybatis 一对一、一对多和多对多查询实例代码

    关键字:association 一对一映射(一个班级只有一个班主任) <select id="getClass" parameterType="int" resultMap="ClassesResultMap"> select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id} </select> <resultMap type=&q

  • SpringBoot中Mybatis注解一对多和多对多查询实现示例

    目录 一.模拟的业务查询 二.对应的实体类如下 三.对应的建表语句和模拟数据如下 四.@One一对一映射 五.@Many一对多查询 六.@One @Many的总结 一.模拟的业务查询 系统中的用户user都有唯一对应的地址信息address,每个用户可以有多量车car,类似如下结构 |-- user |-- address |-- carList |-- car1 |-- car2 二.对应的实体类如下 @Data public class AddressPO { private Long id

  • MyBatis存储过程、MyBatis分页、MyBatis一对多增删改查操作

    一.用到的实体类如下: Student.java package com.company.entity; import java.io.Serializable; import java.util.Date; public class Student implements Serializable{ private static final long serialVersionUID = 1L; private int id; private String name; private Date

  • Mybatis一对多关联关系映射实现过程解析

    这篇文章主要介绍了Mybatis一对多关联关系映射实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一对多关联关系只需要在多的一方引入少的一方的主键作为外键即可.在实体类中就是反过来,在少的一方添加多的一方,声明一个List<另一方> 属性名 作为少的一方的属性. 用户和订单就是一对多的关系,从用户角度看就是一对多关系,从订单的角度来看就是多对一的关系. /** * 订单持久化类 */ public class Orders { p

  • mybatis一对多查询功能

    首先,我们还是先给出一个需求:根据订单id查询订单明细--我们知道,一个订单里面可以有多个订单的明细(需求不明确的同学,请留言或者去淘宝网上的订单处点一下就知道了).这个时候,一个订单,对应多个订单的id.这种需求出现的时候,我们应该如何查询呢? 此时我们的数据模型如下图(左)由于查询用户也是我们的需求,所以就在原有的基础上进行扩展,数据模型如下(右): 很显然,如果用resultType的方式去实现的话,是不合理的了.因为我们需要创建一个既有订单又有订单明细的pojo然后呢,我们的mybati

随机推荐