上一篇文章:《Mybatis基本知识八:关联关系查询:多对一、一对一关联查询》
若文中有纰漏,请多多指正!!!
1. 前言
多对多关联查询在实际开发中是非常常见的一种查询。比如RBAC(Role-Based Access Control,基于角色的访问控制)即:用户、角色以及权限,就是用户通过角色(管理员角色,一般用户角色)与权限(通俗点就是前台页面能操作的功能项)进行关联,一个用户可以有多个角色,同时一个角色也可以有多个权限。再比如学校里学生和选修课程的关系,也是多对多的关系。其实多对多的关联查询可以看做是两个互反一对多的关系组成。
一般来说多对多的关系都会通过一张中间表进行关联。对于中间表来说,实际开发中一般是不会创建对应的实体类的,另外多对多关联查询在mapper配置查询时也有两种配置写法:
- 多表连接查询
- 多表单独查询(要拆分成一对多的模式进行查询)
本章节将回以学生(Student)和选修课程(Course)的关系进行讲解(中间表middle),以Student表作为主表进行关联查询。
2.创建表数据
/**学生*/create table Student( id int primary key auto_increment comment '主键', sname varchar(50) comment '姓名');INSERT INTO `Student`(sname) VALUES ('明明');INSERT INTO `Student`(sname) VALUES ('白白');/**课程*/create table Course( id int primary key auto_increment comment "主键", cname varchar(50) comment "课程名");INSERT INTO `Course` (cname)VALUES ('Java编程');INSERT INTO `Course` (cname)VALUES ('C++编程');INSERT INTO `Course` (cname)VALUES ('Python编程');/**中间表*/create table Middle( id int primary key auto_increment comment "主键", sid int comment "学生ID", cid int comment "课程ID");INSERT INTO `Middle` (sid,cid)VALUES (1,1);INSERT INTO `Middle` (sid,cid)VALUES (1,2);INSERT INTO `Middle` (sid,cid)VALUES (2,1);INSERT INTO `Middle` (sid,cid)VALUES (2,3);
3.搭建实体类
//学生- 多的一方(主)public class Student { private Integer id;//主键 private String sname;//名称 private List courses;//课程 }//课程 -多的一方(辅)public class Course { private Integer id; private String cname;//名称 private List stus;//学生 //tostring 不包含 stus}
4.搭建DAO接口
//多对多查询public interface StudentDao { //多表单独查询 Student findStudentByIdSingle(Integer id); //多表连接查询 Student findStudentByIdGather(Integer id);}
5.Mapper配置文件整体搭建
select id,sname from student where id = #{id} select c.id,c.cname from course c,middle m where c.id=m.cid and m.sid = #{id} select s.id stuid,s.sname,c.id cid ,c.cname from student s,middle m ,course c where s.id=m.sid and c.id=m.cid and s.id = #{id}
6.测试方法以及测试结果输出
//多对多测试@Testpublic void findStudent(){ //1.多表单独查询 Student single = dao.findStudentByIdSingle(1); System.out.println(single); System.out.println("************************************"); //2.多表连接查询 Student gather = dao.findStudentByIdGather(2); System.out.println(gather);}测试结果:DEBUG [main] - ==> Preparing: select id,sname from student where id = ? DEBUG [main] - ==> Parameters: 1(Integer)DEBUG [main] - ====> Preparing: select c.id,c.cname from course c,middle m where c.id=m.cid and m.sid = ? DEBUG [main] - ====> Parameters: 1(Integer)DEBUG [main] - <==== Total: 2DEBUG [main] - <== Total: 1Student [id=1, sname=明明, courses=[Course [id=1, cname=Java编程], Course [id=2, cname=C++编程]]]****************************************DEBUG [main] - ==> Preparing: select s.id stuid,s.sname,c.id cid ,c.cname from student s,middle m ,course c where s.id=m.sid and c.id=m.cid and s.id = ? DEBUG [main] - ==> Parameters: 2(Integer)DEBUG [main] - <== Total: 2Student [id=2, sname=白白, courses=[Course [id=1, cname=Java编程], Course [id=3, cname=Python编程]]]
7.Down测试源码
链接:https://pan.baidu.com/s/1SX2-mYD_YOsNGYKPKzCoag 提取码:g0z4
![1eb2847bcff9e83f4eb5af14bb326ccd.png](https://i-blog.csdnimg.cn/blog_migrate/1cb0b9f5f02eefcd1da57e5dcfde065d.jpeg)
Day Day Up