@Data
@AllArgsConstructor
@NoArgsConstructor
public class Course {
private Integer id;
private String courseCode; // 课程编号
private String courseName;// 课程名称
private List<Student> students;// 选课学生
public Course(Integer id, String courseCode, String courseName) {
super();
this.id = id;
this.courseCode = courseCode;
this.courseName = courseName;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name; // 姓名
private String gender; // 性别
private String major; // 专业
private String grade; // 年级
private List<Course> courses;// 所选的课程
public Student(Integer id, String name, String gender, String major,
String grade) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.major = major;
this.grade = grade;
}
}
xml写法
<!-- 这里使用了嵌套结果ResultMap的方式进行级联查询,当然也可以使用嵌套查询select -->
<!-- 映射一个基本的Student查询结果 -->
<resultMap id="StudentResult" type="com.chyangwu.demo.entity.Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gender" column="gender"/>
<result property="major" column="major"/>
<result property="grade" column="grade"/>
</resultMap>
<!-- 继承上面那个基本的映射,再扩展出级联查询 -->
<resultMap id="StudentResultWithCourses" type="com.chyangwu.demo.entity.Course" extends="CourseResult">
<collection property="students" resultMap="StudentResult"></collection>
</resultMap>
<!-- 这里特别要是的是column="cid" 这是和select语句中的 c.id as cid对应的 一定一定一定要对应起来 -->
<resultMap id="CourseResult" type="com.chyangwu.demo.entity.Course">
<id property="id" column="cid"/>
<result property="courseCode" column="course_code"/>
<result property="courseName" column="course_name"/>
</resultMap>
<!--
注意:查询语句的中的c.id as cid这个地方,避免名字相同出现查询结果不正确的情况
同时在id="CourseResult"的resultMap中也有与这里对应的设置要特别特别注意
-->
<select id="getStudentByIdWithCourses" resultMap="StudentResultWithCourses">
select s.id,s.name,s.gender,s.major,s.grade,c.id as cid,c.course_code,c.course_name,sc.id,sc.student_id,sc.course_id
from student s
left join student_course sc on s.id=sc.student_id
right join course c on sc.course_id=c.id
</select>
一种课程对应多个学生
{
"data":[
{
"courseCode":"001",
"courseName":"心理学",
"id":1,
"students":[
{
"gender":"男",
"grade":"大一",
"id":1,
"major":"专业1",
"name":"苗文平"
},
{
"gender":"男",
"grade":"大二",
"id":3,
"major":"大师专业",
"name":"景永春"
},
{
"gender":"男",
"grade":"大四",
"id":4,
"major":"专业3",
"name":"老赵"
}
]
},
{
"courseCode":"002",
"courseName":"犯罪心理学",
"id":2,
"students":[
{
"gender":"男",
"grade":"大一",
"id":1,
"major":"专业1",
"name":"苗文平"
},
{
"gender":"男",
"grade":"大一",
"id":2,
"major":"专业1",
"name":"谢升伟"
},
{
"gender":"男",
"grade":"大二",
"id":3,
"major":"大师专业",
"name":"景永春"
}
]
},
{
"courseCode":"003",
"courseName":"统筹学",
"id":3,
"students":[
{
"gender":"男",
"grade":"大一",
"id":2,
"major":"专业1",
"name":"谢升伟"
},
{
"gender":"男",
"grade":"大二",
"id":3,
"major":"大师专业",
"name":"景永春"
}
]
},
{
"courseCode":"004",
"courseName":"土木工程",
"id":4,
"students":[
{
"gender":"女",
"grade":"大三",
"id":5,
"major":"专业2",
"name":"老五"
}
]
},
{
"courseCode":"005",
"courseName":"催眠学",
"id":5,
"students":[
]
}
],
"message":"请求成功",
"state":1
}