1. 程式人生 > >訂單和訂單詳情的一對一 ,一對多對映

訂單和訂單詳情的一對一 ,一對多對映

我們知道,訂單和訂單詳情是一對多的關係,所以在訂單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<OrderDetailBean> 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<OrderDetailBean> 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<OrderDetailBean> getOrderDetail() {
 84         return orderDetail;
 85     }
 86 
 87     public void setOrderDetail(List<OrderDetailBean> 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<OrderBean> findOrderList();
8 
9 }

       OrderMapper.xml  程式碼如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <!-- namespace用於區分 sql標籤 ,一般為了保證唯一,namespace表示為 包名+xml檔名 -->
 7 <mapper namespace="com.november.query.zuoye1.OrderMapper">
 8     
 9     
10     <!-- select * from t_Order ord join t_OrderDetail detail on ord.OrderId = detail.OrderId -->
11     <!-- 查詢所有訂單:resultMap就是結果集對映 -->
12     <select id="findOrderList" resultMap="orderMap">
13         select * from t_Order ord join t_OrderDetail detail on ord.OrderId = detail.OrderId
14     </select>
15     
16     
17     <!-- 自動對映 :當屬性名和 欄位名一致時,會自動將欄位值 裝配到 屬性中-->
18     <!-- id屬性 ,resultMap標籤的標識。
19          type屬性 ,返回值的全限定類名,或類型別名。
20          autoMapping屬性 ,值範圍true(預設值)|false, 設定是否啟動自動對映功能,
21          自動對映功能就是自動查詢與欄位名小寫同名的屬性名,並呼叫setter方法。
22          而設定為false後,則需要在resultMap內明確註明對映關係才會呼叫對應的setter方法 
23      -->
24     <resultMap type="OrderBean" id="orderMap" autoMapping="true">
25         <!-- 主鍵 -->
26         <id property="orderId" column="OrderId"/>
27         <!-- 其他屬性 -->
28         <result property="orderNo" column="OrderNo"/>
29         <result property="userName" column="UserName"/>
30         <result property="orderState" column="OrderState"/>
31         <result property="addressId" column="AddressId"/>
32         <result property="orderTime" column="OrderTime"/>
33         
34         <!-- 一對多關係對映 -->
35         <!-- property="orderDetailList"的orderDetailList是OrderBean的集合屬性 -->
36         <collection property="orderDetailList" ofType="OrderDetailBean" autoMapping="true">
37             <!-- orderDetailList 實體屬性 和 t_OrderDetail 表字段的對映  -->
38             
39             <id property="orderDetailId" column="OrderDetailId"/>
40             <result property="orderId" column="OrderId"/>
41             <result property="goodsId" column="GoodsId"/>
42             <result property="orderAmount" column="OrderAmount"/>
43             <result property="orderMoneyPayType" column="OrderMoneyPayType"/>
44             <result property="orderPayTime" column="OrderPayTime"/>
45             <result property="isReview" column="IsReview"/>
46             
47         </collection>
48 
49     </resultMap>
50 
51 </mapper>

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<OrderBean> list = mapper.findOrderList();
42         session.close();
43         System.out.println(list);
44     }
45 
46 }

5、執行結果