当前位置:   article > 正文

springboot第二章---打造企业级微信点餐系统(4)--买家订单01---实体类及dao_public orderstatusenums? orderstatus { get; set; }

public orderstatusenums? orderstatus { get; set; } =

第一步:创建实体类(订单主表及订单详情表)

OrderMaster.java

  1. package com.fjz.vxsell.bean;
  2. import com.fjz.vxsell.enums.OrderStatusEnums;
  3. import com.fjz.vxsell.enums.PayStatusEnums;
  4. import lombok.Data;
  5. import org.hibernate.annotations.DynamicUpdate;
  6. import javax.persistence.Entity;
  7. import javax.persistence.Id;
  8. import java.math.BigDecimal;
  9. import java.util.Date;
  10. /**
  11. * 订单主表
  12. * @author 冯师兄
  13. * @date 2020-05-08 14:18
  14. */
  15. @Entity
  16. @Data
  17. @DynamicUpdate
  18. public class OrderMaster {
  19. /*订单id*/
  20. @Id
  21. private String orderId;
  22. /*买家名字*/
  23. private String buyerName;
  24. /*买家电话*/
  25. private String buyerPhone;
  26. /*买家地址*/
  27. private String buyerAddress;
  28. /*买家微信id*/
  29. private String buyerOpenid;
  30. /*订单总金额*/
  31. private BigDecimal orderAmount;
  32. /*订单状态,默认是新订单为0,实体类中最好不要出现=0,这里用一个枚举*/
  33. private Integer orderStatus = OrderStatusEnums.NEW.getCode();
  34. /*支付状态,默认为0 未支付*/
  35. private Integer payerStatus = PayStatusEnums.WAIT.getCode();
  36. /*创建时间*/
  37. private Date createTime;
  38. /*修改时间*/
  39. private Date updateTime;
  40. }

创建枚举 OrderStatusEnumsjava

  1. package com.fjz.vxsell.enums;
  2. import lombok.Getter;
  3. /**
  4. * @author 冯师兄
  5. * @date 2020-05-08 14:29
  6. */
  7. @Getter
  8. public enum OrderStatusEnums {
  9. NEW(0,"新订单"),
  10. FINISH(1,"订单已完结"),
  11. CANCLE(2,"订单已取消"),
  12. ;
  13. private Integer code;
  14. private String message;
  15. OrderStatusEnums(Integer code, String message) {
  16. this.code = code;
  17. this.message = message;
  18. }
  19. }

PayStatusEnums.java

  1. package com.fjz.vxsell.enums;
  2. import lombok.Getter;
  3. /**
  4. * @author 冯师兄
  5. * @date 2020-05-08 14:36
  6. */
  7. @Getter
  8. public enum PayStatusEnums {
  9. WAIT(0,"等待支付"),
  10. SUCCESS(1,"支付成功"),
  11. ;
  12. private Integer code;
  13. private String message;
  14. PayStatusEnums(Integer code, String message) {
  15. this.code = code;
  16. this.message = message;
  17. }
  18. }

OrderDetail.java

  1. package com.fjz.vxsell.bean;
  2. import lombok.Data;
  3. import org.hibernate.annotations.DynamicUpdate;
  4. import javax.persistence.Entity;
  5. import javax.persistence.Id;
  6. import java.math.BigDecimal;
  7. import java.security.PrivateKey;
  8. import java.util.Date;
  9. /**
  10. * 订单详情表
  11. * @author 冯师兄
  12. * @date 2020-05-08 14:44
  13. */
  14. @Entity
  15. @Data
  16. @DynamicUpdate
  17. public class OrderDetail {
  18. /*订单详情id*/
  19. @Id
  20. private String detailId;
  21. /*订单id*/
  22. private String orderId;
  23. /*商品id*/
  24. private String productId;
  25. /*商品名称*/
  26. private String productName;
  27. /*商品价格*/
  28. private BigDecimal productPrice;
  29. /*商品数量*/
  30. private Integer productQuantity;
  31. /*商品图片*/
  32. private String productIcon;
  33. }

第二步:创建repository:OrderMasterRepository.java

  1. package com.fjz.vxsell.repository;
  2. import com.fjz.vxsell.bean.OrderMaster;
  3. import org.springframework.data.domain.Page;
  4. import org.springframework.data.domain.Pageable;
  5. import org.springframework.data.jpa.repository.JpaRepository;
  6. /**
  7. * @author 冯师兄
  8. * @date 2020-05-08 14:59
  9. */
  10. public interface OrderMasterRepository extends JpaRepository<OrderMaster,String> {
  11. /**
  12. * 分页查询
  13. * 根据买家的openid查询
  14. * @param buyerOpenid
  15. * @param pageable
  16. * @return
  17. */
  18. Page<OrderMaster> findByBuyerOpenid(String buyerOpenid, Pageable pageable);
  19. }

测试:

  1. package com.fjz.vxsell.repository;
  2. import com.fjz.vxsell.bean.OrderMaster;
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.data.domain.Page;
  9. import org.springframework.data.domain.PageRequest;
  10. import org.springframework.test.context.junit4.SpringRunner;
  11. import java.math.BigDecimal;
  12. import static org.junit.Assert.*;
  13. @RunWith(SpringRunner.class)
  14. @SpringBootTest
  15. public class OrderMasterRepositoryTest {
  16. @Autowired
  17. private OrderMasterRepository orderMasterRepository;
  18. private final String Openid = "123456";
  19. /**
  20. * 测试保存
  21. */
  22. @Test
  23. public void saveTest() {
  24. OrderMaster orderMaster = new OrderMaster();
  25. orderMaster.setOrderId("004");
  26. orderMaster.setBuyerName("十三师兄");
  27. orderMaster.setBuyerPhone("15916574437");
  28. orderMaster.setBuyerAddress("北京房山");
  29. orderMaster.setBuyerOpenid(Openid);
  30. orderMaster.setOrderAmount(new BigDecimal(255.63));
  31. OrderMaster result = orderMasterRepository.save(orderMaster);
  32. Assert.assertNotNull(result);
  33. }
  34. /**
  35. * 分页查询
  36. */
  37. @Test
  38. public void findByBuyerOpenid() {
  39. PageRequest request = new PageRequest(0,2);
  40. Page<OrderMaster> page = orderMasterRepository.findByBuyerOpenid(Openid, request);
  41. System.out.println(page.getTotalElements());
  42. Assert.assertNotEquals(0, page.getTotalPages());
  43. }
  44. }

OrderDetailRepository.java

  1. package com.fjz.vxsell.repository;
  2. import com.fjz.vxsell.bean.OrderDetail;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import java.util.List;
  5. /**
  6. * @author 冯师兄
  7. * @date 2020-05-08 15:02
  8. */
  9. public interface OrderDetailRepository extends JpaRepository<OrderDetail, String> {
  10. /**
  11. * 根据orderID查询出所有的订单详情
  12. * @param orderId
  13. * @return
  14. */
  15. List<OrderDetail> findByOrderId(String orderId);
  16. }

测试:

  1. package com.fjz.vxsell.repository;
  2. import com.fjz.vxsell.bean.OrderDetail;
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import java.math.BigDecimal;
  10. import java.util.List;
  11. import static org.junit.Assert.*;
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest
  14. public class OrderDetailRepositoryTest {
  15. @Autowired
  16. private OrderDetailRepository orderDetailRepository;
  17. @Test
  18. public void saveTest(){
  19. OrderDetail orderDetail = new OrderDetail();
  20. orderDetail.setDetailId("000002");
  21. orderDetail.setOrderId("111111");
  22. orderDetail.setProductId("010101");
  23. orderDetail.setProductIcon("http://xxxx.jpg");
  24. orderDetail.setProductName("华为");
  25. orderDetail.setProductPrice(new BigDecimal(35.3));
  26. orderDetail.setProductQuantity(50);
  27. OrderDetail result = orderDetailRepository.save(orderDetail);
  28. Assert.assertNotNull(result);
  29. }
  30. @Test
  31. public void findByOrderId() {
  32. List<OrderDetail> list = orderDetailRepository.findByOrderId("111111");
  33. Assert.assertNotEquals(0, list.size());
  34. }
  35. }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/954922
推荐阅读
相关标签
  

闽ICP备14008679号