springBoot+restfulApi+注解开发总结(小Demo)(续篇1)

紧接着上篇的继续写。

ProductInfo:
 

@Entity
@DynamicUpdate
@Data
public class ProductInfo {
    /**
     * 商品id
     */
	@Id
	private String productId;
	
	/**
	 *商品名称
	 */
	private String productName;
	
	/**
	 *单价
	 */
	private BigDecimal productPrice;
	
	/**
	 *库存
	 */
	private Integer productStock;
	
	/**
	 *描述
	 */
	private String productDescription;
	
	/**
	 *小图
	 */
	private String productIcon;
	
	/**
	 * 商品状态,0正常1下架
	 */
	private Integer productStatus;
	
	/**
	 *类目编号
	 */
	private Integer categoryType;
	
	/**
	 *创建时间
	 */
	private Date createTime;
	
	/**
	 *修改时间
	 */
	private Date updateTime;
	
	
	public ProductInfo() {
	}


	/** 
	  * <p>Title: </p> 
	  * <p>Description: </p> 
	  * @param productId
	  * @param productName
	  * @param productPrice
	  * @param productStock
	  * @param productDecription
	  * @param productIcon
	  * @param productStatus
	  * @param categoryType
	  * @param createTime
	  * @param updateTime 
	*/
	
	public ProductInfo(String productId, String productName, BigDecimal productPrice, int productStock,
			String productDescription, String productIcon, int productStatus, int categoryType, Date createTime,
			Date updateTime) {
		super();
		this.productId = productId;
		this.productName = productName;
		this.productPrice = productPrice;
		this.productStock = productStock;
		this.productDescription = productDescription;
		this.productIcon = productIcon;
		this.productStatus = productStatus;
		this.categoryType = categoryType;
		this.createTime = createTime;
		this.updateTime = updateTime;
	}
	
}

OrderDetail:
 

@Entity
@DynamicUpdate
@Data
public class OrderDetail {
    
    /**订单明细详情id.*/
    @Id
    private String detailId;
    
    /**订单id.*/
    private String orderId;
    
    /**商品id.*/
    private String productId;
    
    /**商品名称.*/
    private String productName;
    
    /**当前价格,单位分.*/
    private BigDecimal productPrice;
    
    /**数量.*/
    private Integer productQuantity;
    
    /**小图.*/
    private String productIcon;
    
    /**创建时间.*/
    private Date createTime;
    
    /**修改时间.*/
    private Date updateTime;
    
    public OrderDetail() {
    }

}

OrderMaster:

@Entity
@DynamicUpdate
@Data
public class OrderMaster {
	
	/** 订单id. */
	@Id
//	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private String orderId;

	/** 买家名字. */
	private String buyerName;

	/** 买家电话. */
	private String buyerPhone;

	/** 买家地址. */
	private String buyerAddress;

	/** 买家微信openid. */
	private String buyerOpenid;

	/** 订单总金额. */
//	private double orderAmount;
	private BigDecimal orderAmount;

	/** 订单状态,默认为新下单. */
	private Integer orderStatus;

	/** 支付状态,默认未支付. */
	private Integer payStatus;

	/** 创建时间. */
	private Date createTime;

	/** 修改时间. */
	private Date updateTime;

	/**订单明细列表.*/
//	@Transient
//	private List<OrderDetail> orderDetailList;

}

注:1.@Data注解是lombok的注解,目的在于简化getset方法,等价于注解@Getter和@Setter两者组合

      2.对于属性字段有时间的,如updatetime,想每次操作更改数据表的时候更改update字段,其中@DynamicUpdate注解可以实现这个功能。

接下来是pom依赖:

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- springboot热部署依赖包 start -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
			<scope>true</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
</dependencies>

注:这个项目是springboot项目,在创建的时候记得是maven构建的springboot项目,然后添加进去这些依赖;

接下来新建dao层:ProductCategoryRepository继承JpaRepository

public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer>{

	List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
    
//	void UpdateCategoryAndFlush(ProductCategory  productCategory);
}

新建service层:

public interface CategoryService {
    
	ProductCategory findOne(Integer categoryId);
	
	List<ProductCategory> findAll();
	
      List<ProductCategory> findByCategoryTypeIn(List<Integer> list);

}

新建service的impl层:

@Service
public class CategoryServiceImpl implements CategoryService {

	public CategoryServiceImpl() {
	}

	@Autowired
	private ProductCategoryRepository repository;
    
	@Override
	public ProductCategory findOne(Integer categoryId) {
		return repository.getOne(categoryId);
	}
	
	@Override
	public List<ProductCategory> findAll(){
		return  repository.findAll();
	}
	@Override
	public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
		return repository.findByCategoryTypeIn(categoryTypeList);
	}

	
    
}

创建controller层:

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
		@Autowired
		private ProductInfoService productService;
	
       @Autowired
       private CategoryService categoryService;
       
       
       @GetMapping("/list")
       public ResultVO list(){
    	   //1.查询所有上架商品
    	   List<ProductInfo> productInfoUpList = productService.findUpAll();
    	   //2.查询类目--一次性查出来
    	   List<Integer> categoryTypeList = productInfoUpList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
    	   List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);
    	   //3.数据的拼装
    	   List<ProductVO> productVOList=new ArrayList<ProductVO>();
    	   for (ProductCategory productCategory : productCategoryList) {
			ProductVO productVO=new ProductVO();
			productVO.setCategoryType(productCategory.getCategoryType());
			productVO.setCategoryName(productCategory.getCategoryName());
			
			List<ProductInfoVO> productInfoVOList= new ArrayList<ProductInfoVO>();
			for (ProductInfo productInfo : productInfoUpList) {
				if(productInfo.getCategoryType()==(productCategory.getCategoryType())){
					ProductInfoVO productInfoVO=new ProductInfoVO();
					BeanUtils.copyProperties(productInfo,productInfoVO);
					productInfoVO.setProductPrice(productInfo.getProductPrice());
					productInfoVOList.add(productInfoVO);
				}
				
			}
			productVO.setProductInfoVOList(productInfoVOList);
			productVOList.add(productVO);
		}
    	   ResultVO resultVo =new ResultVO();
//    	   ProductVO  productVO=new ProductVO();
//    	   ProductInfoVO productinfovo=new ProductInfoVO();
//    	   productinfovo.setProductId("1234234");
//    	   productinfovo.setProductName("皮蛋瘦肉粥");
//    	   productinfovo.setProductPrice(3.4);
//    	   productinfovo.setProductIcon("HTTP://QWEQWE.JPG");
//    	   productinfovo.setProductDescription("好吃不上火");
//    	   productVO.setCategoryType(1);
//    	   productVO.setCategoryName("热榜");//皮蛋瘦肉粥
//    	   productVO.setProductInfoVOList(Arrays.asList(productinfovo));
    	   resultVo.setData(productVOList);
    	   resultVo.setCode(0);
    	   resultVo.setMsg("成功");
    	   return resultVo;
       }
}

 

访问路径:http://localhost:8080/sell/buyer/product/list

结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值