开发工具:STS
代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/3baea10a3a1104bda815c206954b2b687511aa3d
前言:
之前我们探讨了一对一、一对多的映射关系,今天我们来讨论多对多的映射关系。
多对多,其实可以拆成多个一对多来理解。
比如:
学生-------课程----------学生选课的关系:
(1)查询某个学生所选的全部课程;
(2)查询选修某个课程的全部学生;
今天我们就来实现这个实例。
一、数据库建表:
1.student表:
2.course表:
3.student-course表:
二、查询某个学生所选的全部课程代码实现:
1.代码实现:
(1)添加Course实体:
1 package com.xm.pojo; 2 /** 3 * 课程实体 4 * @author xm 5 * 6 */ 7 public class Course { 8 private int id; 9 private String name; 10 public int getId() { 11 return id; 12 } 13 public void setId(int id) { 14 this.id = id; 15 } 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 24 }
(2)添加StudntCourse实体:
1 package com.xm.pojo; 2 3 import java.util.List; 4 /** 5 * 学生选课实体 6 * @author xm 7 * 8 */ 9 public class StudentCourse { 10 11 private int sid; 12 private int cid; 13 private int sorce; 14 15 private List<Student> students; 16 private List<Course> courses; 17 public int getSid() { 18 return sid; 19 } 20 public void setSid(int sid) { 21 this.sid = sid; 22 } 23 public int getCid() { 24 return cid; 25 } 26 public void setCid(int cid) { 27 this.cid = cid; 28 } 29 public int getSorce() { 30 return sorce; 31 } 32 public void setSorce(int sorce) { 33 this.sorce = sorce; 34 } 35 public List<Student> getStudents() { 36 return students; 37 } 38 public void setStudents(List<Student> students) { 39 this.students = students; 40 } 41 public List<Course> getCourses() { 42 return courses; 43 } 44 public void setCourses(List<Course> courses) { 45 this.courses = courses; 46 } 47 48 49 50 }
(3)在Studnent实体中添加StudentCourse列表:
1 package com.xm.pojo; 2 3 import java.util.List; 4 5 /** 6 * name:学生实体 7 * @author xxm 8 * 9 */ 10 public class Student { 11 /** 12 * content:主键id 13 */ 14 private int id; 15 /** 16 * content:姓名 17 */ 18 private String name; 19 20 private List<Book> books; 21 22 private List<StudentCourse> studentCourses; 23 24 public Student() { 25 // TODO Auto-generated constructor stub 26 } 27 28 29 public List<StudentCourse> getStudentCourses() { 30 return studentCourses; 31 } 32 33 34 public void setStudentCourses(List<StudentCourse> studentCourses) { 35 this.studentCourses = studentCourses; 36 } 37 38 39 public List<Book> getBooks() { 40 return books; 41 } 42 43 44 public void setBooks(List<Book> books) { 45 this.books = books; 46 } 47 48 49 public int getId() { 50 return id; 51 } 52 public void setId(int id) { 53 this.id = id; 54 } 55 public String getName() { 56 return name; 57 } 58 public void setName(String name) { 59 this.name = name; 60 } 61 62 63 64 }
(4)在数据库操作接口中添加方法:
1 package com.xm.mapper; 2 3 import java.util.List; 4 5 import com.xm.pojo.Student; 6 7 public interface StudentMapper { 8 9 /***********/ 10 11 /** 12 * 根据学生id查询该学生选修的所有课程 13 * @param id 14 * @return 15 */ 16 public Student selectCourseById(Integer id); 17 18 }
(5)完善mapper映射:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xm.mapper.StudentMapper"> 4 5 6 7 <resultMap type="student" id="studentMap"> 8 <id property="id" column="id"/> 9 <result property="name" column="name"/> 10 </resultMap> 11 12 13 14 <resultMap type="student" id="courseMap" extends="studentMap"> 15 <collection property="studentCourses" ofType="studentCourse"> 16 <result property="sorce" column="sorce"/> 17 <collection property="courses" ofType="course"> 18 <id property="id" column="cid"/> 19 <result property="name" column="cname"/> 20 </collection> 21 </collection> 22 </resultMap> 23 24 <!-- 根据学生id查询该学生选修的所有课程 --> 25 <select id="selectCourseById" parameterType="int" resultMap="courseMap" > 26 select a.*,b.sorce,c.id cid,c.name cname from student a,student_course b,course c where a.id=b.sid and b.cid=c.id and a.id=#{id} 27 </select> 28 </mapper>
(6)在controller中实现:
1 package com.xm.controller; 2 3 import java.util.List; 4 5 import javax.websocket.server.PathParam; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.web.bind.annotation.DeleteMapping; 9 import org.springframework.web.bind.annotation.GetMapping; 10 import org.springframework.web.bind.annotation.PathVariable; 11 import org.springframework.web.bind.annotation.PostMapping; 12 import org.springframework.web.bind.annotation.PutMapping; 13 import org.springframework.web.bind.annotation.RestController; 14 15 import com.xm.mapper.StudentMapper; 16 import com.xm.pojo.Student; 17 18 @RestController 19 public class StudentController { 20 @Autowired 21 private StudentMapper studentMapper; 22 23 /************/ 24 25 /** 26 * 根据学生id查询该学生选修的所有课程 27 * @param id 28 * @return 29 */ 30 @GetMapping("/student/course/{id}") 31 public Student selectCourseById(@PathVariable("id") Integer id) { 32 Student student = studentMapper.selectCourseById(id); 33 return student; 34 } 35 36 }
2.测试结果:
(1)数据库运行
2.postman运行:
三、查询选修某个课程的全部学生代码实现:
1.代码实现:
(1)Course实体中添加StudentCourse列表:
1 package com.xm.pojo; 2 3 import java.util.List; 4 5 /** 6 * 课程实体 7 * @author xm 8 * 9 */ 10 public class Course { 11 private int id; 12 private String name; 13 private List<StudentCourse> studentCourses; 14 15 public List<StudentCourse> getStudentCourses() { 16 return studentCourses; 17 } 18 public void setStudentCourses(List<StudentCourse> studentCourses) { 19 this.studentCourses = studentCourses; 20 } 21 public int getId() { 22 return id; 23 } 24 public void setId(int id) { 25 this.id = id; 26 } 27 public String getName() { 28 return name; 29 } 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 35 }
(2)添加Course数据操作接口:
1 package com.xm.mapper; 2 3 import com.xm.pojo.Course; 4 5 public interface CourseMapper { 6 /** 7 * 根据课程id查询选修此课程的全部学生 8 * @param id 9 * @return 10 */ 11 public Course selectStudentById(Integer id); 12 13 }
(3)添加mapper映射:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xm.mapper.CourseMapper"> 4 <resultMap type="course" id="courseMap"> 5 <id property="id" column="id"/> 6 <result property="name" column="name"/> 7 </resultMap> 8 <resultMap type="course" id="studentMap" extends="courseMap"> 9 <collection property="studentCourses" ofType="studentCourse"> 10 <result property="sorce" column="sorce"/> 11 <collection property="students" ofType="student"> 12 <id property="id" column="sid"/> 13 <result property="name" column="sname"/> 14 </collection> 15 </collection> 16 </resultMap> 17 <!-- 根据课程id查询选修此课程的全部学生 --> 18 <select id="selectStudentById" parameterType="int" resultMap="studentMap"> 19 select a.*,b.sorce,c.id sid,c.name sname from student c,student_course b,course a where a.id=b.cid and b.sid=c.id and a.id=#{id} 20 </select> 21 </mapper>
(4)添加controller:
1 package com.xm.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import com.xm.mapper.CourseMapper; 9 import com.xm.pojo.Course; 10 11 /** 12 * 课程 13 * @author xm 14 * 15 */ 16 @RestController 17 public class CourseController { 18 @Autowired 19 private CourseMapper courseMapper; 20 21 /** 22 * 根据课程id查询选修此课程的全部学生 23 * @param id 24 * @return 25 */ 26 @GetMapping("/course/student/{id}") 27 public Course selectStudentById(@PathVariable("id")Integer id) { 28 29 Course course = courseMapper.selectStudentById(id); 30 return course; 31 32 } 33 34 }