简体   繁体   English

建立三个表之间的关系

[英]Establishing relationship between three tables

I am building Demo application in which we have three tables and i am using spring-boot data Jpa with mysql.我正在构建演示应用程序,其中我们有三个表,并且我正在使用 spring-boot 数据 Jpa 和 mysql。 I have following requirement我有以下要求

Table1 name->Student表1名称->学生

sid sname srole //columns name sid sname srole //列名

Table2 name->Courses表2名称->课程

cid cname ctime //columns name cid cname ctime //列名

Table3 name-> Tutioncenter表3名称-> Tutioncenter

id Name sid(student table sid) cid(Courses table cid) id 名称 sid(学生表 sid) cid(课程表 cid)

@Entity
public class Student{

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long sid;
  private String sname;
  private String srole;

  //getter and setter
}

@Entity
public class Courses{

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long cid;
  private String cname;
  private String ctimme;

  //getter and setter
}

@Entity
public class Tutioncenter{

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;

  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JoinColumn(name = "sid", nullable = false)
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JsonIgnore
  private Student student;

  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JoinColumn(name = "cid", nullable = false)
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JsonIgnore
  private Courses courses;

  //getter ans setter
}
@Repository
public interface CoursesRepository extends JpaRepository<Courses,Long>{}

@Repository
public interface StudentRepository extends JpaRepostiroy<Student,Long>{}

@Repository
public interface TutionRepository extends JpaRepository<Tutioncenter,Long>{

  Page<SecretManager> findBySIdAndCId(Long sId, Pageable pageable);
    
  Optional<SecretManager> findBySecretIdAndSIdAndCId(Long sid, Long cid);

}

Now I can design controller for Student and Courses but how should i design controller of Tutioncenter for crud operations??现在我可以为学生和课程设计 controller 但是我应该如何设计 Tutioncenter 的 controller 来进行 crud 操作?

The create, read, update operations of both student and courses table should be no relationship with tutioncenter table. student表和courses表的create, read, update操作都应该与 tutioncenter 表没有关系。 Then we only need to consider how to deal with delete operation.那么我们只需要考虑如何处理delete操作。 Considering data in tutioncenter table, there would be 2 opotions:考虑到 tutioncenter 表中的数据,将有 2 个选项:

  1. Delete all related data in tutioncenter table at the same time.同时删除tutioncenter表中所有相关数据。
  2. Do not delete related data, or do not delete imediaterly.不要删除相关数据,也不要立即删除。

The following are the SQL of delete both table together:以下是同时删除两个表的 SQL:

delete c, t from `courses` c join `tutioncenter` t on c.cid = t.cid where id = 1;

For tutioncenter table, I think there may be no update operation according to your business.对于tutioncenter表,我认为根据您的业务可能没有更新操作。 Therefore we only need to focus on create, read, delete operations.因此我们只需要关注create, read, delete操作。

read operation is as following:读操作如下:

select s.sname, c.cname, c.ctime from `tutioncenter` t join `student` s on s.sid = t.sid join `courses` c on c.cid = t.cid;

delete operation is as following:删除操作如下:

delete t from `tutioncenter` t join `student` s on s.sid = t.sid join `courses` c on c.cid = t.cid where t.sid = 1 and t.cid = 2;

insert operation as following:插入操作如下:

insert into `tutioncenter` (name, sid, cid)
select 'test3', sid, cid from `student` s, `courses` c where s.sid = 1 and c.cid = 1;

Back to how to design your controller for TutionRepository , I think that should base on how to design your business.回到如何为 TutionRepository 设计您的TutionRepository ,我认为这应该基于如何设计您的业务。 For example, If this demo system is designed only for student and administrator, then you need to consider both situations.例如,如果这个演示系统是为学生和管理员设计的,那么你需要考虑这两种情况。

The following are what I could give till now how a student use your system:以下是到目前为止我可以提供的学生如何使用您的系统的内容:

  1. Log in. With that said, all operation will have a sid now.登录。话虽如此,现在所有操作都会有一个 sid。 Query student.查询学生。
  2. List all chozen courses.列出所有选择的课程。 Query all courses have been choozen by this student.查询该学生已选择的所有课程。
  3. If add new course, get all valid courses, then add chozen course.如果添加新课程,获取所有有效课程,然后添加选择课程。 Query all courses operation.查询所有课程操作。 Add courses for this student.为该学生添加课程。
  4. Or delete chozen courses.或删除选择的课程。 Delete chozen courses.删除选择的课程。

To sum up, choosing courses system for student need the following interface:综上所述,学生选课系统需要如下界面:

  1. Query student by name or id.按姓名或身份证查询学生。 url may be GET /students/<sid> or GET /students/<sname> . url 可能是GET /students/<sid>GET /students/<sname> If you have login interface, you could use it instead.如果你有登录界面,你可以用它来代替。
  2. Query all valid courses have been choozen by this student's id.查询该学生id选择过的所有有效课程。 GET /students/<sid>/courses
  3. Query all valid courses.查询所有有效课程。 GET /courses
  4. Add courses for this student (courses ids and student id).为该学生添加课程(课程 ID 和学生 ID)。 POST /students/<sid>/courses body is cid list like [cid1, cid2] POST /students/<sid>/courses正文是 cid 列表,如[cid1, cid2]
  5. Delete courses for this student (courses ids and student id).删除该学生的课程(课程 ID 和学生 ID)。 DELETE /students/<sid>/courses/<cid>

The same to analysis what interfaces needed if you want to add a administrator role, who can manage courses and students.如果要添加管理员角色,谁可以管理课程和学生,则分析需要哪些接口也是如此。

Anyway, all should begin from business requirements.无论如何,一切都应该从业务需求开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM