zoukankan      html  css  js  c++  java
  • 菜鸡的Java笔记

        双向一对多映射
        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("	|-【参与用户】 "+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("	|-【参与用户】 "+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("	|-【参与用户】 "+c.getlnfo()+",考试成绩"+uct[x].getScore());
            }
  • 相关阅读:
    [LeetCode] Meeting Rooms I & II
    [LeetCode] Graph Valid Tree
    [LeetCode] Palindrome Permutation I & II
    [LeetCode] Encode and Decode Strings
    [LeetCode] Paint Fence
    android 发送短信功能
    andrioid 分享到其它(短信,qq,微信等功能)
    android Dialog实例
    android开发之onCreate( )方法详解
    android edittext属性说明
  • 原文地址:https://www.cnblogs.com/mysterious-killer/p/10140721.html
Copyright © 2011-2022 走看看