双向一对多映射
    two-way

    开发要求:
        根据数据表的结构进行简单java类的转换:
        要求实现如下的输出信息:
            可以根据课程取得全部参与此课程用户的信息
                输出课程信息:
                输出参与此课程用户的信息以及考试成绩
            用户可以取得自己所参加的课程信息
                输出某一个用户的信息
                输出该用户所参加的所有课程信息以及对应的考试成绩
        关系上来讲:一个用户可以参加多门课程,一门课程可以有多个用户参加,每个用户在每个课程内都会有一个成绩
        此时最麻烦的问题在于用户-课程关系表中除了关联字段之外,还包含有其他字段,这样的表一定要作为一个实体类出现
        所以现在需要定义有三个类
        第一步:先完成基本字段

class User{
private String userid:
private String name:
public User(String userid,String name){
this.userid = userid:
this.name = name:
}
public String getlnfo(){
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 getlnfo(){
return "课程编号:"+this.cid
+",名称:"+this.title
+",课时:"+this.num
+",简介:"+this.note:
}
}
public class TwoWay{
public static void main(String args[]){ }
}

第二步:进行字段关联的时候都是以外键为主
            为了可以进行关联,需要引入一个新的类:要保存用户,课程等信息的联系

class User{
private String userid;
private String name;
public User(String userid,String name){
this.userid = userid;
this.name = name;
}
public String getlnfo(){
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 getlnfo(){
return "课程编号:"+this.cid
+",名称:"+this.title
+",课时:"+this.num
+",简介:"+this.note;
}
public Course getCourse(){
return this.course;
}
public User getUser(){
return this.user;
}
}
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 class TwoWay{
public static void main(String args[]){ }
}

第三步:程序测试

class User{
private String userid;
private String name;
private 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 getlnfo(){
return "用户编号:"+this.userid
+",姓名"+this.name;
}
}
class Course{
private int cid;
private String title;
private int num;
private String note;
private 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 void setUcs(UserCourse ucs[]){
this.ucs = ucs;
}
public UserCourse[] getUcs(){
return this.ucs;
}
public String getlnfo(){
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 double getScore(){
return this.score;
}
public Course getCourse(){
return this.course;
}
public User getUser(){
return this.user;
}
}
public class TwoWay{
public static void main(String args[]){
//第一步:设置类与类之间的关系
//1.定义单独的类对象
User ua = new User("zhangsan","张三");
User ub = new User("lisi","李四");
User uc = new User("wangwu","王五");
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,"暂无评价",91.0);
UserCourse ucc = new UserCourse(ub,c1,"暂无评价",92.0);
UserCourse ucd = new UserCourse(uc,c1,"暂无评价",93.0);
UserCourse uce = new UserCourse(uc,c2,"暂无评价",94.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.getlnfo()); // 输出一个课程信息
for(int x = 0;x<c1.getUcs().length;x++){ // 该门课程的用户信息
System.out.println("\t|-【参与用户】 "+c1.getUcs()[x].getUser().getlnfo()+",考试成绩"+c1.getUcs()[x].getScore());
}
System.out.println("*******************************************");
System.out.println(ua.getlnfo());
for(int x = 0;x<ua.getUcs().length;x++){// 都是UserCourse对象
System.out.println("\t|-【参与用户】 "+ua.getUcs()[x].getCourse().getlnfo()+",考试成绩"+ua.getUcs()[x].getScore());
}
}
}

本程序与之前的代码相比,唯一麻烦的地方在于中间的关系表上的其他字段
            
            代码链是本次讲解的重点所在
            
            不晕的方法(笨方法容易理解的方法)

    System.out.println(ua.getlnfo());
UserCourse uct[] = ua.getUcs();
for(int x = 0;x<uct.length;x++){// 都是UserCourse对象
Course c = uct[x].getCourse();
System.out.println("\t|-【参与用户】 "+c.getlnfo()+",考试成绩"+uct[x].getScore());
}

最新文章

  1. MVC之前的那点事儿系列(8):UrlRouting的理解
  2. PN结的形成
  3. js设置自动刷新
  4. 实现鼠标拖动canvas绘制的图片
  5. css默认值汇总
  6. NSThread 多线程相关
  7. 手机新闻网站,手持移动新闻,手机报client,jQuery Mobile手机新闻网站,手机新闻网站demo,新闻阅读器开发
  8. Python简要学习笔记
  9. VB6之截图
  10. 浅试 Webview 一app 加载 H5小游戏
  11. MVVM 框架解析之双向绑定
  12. ArcGIS中的坐标系:基本概念和常用操作(一)
  13. windows下vbs脚本隐藏控制台
  14. 64位的pyd报&quot;ImportError: No module named&quot;错误
  15. ssh架构之hibernate(二)进阶学习
  16. Docker介绍-hc课堂笔记
  17. HTML|CSS之HTML常用标签
  18. 迷你MVVM框架 avalonjs 学习教程10、样式操作
  19. Vivado安装教程
  20. oracle 创建表 外键约束

热门文章

  1. 数值计算:四阶龙格-库塔法 for 二阶微分方程
  2. mysql面试题及答案,mysql最新面试题,mysql面试题大全汇总
  3. 洛谷4400 BlueMary的旅行(分层图+最大流)
  4. md5验证文件上传,确保信息传输完整一致
  5. Catch That Cow 经典广搜
  6. 高效动画实现原理-Jetpack Compose 初探索
  7. 《python编程:从入门到实践》课后习题及答案
  8. Vue el 使用el-checkbox-group复选框进行单选框操作
  9. TDengine在数益工联工业物联采集平台建设中的初步实践
  10. (一)、Docker 简介