订单和订单详情的一对一 ,一对多映射

我们知道,订单和订单详情是一对多的关系,所以在订单Bean(OrderBean)中包含一个订单详情Bean(OrderDetailBean)的集合.

开发步骤:

1、先建好OrderDetailBean

  1 package com.november.query.zuoye1;
  2 
  3 import java.sql.Date;
  4 
  5 public class OrderDetailBean {
  6 
  7     //订单详情实体
  8     private int orderDetailId;
  9     private int orderId;
 10     private int goodsId;
 11     private int orderAmount;
 12     private String orderMoneyPayType;
 13     private Date orderPayTime;
 14     private int isReview;
 15     
 16     
 17     public OrderDetailBean() {
 18         super();
 19     }
 20 
 21 
 22     public OrderDetailBean(int orderDetailId, int orderId, int goodsId,
 23             int orderAmount, String orderMoneyPayType, Date orderPayTime,
 24             int isReview) {
 25         super();
 26         this.orderDetailId = orderDetailId;
 27         this.orderId = orderId;
 28         this.goodsId = goodsId;
 29         this.orderAmount = orderAmount;
 30         this.orderMoneyPayType = orderMoneyPayType;
 31         this.orderPayTime = orderPayTime;
 32         this.isReview = isReview;
 33     }
 34 
 35 
 36     public int getOrderDetailId() {
 37         return orderDetailId;
 38     }
 39 
 40 
 41     public void setOrderDetailId(int orderDetailId) {
 42         this.orderDetailId = orderDetailId;
 43     }
 44 
 45 
 46     public int getOrderId() {
 47         return orderId;
 48     }
 49 
 50 
 51     public void setOrderId(int orderId) {
 52         this.orderId = orderId;
 53     }
 54 
 55 
 56     public int getGoodsId() {
 57         return goodsId;
 58     }
 59 
 60 
 61     public void setGoodsId(int goodsId) {
 62         this.goodsId = goodsId;
 63     }
 64 
 65 
 66     public int getOrderAmount() {
 67         return orderAmount;
 68     }
 69 
 70 
 71     public void setOrderAmount(int orderAmount) {
 72         this.orderAmount = orderAmount;
 73     }
 74 
 75 
 76     public String getOrderMoneyPayType() {
 77         return orderMoneyPayType;
 78     }
 79 
 80 
 81     public void setOrderMoneyPayType(String orderMoneyPayType) {
 82         this.orderMoneyPayType = orderMoneyPayType;
 83     }
 84 
 85 
 86     public Date getOrderPayTime() {
 87         return orderPayTime;
 88     }
 89 
 90 
 91     public void setOrderPayTime(Date orderPayTime) {
 92         this.orderPayTime = orderPayTime;
 93     }
 94 
 95 
 96     public int getIsReview() {
 97         return isReview;
 98     }
 99 
100 
101     public void setIsReview(int isReview) {
102         this.isReview = isReview;
103     }
104 
105 
106     @Override
107     public String toString() {
108         return "OrderDetialBean [orderDetailId=" + orderDetailId + ", orderId="
109                 + orderId + ", goodsId=" + goodsId + ", orderAmount="
110                 + orderAmount + ", orderMoneyPayType=" + orderMoneyPayType
111                 + ", orderPayTime=" + orderPayTime + ", isReview=" + isReview
112                 + "]";
113     }
114     
115     
116 
117 }

2、建好OrderBean,里面放一个OrderDetailBean的集合

  1 package com.november.query.zuoye1;
  2 
  3 import java.sql.Date;
  4 import java.util.List;
  5 
  6 //订单实体
  7 public class OrderBean {
  8 
  9     private int orderId;
 10     private int orderNo;
 11     private String userName;
 12     private int orderState;
 13     private int addressId;
 14     private Date orderTime;
 15     
 16     //订单对应多个订单详情 一对多的关系,所以维护一个 集合对象
 17     List orderDetail;
 18 
 19     public OrderBean() {
 20         super();
 21     }
 22 
 23     public OrderBean(int orderId, int orderNo, String userName, int orderState,
 24             int addressId, Date orderTime, List orderDetail) {
 25         super();
 26         this.orderId = orderId;
 27         this.orderNo = orderNo;
 28         this.userName = userName;
 29         this.orderState = orderState;
 30         this.addressId = addressId;
 31         this.orderTime = orderTime;
 32         this.orderDetail = orderDetail;
 33     }
 34 
 35     public int getOrderId() {
 36         return orderId;
 37     }
 38 
 39     public void setOrderId(int orderId) {
 40         this.orderId = orderId;
 41     }
 42 
 43     public int getOrderNo() {
 44         return orderNo;
 45     }
 46 
 47     public void setOrderNo(int orderNo) {
 48         this.orderNo = orderNo;
 49     }
 50 
 51     public String getUserName() {
 52         return userName;
 53     }
 54 
 55     public void setUserName(String userName) {
 56         this.userName = userName;
 57     }
 58 
 59     public int getOrderState() {
 60         return orderState;
 61     }
 62 
 63     public void setOrderState(int orderState) {
 64         this.orderState = orderState;
 65     }
 66 
 67     public int getAddressId() {
 68         return addressId;
 69     }
 70 
 71     public void setAddressId(int addressId) {
 72         this.addressId = addressId;
 73     }
 74 
 75     public Date getOrderTime() {
 76         return orderTime;
 77     }
 78 
 79     public void setOrderTime(Date orderTime) {
 80         this.orderTime = orderTime;
 81     }
 82 
 83     public List getOrderDetail() {
 84         return orderDetail;
 85     }
 86 
 87     public void setOrderDetail(List orderDetail) {
 88         this.orderDetail = orderDetail;
 89     }
 90 
 91     @Override
 92     public String toString() {
 93         return "OrderBean [orderId=" + orderId + ", orderNo=" + orderNo
 94                 + ", userName=" + userName + ", orderState=" + orderState
 95                 + ", addressId=" + addressId + ", orderTime=" + orderTime
 96                 + ", orderDetail=" + orderDetail + "]";
 97     }
 98     
 99     
100 
101 }

3、创建接口OrderMapper.java 和  OrderMapper.xml  ,注意:这两个的命名必须相同

  OrderMapper.java写各种方法的接口,代码如下:

1 package com.november.query.zuoye1;
2 
3 import java.util.List;
4 
5 public interface OrderMapper {
6     
7     List findOrderList();
8 
9 }

       OrderMapper.xml  代码如下:

 1 
 2 DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 
 7 
 8     
 9     
10     
11     
12     
15     
16     
17     
18     
24     
25         
26         
27         
28         
29         
30         
31         
32         
33         
34         
35         
36         
37             
38             
39             
40             
41             
42             
43             
44             
45             
46             
47         
48 
49     
50 
51 

4、建立一个测试类 OrderTest

 1 package com.november.query.zuoye1;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.io.Resources;
 8 import org.apache.ibatis.session.SqlSession;
 9 import org.apache.ibatis.session.SqlSessionFactory;
10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 public class OrderTest {
15     
16     private SqlSessionFactory sqlSessionFactory = null;
17     //在执行测试方法之前 执行的 方法
18     @Before
19     public void init(){
20         //对mybatis环境进行 初始化
21         String resource = "config/mybatis.xml";
22         
23         try {
24             //1)读取配置文件
25             InputStream input = Resources.getResourceAsStream(resource);
26             //2)使用SQlSessionFactoryBuilder 创建SqlSessionFactory
27             sqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
28     
29             
30         } catch (IOException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34     }
35     
36     @Test
37     public void findOrderTest(){
38         
39         SqlSession session = sqlSessionFactory.openSession();
40         OrderMapper mapper =  session.getMapper(OrderMapper.class);
41         List list = mapper.findOrderList();
42         session.close();
43         System.out.println(list);
44     }
45 
46 }

5、运行结果

订单和订单详情的一对一 ,一对多映射_第1张图片

订单和订单详情的一对一 ,一对多映射_第2张图片

 

转载于:https://www.cnblogs.com/xsy20181001/p/9898979.html

你可能感兴趣的:(订单和订单详情的一对一 ,一对多映射)