用java写一个关联多个表单的parent_id
时间: 2024-05-15 08:15:26 浏览: 66
JAVA表单上传多个文件 java web form表单上传多个文件.zip_form表单post提交
假设有三个表单:student、class、school,其中student表单中的每个学生都属于一个class表单中的班级,而每个班级都属于一个school表单中的学校。因此,我们可以使用parent_id来关联这些表单。
在student表单中,我们可以添加一个parent_id列,用于存储该学生所属班级的class_id。同时,在class表单中也可以添加一个parent_id列,用于存储该班级所属学校的school_id。这样,我们就可以通过parent_id关联这三个表单,形成一个树形结构。
下面是一个示例代码:
```
public class Student {
private int student_id;
private int class_id;
// other fields and methods
public int getClassId() {
return class_id;
}
public void setClassId(int class_id) {
this.class_id = class_id;
}
}
public class Class {
private int class_id;
private int school_id;
// other fields and methods
public int getSchoolId() {
return school_id;
}
public void setSchoolId(int school_id) {
this.school_id = school_id;
}
}
public class School {
private int school_id;
// other fields and methods
}
public class Main {
public static void main(String[] args) {
School school = new School();
school.setSchoolId(1);
Class clazz = new Class();
clazz.setClassId(1);
clazz.setSchoolId(school.getSchoolId());
Student student = new Student();
student.setStudentId(1);
student.setClassId(clazz.getClassId());
// query school by student
int school_id = getClassById(student.getClassId()).getSchoolId();
School school = getSchoolById(school_id);
// do something with school
}
public static Class getClassById(int class_id) {
// query class by class_id
}
public static School getSchoolById(int school_id) {
// query school by school_id
}
}
```
在这个示例中,我们通过parent_id关联了三个表单,并且可以根据学生查询所属学校。当然,实际开发中可能需要更复杂的关联关系,但是基本的原理都是类似的。
阅读全文