11.映射

版权声明:原创文章,转载请声明原处 https://blog.csdn.net/qq_41723615/article/details/86295056

简单Java类在实际的开发中都是根据其数据表的定义来实现的, 在项目开发中有着无可替代的重要作用;

  • 一个部门有多个雇员;
  • 一个雇员有一个或零个领导。

• 部门表(dept):部门编号(deptno)、部门名称(dname)、部门位置(lac);
• 雇员表(emp): 雇员编号(empno) 、姓名(ename)、职位(job )、工资( sal ) 、佣金(comm)、领导编号(mgr)、部门编号( deptno)。

范例:代码实现。

//简单类映射
class Dept{
    private int deptno;
    private String dname;
    private String loc;
    public Dept(int deptno,String dname,String loc){
         this.deptno=deptno;
         this.dname=dname;
         this.loc=loc;
    }
    public String getInfo(){
        return "部门编号:"+this.deptno+",名称:"+this.dname+",位置:"+this.loc;
    }
}
class Emp{
    private int empno;
    private String ename;
    private String job;
    private double sal;
    private double comm;
    public Emp(int empno,String ename,String job,double sal,double comm){
            this.empno=empno;
            this.ename=ename;
            this.job=job;
            this.sal=sal;
            this.comm=comm;
    }
    public String getInfo(){
         return"雇员编号:"this.empno+",姓名:"+this.ename+",职位:"+this.job+",工资:"+this.sal+",佣金:"+this.comm;

    }
}
public class TestYingshe{
    public static void main(String args[]){
        //第一步:设置数据
        //1.产生给自的独立对象
           Dept dept=new Dept(10,"ACCOUNTING","New York");
           Emp ea=new Emp(7369,"SMITH","CLERK",800.0,0.0);
           Emp eb=new Emp(7309,"FORD","MANAGER",800.0,0.0);
           Emp ec=new Emp(7069,"KING","PRESIDENT",800.0,0.0);
           //2.设置雇员与领导关系
           ea.setMgr(eb);
           eb.setMgr(ec);
           //3.设置雇员和部门关系
           ea.setDept(dept);    //雇员与部门
           eb.setDept(dept);    //雇员与部门
           ec.setDept(dept);    //雇员与部门
           dept.setEmps(new Emp[]{ea,eb,ec});
           
           //第二步:取出数据
           //1.通过雇员找到领导信息和部门信息
           System.out.println(ea.getInfo());
           System.out.println("\t|-"+ea.getMgr().getInfo());
           System.out.println("\t|-"+ea.getDept().getInfo());
           //根据部门找到所有的雇员以及每个雇员的领导信息
           System.out.println("---------------------");
           System.out.println(dept.getInfo());
           for (int x=0;x<dept.length ;x++ )
           {
               System.out.println("\t|-"+dept.getEmps()[x].getInfo());
               if(dept.getEmps()[x].getMgr() !=null){
               System.out.println("\t|-"+dept.getEmps()[x].getMgr().getInfo());
               }

           }

    }
}

在本程序中可以发现Emp与Dept类之间存在如下引用关系定义。
• 一个雇员属于一个部门, 应该在雇员里面保存部门信息, 所以在Emp类中定义了一个dept属
性, 如果有部门则设置一个Dept类的实例化对象, 否则设置为null;
• 一个部门有多个雇员, 如果要描述多这个概念应该使用对象数组完成。所以在Dept 类中增加一
个Emp类的对象数组(Emp emps[]);
• 一个雇员有一个领导. 领导信息也就是雇员信息, 应该在Emp类中增加领导的向身关联(Emp
mgr)。
此时两个简单Java类已经可以完整地描述出数据表的结构定义, 随后将根据结构设置并取得数据,
要求可以完成如下信息输出。
• 可以根据一个雇员查询他所对应的领导信息和部门信息;
• 可以根据一个部门取出所有的雇员以及每个雇员的领导信息。

范例:一对多映射

//一对多映射
class Province{
    private int pid;
    private String name;
    //setter、getter、无参构造略
    public Province(int pid,String name){
         this.pid=pid;
         this.name=name;
    }
    private City cities[];
    public void setCities(City cities[]){
         this.cities=cities;
    }
    public City[] getCities(){
         return this.cities;
    }
    public String getInfo(){
         return "省份编号:"+this.pid+",名称:"+this.name;
    }
}
class City{
    private int cid;
    private String name;
    private Province province;
    //setter、getter、无参构造略
    public City(int cid,String name){
         this.cid=cid;
         this.name=name;
    }
    public void setProvince(Province province){
         this.Province=Province;
    }
    public Province getProvince(){
         return this.province;
    }
    public String getInfo(){
         return "城市编号:"+this.cid+",名称:"+this.name;
    }
}
public class TestYingshe1{
    public static void main(String args[]){
          //第一步:设置关系数据
          //1.先准备好给自独立的对象
          Province pro =new Province(1,"河北省");
          City c1=new City(1001,"唐山");
          City c2=new City(1002,"秦皇岛");
          City c3=new City(1003,"石家庄");
          //2.设置关系
          c1.setProvince(pro);    //一个城市属于一个省份
          c2.setProvince(pro);
          c3.setProvince(pro);    
          pro.setCities(new City[] {c1,c2,c3});   //一个省份有多个城市
          //第二步:取出关系数据
          System.out.println(c2.getProvince().getInfo());
    }
}

范例:双向一对多映射

//双向一对多映射
class Item{
    private int iid;
    private String name;
    private String note;
    private Subitem subitems[];    //设置关系
    private Product products[];
    public Item(int iid,String name,String note){
         this.iid=iid;
         this.name=name;
         this.note=note;
    }
    public void setSubitems(Subitem subitems[]){
          this.subitems=subitems;
    }
    public void setProduct(Product products[]){
          this.products=products;    
    }
    public Subitem[] getSubitems(){
       return this.subitems;
    }
    public Product[] getProduct(){
       return this.products;
    }
    
    public String getInfo(){
        return "栏目编号:"+this.iid+",名称:"+this.name+",描述:"+this.note;
    }
}
class Subitem  { //子栏目
    private int sid;
    private String name;
    private String note;
    private Item item;
    private Product products[];
    public Subitem(int sid,String name,String note){
         this.sid=sid;
         this.name=name;
         this.note=note;
    }
    public void setItem(Item item){
        this.item=item;
    }
    public Item getItem(){
        return this.item;
    }
    private void setProducts(Product products[]){
        this.products=products;
    }
    public Product[] getProducts(){
        return this.products;
    }
    public String getInfo(){
         return "子栏目编号:"+this.sid+",名称:"+this.name+",描述:"+this.note;       
    }

}
class Product{  //商品
      private int pid;
      private String name;
      private double price;
      private Item item;
      private Subitem subitem;
      public Product(int pid,String name,double price){
            this.pid=pid;
            this.name=name;
            this.price=price;
      }
      public void setItem(Item item){
           this.item=item;
      }
      public Item getItem(){
           return this.subitem;
      }
      public void setSubitem(Subitem subitem){
           this.subitem=subitem;
      }
      public Subitem getSubitem(){
         return this.subitem;
      }
      public String getInfo(){
           return "商品编号:"+this.pid+",名称:"+this.name+",价格:"+this.price;
           }
      
}
public class TestYingshe2{
     public static void main(String args[]){
         //第一步:设置数据关系
         //1.准备出单独的类对象
         Item item=new Item(1,"厨房用具","-");
         Subitem suba=new Subitem(1001,"厨具","-");
         Subitem subb=new Subitem(1002,"刀具","-");
         Subitem subc=new Subitem(1003,"餐具","-");
         Product proa=new Product(90001,"蒸锅",500.0);
         Product prob=new Product(90002,"炒锅",3000.0);
         Product proc=new Product(90003,"菜刀",1000.0);
         Product prod=new Product(90004,"水果刀",80.0);
         Product proe=new Product(90005,"青花瓷",10000.0);
         Product prof=new Product(90006,"水晶筷子",8000.0);
         //2.设置对象间的引用关系
         suba.setItem(item);
         subb.setItem(item);
         subc.setItem(item);
         item.setSubitems(new Subitem[]{suba,subb,subc});
         proa.setSubitem(suba);
         proa.setItem(item);
         prob.setSubitem(suba);
         prob.setSublitem(item);
         proc.setSubitem(subb);
         proc.setItem(item);
         prod.setSubitem(subb);
         prod.setItem(item);
         proe.setSubitem(subc);
         proe.setItem(item);
         prof.setSubitem(subc);
         prof.setItem(item);
         suba.setProducts(new Product[] {proa,prob});
         subb.setProducts(new Product[] {proc,prod});
         subc.setProducts(new Product[] {proe,prof});
         item.setProducts(new Product[] {proa,prob,proc,prod,proe,prof});
         //第二步:取出数据
         //1.通过一个类型找到对应的全部子类型
         System.out.println(item.getInfo());
         for (int x=0;x<item.getSubitems().length ;x++ )
         {
             System.out.println("\t|-"+item.getSubitems()[x].getInfo());

         }
         System.out.println("------------------------------");
         System.out.println(item.getInfo());
         for (int x=0;x<item.getProducts().length ;x++ )
         {
             System.out.println("\t|-"+item.getProducts()[x].getInfo());
             System.out.println("\t|-"+item.getProducts()[x].getSubitem().getInfo());
            
              
         }
             System.out.println("-----------------------------------");
             System.out.println(subb.getInfo());
             for(int x=0;x<subb.getProducts().length;x++){
                 System.out.println("\t|-"+subb.getProducts()[x].getInfo());
             }

     }
}

范例:多对多映射

//多对多映射(重要)
class Admin{
    private String aid;
    private String password;
    private Role role;
    public Admin(String aid;Strig password){
         this.aid=aid;
         this.password=password;
    }
    public void setRole(Role role){
         this.role=role;
    }
    public Role getRole(){
         return this.role;
    }
    public String getInfo(){
         return "管理员编号:"+this.aid+",密码:"+this.password;
    }

}
class Role{
    private int rid;
    private String title;
    private Admin admins[];
    private Group groups[];
    public Role(int rid,String title){
          this.rid=rid;
          thid.title=title;
    }
    public void setAdmins(Admin[] admins){
         this.admins=admins;
    }
    public Admin[] getAdmins(){
          return this.admins;
     }
     public void setGroups(Group groups[]){
           this.groups=groups;
     }
     public Group[] getGroups(){
          return this.groups;
     }
    public String getInfo(){
         return"角色编号:"+this.rid+",名称:"+this.title;
    }
}
class Group{
    private int gid;
    private String title;
    private Role roles[];4
    private Action actions[];
    public Group(int gid,String title){
          this.gid=gid;
          this.title=title;
    }
    public void setRoles(Role roles[]){
          this.roles=roles;
    }
    public void setActions(Action actions[]){
          this.actions=actions;
    }
    public Action[] getActions(){
          return this.actions;
    }
    public Role[] getRoles(){
        return this.roles;
    }
    public String getInfo(){
          return "权限组编号:"+this.gid+",名称:"+this.title;
    }
}
class Action{
    private int aid;
    private String title;
    private String url;
    private Group group;
    public Action(int aid,String title,String url){
            this.aid=aid;
            this.title=title;
            this.url=url;
    }
    public void setGroup(Group group){
           this.group=group;
    }
    public Group getGroup(){
         return this.group;
    }
    public String getInfo(){
          return "权限编号:"+this.aid+",名称:"+this.title+",路径:"+this.url;
    }
}
public class TestYingshe3{
    public static void main(String args[]){
         //第一步:设置完整关系
         //1.准备出若干个对象
         Admin a1=new Admin("admin","hello");
         Admin a2=new Admin("mldn","hello");
         Admin a3=new Admin("admin","hello");
         Role r1=new Role(1,"系统管理员");
         Role r2=new Role(2,"信息管理员");
         Group g1=new Group(10,"信息管理");
          Group g2=new Group(11,"用户管理");
           Group g3=new Group(12,"数据管理");
            Group g4=new Group(13,"接口管理");
             Group g5=new Group(14,"备份管理");
             Action at01=new Action(1001,"新闻发布","-");
             Action at02=new Action(1002,"新闻列表","-");
             Action at03=new Action(1003,"新闻审核","-");
             Action at04=new Action(1004,"增加用户","-");
             Action at05=new Action(1005,"用户列表","-");
             Action at06=new Action(1006,"登录日志","-");
             Action at07=new Action(1007,"雇员数据","-");
             Action at08=new Action(1008,"部门数据","-");
             Action at09=new Action(1009,"公司数据","-");
             Action at10=new Action(10010,"服务传输","-");
             Action at11=new Action(10011,"短信平台","-");
             Action at12=new Action(10012,"全部备份","-");
             Action at13=new Action(10013,"局部备份","-");
             //2.要设置这些对象之间的基本关系
             //设置管理员与角色
             a1.setRole(r1);
             a2.setRole(r2);
             a3.setRole(r2);
             r1.setAdmins(new Admin[] {a1});
             r2.setAdmins(new Admin[] {a2});
             //设置角色与管理员组
             r1.setGroups(new Group[] {g1,g2,g3,g4,g5});
             r2.setGroups(new Group[] {g1,g2});
             g1.setRoles(new Role[] {r1,r2});
              g2.setRoles(new Role[] {r1,r2});
               g3.setRoles(new Role[] {r1});
                g4.setRoles(new Role[] {r1});
                 g5.setRoles(new Role[] {r1});
                 //设置管理员组与权限
                 g1.setActions(new Action[]{ato1,ato2,ato3});
                 g2.setActions(new Action[]{ato4,ato5,ato6});
                 g3.setActions(new Action[]{ato7,ato8,ato9});
                 g4.setActions(new Action[]{ato10,ato11});
                 g5.setActions(new Action[]{ato12,ato13});
                 ato1.setGroup(g1);
                 ato2.setGroup(g1);
                 ato3.setGroup(g1);
                 ato4.setGroup(g2);
                 ato5.setGroup(g2);
                 ato6.setGroup(g2);
                 ato7.setGroup(g3);
                 ato8.setGroup(g3);
                 ato9.setGroup(g3);
                 at10.setGroup(g4);
                 at11.setGroup(g4);
                 at12.setGroup(g5);
                 at13.setGroup(g5);
                 //第二步:取出数据
                 System.out.println(a1.getInfo());
                 System.out.println("\t|-"+a1.getRole().getInfo());
                 for (int x=0;x<a1.getRole().getInfo().lenght ;x++ )
                 {
                         System.out.println("\t\t|-"+a1.getRole().getGroups()[x].getInfo());
                         for (int y=0;y<a1.getRole().getGroup()[x].getActions().length ;y++ )
                         {
                                   System.out.println("\t\t\t|-"+a1.getRole().getGroups()[x].getActions()[y].getInfo());
                         }
                 }
                 System.out.println("-----------------------------");
                 System.out.println(g2.getInfo());
                 for (int x=0;x<g2.getRoles().length ;x++ )
                 {
                     System.out.println("\t|-"+g2.getRole()[x].getInfo());
                     for (int y=0;y<g2.getRoles()[x].getAdmins().length ;y++ )
                     {
                         System.out.println("\t\t|-"+g2.getRoles()[x].getAdmins()[y].getInfo());
                     }
                 }
                 

    }
}

 

猜你喜欢

转载自blog.csdn.net/qq_41723615/article/details/86295056