综合实战:双向一对多映射(用户-课程-考试成绩)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5f044a05508dd218bd217c947ed54df4.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3beaa8d233a2fb17c2660dc857876dbc.png)
关系上来讲:一个用户可以参加多门课程,一门课程可以有多个用户参加,每个用户每门课程都会有一个成绩。此时最麻烦的问题在于用户-课程关系表中除了关联字段之外,还包含有其他字段,这样的表一定要作为一个实体类出现。所以现在需要定义三个类。
第一步:完成基本字段的定义
class User{
private String userid;
private String name;
public User(String userid,String name){
this.userid=userid;
this.name=name;
}
public String getInfo(){
return "用户编号:"+this.userid+",姓名:"+this.name;
}
}
class Course{
private int cid;
private String title;
private int num;
private String note;
public Course(int cid,String title,int num,String note){
this.cid=cid;
this.title=title;
this.num=num;
this.note=note;
}
public String getInfo(){
return "课程编号:"+this.cid+",名称:"+this.title+",课时:"+this.num+",课程评价:"+this.note;
}
}
第二步:进行字符串关联的时候都是以外建为主
为了可以进行关联,需要引入一个新的类:要保存用户、课程等信息的联系。
class User{
private String userid;
private String name;
public UserCourse ucs[];
public User(String userid,String name){
this.userid=userid;
this.name=name;
}
public void setUcs(UserCourse ucs[]){
this.ucs=ucs;
}
public UserCourse[] getUcs(){
return this.ucs;
}
public String getInfo(){
return "用户编号:"+this.userid+",姓名:"+this.name;
}
}
class Course{
private int cid;
private String title;
private int num;
private String note;
public UserCourse ucs[];
public Course(int cid,String title,int num,String note){
this.cid=cid;
this.title=title;
this.num=num;
this.note=note;
}
public double getScore(){
return score;
}
public void setUcs(UserCourse ucs[]){
this.ucs=ucs;
}
public UserCourse[] getUcs(){
return this.ucs;
}
public String getInfo(){
return "课程编号:"+this.cid+",名称:"+this.title+",课时:"+this.num+",课程评价:"+this.note;
}
}
class UserCourse{
private User user;
private Course course;
private String note;
private double score;
public UserCourse(User user,Course course,String note,double score){
this.user=user;
this.course=course;
this.note=note;
this.score=score;
}
public Course getCourse(){
return this.course;
}
public User getUser(){
return this.user;
}
}
第三步:程序测试
public class Hello {
public static void main(String args[]){
//第一步:设置类与类之间的关系
//1.定义单独的类对象
User ua=new User("xiaoman","小满");
User ub=new User("pangpang","胖胖");
User uc=new User("huahua","花花");
Course c1=new Course(1,"Oracle",50,"-");
Course c2=new Course(2,"Java",300,"-");
//2. 设置彼此的关系
UserCourse uca=new UserCourse(ua,c1,"暂无评价",90.0);
UserCourse ucb=new UserCourse(ua,c2,"暂无评价",89.0);
UserCourse ucc=new UserCourse(ub,c1,"暂无评价",70.0);
UserCourse ucd=new UserCourse(uc,c1,"暂无评价",76.0);
UserCourse uce=new UserCourse(uc,c2,"暂无评价",79.0);
//在用户中设置关系
ua.setUcs(new UserCourse[]{uca,ucb});
ub.setUcs(new UserCourse[]{ucc});
uc.setUcs(new UserCourse[]{ucd,uce});
c1.setUcs(new UserCourse[]{uca,ucc,ucd});
c2.setUcs(new UserCourse[]{ucb,uce});
//第二步:取得数据
System.out.println(c1.getInfo());//输出一个课程信息
for(int x=0;x<c1.getUcs().length;x++){//该门课程的用户信息
System.out.println("\t|-【参与用户】"+c1.getUcs()[x].getUser().getInfo()+",考试成绩:"+c1.getUcs()[x].getScore());
}
System.out.println("=====================================================");
System.out.println(ua.getInfo());
for(int x=0;x<ua.getUcs().length;x++){
System.out.println("\t|-【参与课程】"+ua.getUcs()[x].getCourse().getInfo()+",考试成绩:"+ua.getUcs()[x].getScore());
}
}
}
本程序与之前的代码相比,唯一麻烦的地方在于中间的关系表上有其他字段。