多对多关系,课程和学生
接口
public interface CourseMapper {
/**
* 获取所有课程
* @return
* @throws Exception
*/
public List getAll() throws Exception;
}
映射文件
select t1.c_id id, t1.c_name name, t1.c_credit credit
from t_course t1
实体类
public class Course {
private Integer id;
private String name;
private Double credit;
private List students;
//省略getter和setter
多
接口
public interface StudentMapper {
/**
* 查询所有学生的选课情况
* @return
* @throws Exception
*/
public List getStuCou() throws Exception;
/**
* 删除指定id用户的某门课(根据课程id)的选课情况
* @param StudentCourseLink
* @throws Exception
* 入参为多个参数时,可以使用Map,model,或者@param
*/
public void delStuCouById(@Param("s_id") String s_id, @Param("c_id") String c_id) throws Exception;
/**
* 添加选课
* @param sc
* @throws Exception
*/
public void addCou(Stu_Cou sc) throws Exception;
/**
* 根据ID获取指定的学生以及选课情况
* @param id
* @return
* @throws Exception
*/
public Student getStuById(String id) throws Exception;
}
映射文件
select t1.*, t2.*
from t_student t1,
t_course t2,
t_stu_cou t3
where t1.s_id = t3.sc_s_id
and t2.c_id = t3.sc_c_id
select t1.c_id id, t1.c_name name, t1.c_credit credit
from t_course t1
left join t_stu_cou t2
on t1.c_id = t2.sc_c_id
where t2.sc_s_id = #{id}
select *
from t_student
where s_id = #{id}
delete from t_stu_cou
where sc_s_id = #{s_id}
and sc_c_id = #{c_id}
insert into t_stu_cou(sc_s_id, sc_c_id, createtime) value(#{stu.id}, #{cou.id}, #{createtime})
实体类
public class Student {
private Integer id;
private String name;
private String sex;
private Integer age;
private List courses;
//省略getter和setter
中间表
实体类
public class Stu_Cou {
private Student stu;
private Course cou;
private Date createtime;
//省略getter和setter