java-集合-realdo-集合一对多

Posted 小丑quan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java-集合-realdo-集合一对多相关的知识,希望对你有一定的参考价值。

 

 

school类:

package setdone;

import java.util.List;

public class School {
    private String schoolname;
    private List<Student> sch_list;

    public School(String schoolname) {
        this.schoolname = schoolname;
    }

    public String getSchoolname() {
        return schoolname;
    }

    public void setSchoolname(String schoolname) {
        this.schoolname = schoolname;
    }

    public List<Student> getSch_list() {
        return sch_list;
    }

    public void setSch_list(List<Student> sch_list) {
        this.sch_list = sch_list;
    }
}

 

student 类:

package setdone;

public class Student {
    private String name;
    private String school;
    private int age;

    public Student(String name,  int age) {
        this.name = name;
        this.school = school;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name=\'" + name + \'\\\'\' +
                ", school=\'" + school + \'\\\'\' +
                ", age=" + age +
                \'}\';
    }
}

test

package setdone;

import java.util.ArrayList;
import java.util.List;

public class SsTest {
    public static void main(String[] args) {
        Student student1 = new Student("张三",22);
        Student student2 = new Student("李四",42);
        Student student3 = new Student("王五",23);

        School school = new School("清华大学");

        student1.setSchool(school.getSchoolname());
        student2.setSchool(school.getSchoolname());
        student3.setSchool(school.getSchoolname());

        List<Student> sch_stu = new ArrayList<>();
        sch_stu.add(student1);
        sch_stu.add(student2);
        sch_stu.add(student3);

        school.setSch_list(sch_stu);
        System.out.println("学校名称:" + school.getSchoolname());
        for (Student student:school.getSch_list()) {
            System.out.println("学生" + student);
        }
    }


}

 

以上是关于java-集合-realdo-集合一对多的主要内容,如果未能解决你的问题,请参考以下文章

单向一对多 多读多

13mybatis一对多resultMap中用collection指定集合的封装规则

EF 5 一对多自引用 - 代码优先

MyBatis---集合查询(一对多)

查询具有两个一对多集合的休眠实体

一对多查询集合