美文网首页
JDBC从入门到放弃(四)

JDBC从入门到放弃(四)

作者: 秀逼 | 来源:发表于2017-10-28 23:57 被阅读0次

本节将为大家介绍ORM模型

ORM:对象关系映射模型,也就是把对象映射到数据库中

其中,类映射为数据库中的表,类的成员变量映射为表中的字段,类的OID映射为表的主键,类的实体映射为表中的记录,而类之间的关联关系映射为表中的外键。这也是ORM的核心所在。

ORM示例:

下面,以订单 ----> 订单明细为例,介绍ORM的简单使用

首先,在oracle数据库中,分别创建订单表,以及与之关联的订单明细表。同时,创建两张表的序列。


创建订单表
SQL> CREATE TABLE t_order(
  2  id number(10) primary key,
  3  ordered_date date not null,
  4  shipped_date date,
  5  total number(10,2)
  6  );


创建订单明细表
SQL> CREATE TABLE t_orderline(
  2  id number(10) primary key,
  3  price number(10,2),
  4  quantity number(10),
  5  product varchar2(30),
  6  order_id number(10) references t_order(id)
  7  );

创建序列
SQL> CREATE SEQUENCE t_order_seq
  2  start with 1
  3  increment by 1;

序列已创建。

SQL> CREATE SEQUENCE t_orderline_seq
  2  start with 1
  3  increment by 1;

序列已创建。

然后,在POJO包中,创建与两张表相对应的POJO类,这里我们分别命名为Order类和Orderline类。

订单Order类:
package com.iotek.pojo;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

public class Order implements Serializable {
    
    private Long id;
    private Date orderedDate;
    private Date shippedDate;
    private Double total;
    private List<Orderline> orderlines = new ArrayList<Orderline>();
    
    public Order() {}

    public Order(Long id, Date orderedDate, Date shippedDate, Double total, List<Orderline> orderlines) {
        super();
        this.id = id;
        this.orderedDate = orderedDate;
        this.shippedDate = shippedDate;
        this.total = total;
        this.orderlines = orderlines;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getOrderedDate() {
        return orderedDate;
    }

    public void setOrderedDate(Date orderedDate) {
        this.orderedDate = orderedDate;
    }

    public Date getShippedDate() {
        return shippedDate;
    }

    public void setShippedDate(Date shippedDate) {
        this.shippedDate = shippedDate;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public List<Orderline> getOrderlines() {
        return orderlines;
    }

    public void setOrderlines(List<Orderline> orderlines) {
        this.orderlines = orderlines;
    }

    @Override
    public String toString() {
        return "Order [id=" + id + ", orderedDate=" + orderedDate + ", shippedDate=" + shippedDate + ", total=" + total
                + ", orderlines=" + orderlines + "]";
    }
    
    
    
}
订单明细:Orderline类
package com.iotek.pojo;

import java.io.Serializable;

public class Orderline implements Serializable {
    private Long id;
    private Double price;
    private Long quantity;
    private String product;
    private Order order;
    
    public Orderline() {}

    public Orderline(Long id, Double price, Long quantity, String product, Order order) {
        super();
        this.id = id;
        this.price = price;
        this.quantity = quantity;
        this.product = product;
        this.order = order;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Long getQuantity() {
        return quantity;
    }

    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public String toString() {
        return "Orderline [id=" + id + ", price=" + price + ", quantity=" + quantity + ", product=" + product
                + ", order=" + order + "]";
    }
    
    
}


相关文章

网友评论

      本文标题:JDBC从入门到放弃(四)

      本文链接:https://www.haomeiwen.com/subject/tnjepxtx.html