2 wjtyy wjtyy 于 2016.03.28 21:48 提问

关于mybatis多对多数据更新的问题

刚学mybatis,遇到如下的问题,纠结了很久啊。。。。。。各路神仙求帮忙
三个表:
drop table middles;
drop table students;
drop table courses;
create table students(
sid int(5) primary key,
sname varchar(10)
);
create table courses(
cid int(5) primary key,
cname varchar(10)
);
create table middles(
sid int(5),
cid int(5),
primary key(sid,cid)
);
学生实体和课程实体如下:
/**

  • 学生(多方) / public class Student { private Integer id; private String name; private List courseList = new ArrayList(); public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getCourseList() { return courseList; } public void setCourseList(List courseList) { this.courseList = courseList; } } /*
  • 课程(多方)
  • @author AdminTC
    */
    public class Course {
    private Integer id;
    private String name;
    private List studentList = new ArrayList();
    public Course(){}
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public List getStudentList() {
    return studentList;
    }
    public void setStudentList(List studentList) {
    this.studentList = studentList;
    }
    }
    现在的问题是,某个学生A在自己学的课程中,又加了一门课叫pentaKillCourse。
    代码流程如下:
    Course pentaKillCourse = new Course ();
    Student A = new Student();
    A.getCourseList().add(pentaKillCourse );
    studentService.update(user);
    --------------------------------studentService.update()方法如下-------------------------------------------
    public void update(User user) {
    userDao.updateUser(user);
    }
    --------------------------------userDao.updateUser()方法如下---------------------------------------------
    public void updateUser(User user) {
    SqlSession sqlSesion = sqlSessionFactory.openSession();
    sqlSesion.update("userNamespace.updateUser", user);

    }

    下面是userMapper:











    .........................................................................

1个回答

caozhy
caozhy   Ds   Rxr 2016.03.29 06:38
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!