数据表与简单java类的映射转换

基本操作:先抛开所有的关联字段,建立基本类,再引用关联表示关联关系

一对多映射

按照表的要求将表的结构转换为类的结构,同时获得如下信息:

  • 获取一个分类的信息
  • 可以根据分类获取对应的所有子分类的信息

package dataExcl;
class Item{
    private long iid;
    private String title;
    private Subitem subitems[];

    public Item(long iid, String title) {
        this.iid = iid;
        this.title = title;
    }

    public long getIid() {
        return iid;
    }

    public void setIid(long iid) {
        this.iid = iid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Subitem[] getSubitems() {
        return subitems;
    }

    public void setSubitems(Subitem[] subitems) {
        this.subitems = subitems;
    }

    public String getInfo(){
        return "分类信息:iid:"+this.iid+" title:"+this.title;
    }
}

class Subitem{
    private long sid ;
    private String title ;
    private Item items;

    public Subitem(long sid, String title) {
        this.sid = sid;
        this.title = title;
    }

    public long getSid() {
        return sid;
    }

    public void setSid(long sid) {
        this.sid = sid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Item getItems() {
        return items;
    }

    public void setItems(Item items) {
        this.items = items;
    }
        public String getInfo(){
            return "子分类信息:sid:"+this.sid+" title:"+this.title;
        }
}
public class JavaDemo {
    public static void main(String[] args) {
        //第一步:根据结构设置对象
        Item item = new Item(1L,"图书");
        Subitem subitem [] = new Subitem[]{
                new Subitem(10L,"编程图书"),
                new Subitem(10L,"图形图书")
        };
        item.setSubitems(subitem);
        for(int i=0;i<subitem.length;i++){
            subitem[i].setItems(item);
        }
        //第二部:根据要求获取数据
        System.out.println(item.getInfo());
        for(int i=0;i<subitem.length;i++){
            System.out.println(subitem[i].getInfo());
        }
    }
}
package dataExcl;
class Item{
    private long iid;
    private String title;
    private Subitem subitems[];

    public Item(long iid, String title) {
        this.iid = iid;
        this.title = title;
    }

    public long getIid() {
        return iid;
    }

    public void setIid(long iid) {
        this.iid = iid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Subitem[] getSubitems() {
        return subitems;
    }

    public void setSubitems(Subitem[] subitems) {
        this.subitems = subitems;
    }
}

class Subitem{
    private long sid ;
    private String title ;
    private Item items;

    public Subitem(long sid, String title) {
        this.sid = sid;
        this.title = title;
    }

    public long getSid() {
        return sid;
    }

    public void setSid(long sid) {
        this.sid = sid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Item getItems() {
        return items;
    }

    public void setItems(Item items) {
        this.items = items;
    }
}
public class JavaDemo {
    public static void main(String[] args) {
        //第一步:根据结构设置对象
        Item item = new Item(1L,"图书");
        Subitem subitem [] = new Subitem[]{
                new Subitem(10L,"编程图书"),
                new Subitem(10L,"图形图书")
        };
        item.setSubitems(subitem);
        for(int i=0;i<subitem.length;i++){
            System.out.println(subitem[i].getSid()+subitem[i].getTitle());
        }
        //第二部:根据要求获取数据

    }
}

多对多关系映射

将以上结构转换为类结构:

  • 获取一个用户访问的所有的商品信息
  • 获取一个商品被浏览过的所有信息

package dataExcl;
class User{
private long uid;
private String uname;
private Product product[];

public User(long uid, String uname) {
this.uid = uid;
this.uname = uname;
}

public Product[] getProduct() {
return product;
}

public void setProduct(Product[] product) {
this.product = product;
}

public String getInfo(){
return "[用户信息] 用户id:"+this.uid+"、用户id:"+this.uname;
}
}
class Product{
private long pnum;
private String pname;
private double price;
private User user[];

public Product(long pnum, String pname, double price) {
this.pnum = pnum;
this.pname = pname;
this.price = price;
}

public String getInfo(){
return "[商品信息] 商品编号:"+this.pnum+"、商品名称:"+this.pname+"、商品价格"+this.price;
}

public User[] getUser() {
return user;
}

public void setUser(User[] user) {
this.user = user;
}
}

public class JavaDemo {
public static void main(String[] args) {
//第一步:根据结构设置对象
User userA = new User(1L,"张三");
User userB = new User(2L,"李四");
User userC = new User(3L,"王五");

Product productA = new Product(1,"耳机",378.5);
Product productB = new Product(2,"电脑",6300.5);
Product productC = new Product(3,"音响",400);

userA.setProduct(new Product[]{productA,productB,productC});
userB.setProduct(new Product[]{productA,productB});
userC.setProduct(new Product[]{productA});

productA.setUser(new User[]{userA,userB,userC});
productB.setUser(new User[]{userA,userB});
productC.setUser(new User[]{userA});
//第二步:根据要求获取数据

System.out.println("------------------------根据用户查询访问信息----------------------------------");
System.out.println(userA.getInfo());
for (int i=0;i<userA.getProduct().length;i++){
System.out.println(userA.getProduct()[i].getInfo());
}
}
}