• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mybatis 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论
文章目录[隐藏]

建表

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">
        <resul<span style="color:transparent">来1源gaodai#ma#com搞*代#码1网</span>t 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底层原理
  • 索引
  • 索引优化

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mybatis一对多和多对一处理的深入讲解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址