双向一对多映射
    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. Hyper-V CPU设置

    前言 最近在用Hyper-V测试项目,发现在运行过程中发现项目总数崩掉,几经发现有一个共性,CPU占用率100%,分析问题发现问题出在Hyper-V CPU设置上,Hyper-V装系统就不赘述了,网上 ...

  2. FastAPI小项目实战:电影列表(Vue3 + FastAPI)

    假期过半, FastAPI + Vue3项目实战 视频也算录完了,尽管项目简单(2张表 共7个接口 4个页面) 起因 在6月底的时候开始录制了FastAPI官方文档中的新手教程部分(实际还没有官网文档 ...

  3. MySQL8.0.20安装教程,MySQL8.0.20安装详细图文教程

    1.下载链接如下: MySQL8.0.20版本 https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-20.html 其他版本:MySQL8 ...

  4. 利用水文分析方法提取山脊线和山谷线(ArcPy实现)

    一.背景 作为地形特征线的山脊线.山谷线对地形.地貌具有一定的控制作用.它们与山顶点.谷底点以及鞍部点等一起构成了地形起伏变化的骨架结构.同时由于山脊线具有分水性,山谷线具有合水性特征,使得它们在地形 ...

  5. python中\t、\n含义

    \t :代表着四个空格也就是一个tab \n:代表着换行

  6. Redis使用过程中有哪些注意事项?看看BAT这类的公司是正确使用Redis的!!

    Redis使用过程中要注意的事项 Redis使用起来很简单,但是在实际应用过程中,一定会碰到一些比较麻烦的问题,常见的问题有 redis和数据库数据的一致性 缓存雪崩 缓存穿透 热点数据发现 下面逐一 ...

  7. Vue2源码解读 - 响应式原理及简单实现

    直接进入主题了,想必大家都知道实现vue响应式核心方法就是 Object.defineProperty,那就从它开始说 Object.defineProperty 缺点: 深度监听,需要递归到底,一次 ...

  8. netty系列之:netty对http2消息的封装

    目录 简介 http2消息的结构 netty对http2的封装 Http2Stream Http2Frame 总结 简介 无论是什么协议,如果要真正被使用的话,需要将该协议转换成为对应的语言才好真正的 ...

  9. 关于java socket中的read方法阻塞问题

    客户端: public class TCPClient { public static void main(String[] args) throws IOException { FileInputS ...

  10. Coursera Deep Learning笔记 逻辑回归典型的训练过程

    Deep Learning 用逻辑回归训练图片的典型步骤. 笔记摘自:https://xienaoban.github.io/posts/59595.html 1. 处理数据 1.1 向量化(Vect ...