开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/3baea10a3a1104bda815c206954b2b687511aa3d

前言:

之前我们探讨了一对一、一对多的映射关系,今天我们来讨论多对多的映射关系。

多对多,其实可以拆成多个一对多来理解。

比如:

学生-------课程----------学生选课的关系:

(1)查询某个学生所选的全部课程;

(2)查询选修某个课程的全部学生;

今天我们就来实现这个实例。


一、数据库建表:

1.student表:

2.course表:

3.student-course表:

二、查询某个学生所选的全部课程代码实现:

1.代码实现:

(1)添加Course实体:

 package com.xm.pojo;
/**
* 课程实体
* @author xm
*
*/
public class Course {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

Course.java

(2)添加StudntCourse实体:

 package com.xm.pojo;

 import java.util.List;
/**
* 学生选课实体
* @author xm
*
*/
public class StudentCourse { private int sid;
private int cid;
private int sorce; private List<Student> students;
private List<Course> courses;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public int getSorce() {
return sorce;
}
public void setSorce(int sorce) {
this.sorce = sorce;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
} }

StudentCourse.java

(3)在Studnent实体中添加StudentCourse列表:

 package com.xm.pojo;

 import java.util.List;

 /**
* name:学生实体
* @author xxm
*
*/
public class Student {
/**
* content:主键id
*/
private int id;
/**
* content:姓名
*/
private String name; private List<Book> books; private List<StudentCourse> studentCourses; public Student() {
// TODO Auto-generated constructor stub
} public List<StudentCourse> getStudentCourses() {
return studentCourses;
} public void setStudentCourses(List<StudentCourse> studentCourses) {
this.studentCourses = studentCourses;
} public List<Book> getBooks() {
return books;
} public void setBooks(List<Book> books) {
this.books = books;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

Student.java

(4)在数据库操作接口中添加方法:

 package com.xm.mapper;

 import java.util.List;

 import com.xm.pojo.Student;

 public interface StudentMapper {

     /***********/

     /**
* 根据学生id查询该学生选修的所有课程
* @param id
* @return
*/
public Student selectCourseById(Integer id); }

StudentMapper.java

(5)完善mapper映射:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xm.mapper.StudentMapper"> <resultMap type="student" id="studentMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap> <resultMap type="student" id="courseMap" extends="studentMap">
<collection property="studentCourses" ofType="studentCourse">
<result property="sorce" column="sorce"/>
<collection property="courses" ofType="course">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
</collection>
</collection>
</resultMap> <!-- 根据学生id查询该学生选修的所有课程 -->
<select id="selectCourseById" parameterType="int" resultMap="courseMap" >
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}
</select>
</mapper>

StudentMapper.xml

(6)在controller中实现:

 package com.xm.controller;

 import java.util.List;

 import javax.websocket.server.PathParam;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController; import com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RestController
public class StudentController {
@Autowired
private StudentMapper studentMapper; /************/ /**
* 根据学生id查询该学生选修的所有课程
* @param id
* @return
*/
@GetMapping("/student/course/{id}")
public Student selectCourseById(@PathVariable("id") Integer id) {
Student student = studentMapper.selectCourseById(id);
return student;
} }

StudentController.java

2.测试结果:

(1)数据库运行

2.postman运行:

三、查询选修某个课程的全部学生代码实现:

1.代码实现:

(1)Course实体中添加StudentCourse列表:

 package com.xm.pojo;

 import java.util.List;

 /**
* 课程实体
* @author xm
*
*/
public class Course {
private int id;
private String name;
private List<StudentCourse> studentCourses; public List<StudentCourse> getStudentCourses() {
return studentCourses;
}
public void setStudentCourses(List<StudentCourse> studentCourses) {
this.studentCourses = studentCourses;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

Course.java

(2)添加Course数据操作接口:

 package com.xm.mapper;

 import com.xm.pojo.Course;

 public interface CourseMapper {
/**
* 根据课程id查询选修此课程的全部学生
* @param id
* @return
*/
public Course selectStudentById(Integer id); }

CourseMapper.java

(3)添加mapper映射:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xm.mapper.CourseMapper">
<resultMap type="course" id="courseMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<resultMap type="course" id="studentMap" extends="courseMap">
<collection property="studentCourses" ofType="studentCourse">
<result property="sorce" column="sorce"/>
<collection property="students" ofType="student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
</collection>
</collection>
</resultMap>
<!-- 根据课程id查询选修此课程的全部学生 -->
<select id="selectStudentById" parameterType="int" resultMap="studentMap">
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}
</select>
</mapper>

CourseMapper.xml

(4)添加controller:

 package com.xm.controller;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.xm.mapper.CourseMapper;
import com.xm.pojo.Course; /**
* 课程
* @author xm
*
*/
@RestController
public class CourseController {
@Autowired
private CourseMapper courseMapper; /**
* 根据课程id查询选修此课程的全部学生
* @param id
* @return
*/
@GetMapping("/course/student/{id}")
public Course selectStudentById(@PathVariable("id")Integer id) { Course course = courseMapper.selectStudentById(id);
return course; } }

CourseController.java

2.测试结果:

(1)数据库运行

(2)postman运行

                                                                                                                   2018-06-22

5、SpringBoot+Mybatis整合------多对多的更多相关文章

  1. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  2. SpringBoot+Mybatis整合入门(一)

    SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...

  3. SpringBoot+Mybatis整合实例

    前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...

  4. 2、SpringBoot+Mybatis整合------一对一

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/93398da60c647573645917b2 ...

  5. springboot/Mybatis整合

    正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper ...

  6. springboot+mybatis整合(单元测试,异常处理,日志管理,AOP)

    我用的事IDEA,jdk版本是1.7.新建项目的时候这个地方的选择需要注意一下,springboot版本是1.5的,否则不支持1.7的jdk pom.xml <dependency> &l ...

  7. 9、SpringBoot+Mybatis整合------动态sql

    开发工具:STS 前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 动态sql中的语法: where标签 if标签 trim标签 set标签 s ...

  8. 3、SpringBoot+Mybatis整合------主键回填

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870 ...

  9. 1、SpringBoot+Mybatis整合------简单CRUD的实现

    编译工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9 ...

随机推荐

  1. Unity string 转换为 Quaternion

    public Quaternion QuaternionParse(string name) { name = name.Replace("(", "").Re ...

  2. informix(南大通用)sql语法的差异

    1.create view  444(...)  as select ...from... 2.insert into select.......union  select     不支持 请分开写 ...

  3. golang中并发的相关知识

    golang中done channel理解:https://segmentfault.com/a/1190000006261218 golang并发模型之使用Context:https://segme ...

  4. [Maven] Project build error: 'packaging' with value 'jar' is invalid. Aggregator projects require 'pom' as packaging.

    将<packaging>jar</packaging> 改为<packaging>pom</packaging>

  5. jQuery中的节点操作(二)

    html代码如下 <p title="武汉长乐教育PHP系列教程" name="hello" class="blue"> < ...

  6. flexpager权限控制文件crossdomain.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE cross-domain-policy SY ...

  7. InvocationTargetException异常

    package com.smbea.demo.reflect; /** * 越界异常 * @author hapday * @date 2017年1月20日 @time下午7:59:01 */ pub ...

  8. JS在与lua的交互心得

    最近在写一个项目,前端技术使用的是Vue,在与lua的交互过程,是通过一个公共JS,前端调用公共js的方法给lua发送命令,lua接到命令,去执行一些方法,然后又通过回调返回到了前端,由于是第一次写这 ...

  9. window系统安装jdk,jre

    java开发少不了安装jdk.当然如果只是想运行其他人的java项目,只需要安装jre就行了,不需要安装jdk,jdk是编译用的.jdk可以同时安装多个 版本,只需要在项目部署时注意切换版本选择.在这 ...

  10. python类的反射

    反射 通过字符串映射或者修改程序运行时的状态.属性.方法, 有一下4个方法 小例子--根据用户输入调用方法: class Dog(object): def __init__(self,name): s ...