实体类-订单表
@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 = OrderStatusEnum.NEW.getCode();
/**创建时间*/
// private Date createTime;
/**修改时间*/
// private Date updateTime;
}
订单详情表:
@Entity
@Data
public class OrderDetail {
@Id
/**主键*/
private String detailId;
/**订单编号*/
private String orderId;
/**商品编号*/
private String productId;
/**商品名称*/
private String productName;
/**单价*/
private BigDecimal productPrice;
/**商品数量*/
private Integer productQuantity;
/**商品图片*/
private String productIcon;
/**创建时间*/
// private Date createTime;
/**修改时间*/
// private Date updateTime;
}
dao层:
public interface OrderMasterRepository extends JpaRepository<OrderMaster,String> {
Page<OrderMaster> findByBuyerOpenid(String buyerOpenid, Pageable pageable);
}
public interface OrderDetailRepository extends JpaRepository<OrderDetail,String> {
List<OrderDetail> findByOrderId(String orderId, Pageable pageable);
}
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderMasterRepositoryTest extends TestCase {
@Autowired
private OrderMasterRepository repository;
private final String OPENID = "10010";
@Test
public void saveTest(){
OrderMaster orderMaster = new OrderMaster();
orderMaster.setOrderId("123457");
orderMaster.setBuyerName("张三的订单");
orderMaster.setBuyerPhone("13754677890");
orderMaster.setBuyerAddress("仙桃池");
orderMaster.setBuyerOpenid(OPENID);
orderMaster.setOrderAmount(new BigDecimal(8.8));
OrderMaster result = repository.save(orderMaster);
Assert.assertNotNull(result);
}
@Test
public void testFindByBuyerOpenid() {
PageRequest request = PageRequest.of(1,3);
Page<OrderMaster> result = repository.findByBuyerOpenid(OPENID, request);
System.out.println(result.getTotalElements());
Assert.assertNotNull(String.valueOf(0),result.getTotalElements());
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderDetailRepositoryTest extends TestCase {
@Autowired
private OrderDetailRepository repository;
private final String ORDERID="11111111";
@Test
public void saveTest(){
OrderDetail orderDetail = new OrderDetail();
orderDetail.setDetailId("12348");
orderDetail.setOrderId(ORDERID);
orderDetail.setProductIcon("http://xigua.jpg");
orderDetail.setProductId("1212121");
orderDetail.setProductName("西瓜粥");
orderDetail.setProductPrice(new BigDecimal(3.2));
orderDetail.setProductQuantity(2);
OrderDetail result = repository.save(orderDetail);
Assert.assertNotNull(result);
}
@Test
public void testFindByOrderId() {
PageRequest request = PageRequest.of(0, 3);
List<OrderDetail> result = repository.findByOrderId(ORDERID, request);
Assert.assertNotNull(String.valueOf(0),result.size());
}
}