双向一对多映射
    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());
}

菜鸡的Java笔记 - java 双向一对多映射的更多相关文章

  1. Java 笔记 —— java 和 javac

    Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...

  2. 菜鸡的Java笔记 - java 断言

    断言:assert (了解)        所谓的断言指的是在程序编写的过程之中,确定代码执行到某行之后数据一定是某个期待的内容        范例:观察断言 public class Abnorma ...

  3. 菜鸡的Java笔记 - java 正则表达式

    正则表达式 RegularExpression        了解正则表达式的好处        正则表达式的基础语法        正则表达式的具体操作            content (内容 ...

  4. 菜鸡的Java笔记 - java 线程常用操作方法

    线程常用操作方法        线程的命名操作,线程的休眠,线程的优先级            线程的所有操作方法几乎都在 Thread 类中定义好了            线程的命名和取得      ...

  5. 菜鸡的Java笔记 - java 访问控制权限

    java中四种访问控制权限的使用                内容            在java里面一共定义有四个权限,按照由小到大的顺序:private<defaule<prote ...

  6. 菜鸡的Java笔记 - java 常用类库

    CommonClassLibrary 常用类库        定时调度            定时调度指的是每到一个时刻,都会自动的产生某些特定的操作形式                    con ...

  7. 菜鸡的Java笔记 - java 反射机制

    反射机制        1.观察 Class 类的使用        2.利用反射改善工程设计模式        3.反射操作类结构            content (内容)        1. ...

  8. 菜鸡的Java笔记 java基础类库 BaseClassLibrary

    java基础类库 BaseClassLibrary        StringBuffer 类的特点        StringBuffer,StringBuilder,String 类之间的关系   ...

  9. 菜鸡的Java笔记 java数据库编程(JDBC)

    java数据库编程(JDBC)        介绍 JDBC 的基本功能            content (内容)        现在几乎所有的项目开发过程之中都不可能离开数据库,所以在java ...

随机推荐

  1. .Net Core 实现 自定义Http的Range输出实现断点续传或者分段下载

    一.Http的Range请求头,结合相应头Accept-Ranges.Content-Range 可以实现如下功能: 1.断点续传.用于下载文件被中断后,继续下载. 2.大文件指定区块下载,如视频.音 ...

  2. 数据结构与算法——克鲁斯卡尔(Kruskal)算法

    目录 应用场景-公交站问题 克鲁斯卡尔算法介绍 克鲁斯卡尔算法图解 克鲁斯卡尔算法分析 如何判断回路? 代码实现 无向图构建 克鲁斯卡尔算法实现 获取一个点的终点解释 应用场景-公交站问题 某城市新增 ...

  3. 第十二章 Net 5.0 快速开发框架 YC.Boilerplate --千万级数据处理解决方案

    在线文档:http://doc.yc-l.com/#/README 在线演示地址:http://yc.yc-l.com/#/login 源码github:https://github.com/linb ...

  4. 从零入门 Serverless | 使用 Spot 低成本运行 Job 任务

    作者 | 代志锋(云果)  阿里云技术专家 本文整理自<Serverless 技术公开课>,点击链接即可免费听课:https://developer.aliyun.com/learning ...

  5. Java语言程序设计与数据结构(基础篇)第七章答案

    答案为本人求解,如有错误,还望海涵.如有雷同,纯属巧合. 7.1 import java.util.Scanner; public class Main { public static void ma ...

  6. SpringCloud升级之路2020.0.x版-27.OpenFeign的生命周期-创建代理

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 接下来,我们开始分析 OpenFeign 的生命周期,结合 OpenFeign 本身的源代 ...

  7. 最详细的Android SDK下载安装及配置教程-------全文均为引用

    <https://www.cnblogs.com/gufengchen/p/11038029.html>

  8. js判断移动端浏览器类型,微信浏览器、支付宝小程序、微信小程序等

    起因 现在市场上各种跨平台开发方案百家争鸣各有千秋,个人认为最成熟的还是hybird方案,简单的说就是写H5各种嵌入,当然作为前端工程师最希望的也就是公司采用hybird方案当作技术路线. 所谓的hy ...

  9. cunda 常用命令,删除,创建,换源

    https://github.com/tensorflow/tensorflow/ conda create --name [虚拟环境名] python=3.7 创建一个环境 conda activa ...

  10. 浅谈如何爆踩TLEcoders

    对付一些速度比老奶奶都慢的评测姬, 除了超级小的常数,往往还不得不使用一些不算办法的办法 比如说这个让人无语的$ACcoders$的评测姬, 当我们感到代码已经无法再卡常的时候,对人生已经近乎绝望的时 ...