SpringBoot 微信點餐系統學習記錄六-訂單表和訂單詳情表的後端開發

  • 在dataobject文件夾創建訂單表和訂單詳情表的實體類

    package com.imooc.dataobject;
    
    import com.imooc.enums.OrderStatusEnum;
    import com.imooc.enums.PayStatusEnum;
    import lombok.Data;
    import org.hibernate.annotations.DynamicUpdate;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import java.math.BigDecimal;
    import java.util.Date;
    
    /**
     * Created by Cdy1234 on 2018/7/24.
     *訂單主表
     */
    @Entity
    @Data
    @DynamicUpdate
    public class OrderMaster  {
    
        /**訂單ID**/
        @Id
        private  String orderId;
    
        /**買家姓名**/
        private  String buyerName;
    
        /**買家電話**/
        private  String buyerPhone;
    
        /**買家地址**/
        private  String buyerAddress;
    
        /**買家微信openId**/
        private  String buyerOpenid;
    
        /**訂單總金額**/
        private  BigDecimal orderAmount;
    
        /**訂單狀態 默認爲0,代表新訂單**/
        private  Integer orderStatus= OrderStatusEnum.NEW.getCode();
    
        /**支付狀態 默認爲0,代表未支付**/
        private  Integer payStatus= PayStatusEnum.WAIT.getCode();
    
        /**訂單創建時間**/
        private  Date createTime;
    
        /**更新時間**/
        private  Date updateTime;
    
    
    
    }
    
    package com.imooc.dataobject;
    
    import lombok.Data;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import java.math.BigDecimal;
    
    /**
     * Created by Cdy1234 on 2018/8/7.
     *訂單詳情表
     */
    @Entity
    @Data
    public class OrderDetail {
      @Id
      private String detailId;
    
      /**訂單ID*/
      private String orderId;
    
      /**商品ID*/
      private String productId;
    
      /**商品名稱*/
      private String productName;
    
      /**商品價格*/
      private BigDecimal productPrice;
    
      /**商品數量*/
      private Integer productQuantity;
    
      /***商品圖片*/
      private String productIcon;
    
    
    }
    

    在DAO層創建對訂單表的操作,繼承JpaRepository,編寫思路,訂單詳情是通過訂單id查詢,可能返回多個結果。所以是返回個list集合。

  • package com.imooc.repository;
    
    import com.imooc.dataobject.OrderDetail;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import java.util.List;
    
    /**
     * Created by Cdy1234 on 2018/8/11.
     * 訂單詳情表dao
     */
    
    public interface OrderDetailRepository extends JpaRepository<OrderDetail,String>{
        /**
         * 根據Orderid 來查詢訂單詳情,返回一條以上的記錄,使用list集合。
         * @param orderId 訂單id
         * @return
         */
        List<OrderDetail>findByOrderId(String orderId);
    
    
    }
    

    編寫對訂單主表的DAO層,繼承JpaRepository,思路,查詢用戶的openid,可以是返回多個訂單,所以返回一個Page。

  • package com.imooc.repository;
    
    import com.imooc.dataobject.OrderMaster;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    /**
     * Created by Cdy1234 on 2018/8/8.
     * 訂單主表表dao層
     */
    
    public interface OrderMasterRepository extends JpaRepository<OrderMaster,String>{
    
        /**
         * 根據用戶id查詢訂單詳情
         * @param buyerOpenid 用戶的微信Open
         * @param pageable    第幾頁
         * @return Page      頁數和用戶下的訂單詳情
         */
       Page<OrderMaster>findByBuyerOpenid(String buyerOpenid, Pageable pageable);
    }
    

    測試DAO層OrderDetailRepository 的情況。右鍵OrderDetailRepository,選擇go to--->Test 

  •  

  • 選擇所編寫的方法。創建OrderDetailRepositoryTest ,測試Save方法, Assert.assertNotNull(result),只需要result不爲空,就可以通過。

  • package com.imooc.repository;
    
    import com.imooc.dataobject.OrderDetail;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.math.BigDecimal;
    
    /**
     * Created by Cdy1234 on 2018/8/17.
     * 訂單詳情表DAO層
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class OrderDetailRepositoryTest {
    
        @Autowired
        private OrderDetailRepository repository;
    
        @Test
        public  void saveTest(){
            OrderDetail orderDetail=new OrderDetail();
            orderDetail.setDetailId("142342842988");
            orderDetail.setOrderId("1111321");
            orderDetail.setProductName("皮蛋粥");
            orderDetail.setProductIcon("http://xxx.jpg");
            orderDetail.setProductId("2332423");
            orderDetail.setProductPrice(new BigDecimal(12));
            orderDetail.setProductQuantity(20);
            OrderDetail result= repository.save(orderDetail);
    
            Assert.assertNotNull(result);
        }
    
        @Test
        public void testFindByOrderId() throws Exception {
    
    
        }
    }

    執行Save方法,數據表成功添加,測試成功。

  • 測試訂單主表DAO層測試,同上。

  • package com.imooc.repository;
    
    import com.imooc.dataobject.OrderMaster;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.math.BigDecimal;
    
    /**
     * Created by Cdy1234 on 2018/8/17.
     * 訂單主表測試
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class OrderMasterRepositoryTest {
    
        @Autowired
        private  OrderMasterRepository repository;
    
        private final String OPENID="110110";
    
        @Test
        public void SaveTest(){
            OrderMaster orderMaster=new OrderMaster();
            orderMaster.setOrderId("124323");
            orderMaster.setBuyerName("蔡中彩");
            orderMaster.setBuyerPhone("18060065761");
            orderMaster.setBuyerAddress("領袖天地");
            orderMaster.setBuyerOpenid(OPENID);
            orderMaster.setOrderAmount(new BigDecimal(2.3));
            OrderMaster result= repository.save(orderMaster);
    
            Assert.assertNotNull(result);
    
    
        }
    
        @Test
        public void testFindByBuyerOpenid() throws Exception {
            PageRequest requset=PageRequest.of(0,1);//調用PAgeRequest,該方法繼承Pageable,
            Page<OrderMaster> result =repository.findByBuyerOpenid(OPENID,requset);
            Assert.assertNotEquals(0,result.getTotalElements());//不等於0,就運行通過
            System.out.println(result.getTotalElements());
    
        }
    }

    測試SaveTest方法。

  • 運行成功

  • 測試testFindByBuyerOpenid

  • 測試成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章