java中多对多的增删改查_4.SSM整合_多表_多对多的增删改查

多对多关系,课程和学生

接口

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值