14 ((BeanUtils.copyProperties () (

1 Product list

1.1 Interface definition

Transfer parameters

categoryId(非必传,子类目的商品也要查出来)
pageNum(default=1)
pageSize(default=10)

return value

{
    
    
    "status": 0,
    "data": {
    
    
        "pageNum": 1,
        "pageSize": 10,
        "size": 2,
        "orderBy": null,
        "startRow": 1,
        "endRow": 2,
        "total": 2,
        "pages": 1,
        "list": [
            {
    
    
                "id": 1,
                "categoryId": 3,
                "name": "iphone7",
                "subtitle": "双十一促销",
                "mainImage": "mainimage.jpg",
                "status":1,
                "price": 7199.22
            },
            {
    
    
                "id": 2,
                "categoryId": 2,
                "name": "oppo R8",
                "subtitle": "oppo促销进行中",
                "mainImage": "mainimage.jpg",
                "status":1,
                "price": 2999.11
            }
        ],
        "firstPage": 1,
        "prePage": 0,
        "nextPage": 0,
        "lastPage": 1,
        "isFirstPage": true,
        "isLastPage": true,
        "hasPreviousPage": false,
        "hasNextPage": false,
        "navigatePages": 8,
        "navigatepageNums": [
            1
        ]
    }
}

1.2 Code writing

1.2.1 controller

	@GetMapping("/products")
	public ResponseVo<PageInfo> list(@RequestParam(required = false) Integer categoryId,
									 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
									 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
    
    
		return productService.list(categoryId, pageNum, pageSize);
	}

1.2.2 Query subcategories and subcategories

Insert picture description here
Improve:
Insert picture description here

	@Override
	public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
    
    
		List<Category> categories = categoryMapper.selectAll();
		findSubCategoryId(id, resultSet, categories);
	}

	private void findSubCategoryId(Integer id, Set<Integer> resultSet, List<Category> categories) {
    
    
		for (Category category : categories) {
    
    
			if (category.getParentId().equals(id)) {
    
    
				resultSet.add(category.getId());
				findSubCategoryId(category.getId(), resultSet, categories);
			}
		}
	}

1.2.3service

	@Override
	public ResponseVo<PageInfo> list(Integer categoryId, Integer pageNum, Integer pageSize) {
    
    
		Set<Integer> categoryIdSet = new HashSet<>();
		if (categoryId != null) {
    
    
			categoryService.findSubCategoryId(categoryId, categoryIdSet);
			categoryIdSet.add(categoryId);
		}

		PageHelper.startPage(pageNum, pageSize);
		List<Product> productList = productMapper.selectByCategoryIdSet(categoryIdSet);
		List<ProductVo> productVoList = productList.stream()
				.map(e -> {
    
    
					ProductVo productVo = new ProductVo();
					BeanUtils.copyProperties(e, productVo);
					return productVo;
				})
				.collect(Collectors.toList());

		PageInfo pageInfo = new PageInfo<>(productList);
		pageInfo.setList(productVoList);
		return ResponseVo.success(pageInfo);
	}
  <select id="selectByCategoryIdSet" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from mall_product
    where status = 1
    <if test="categoryIdSet.size() > 0">
      and category_id in
      <foreach collection="categoryIdSet" item="item" index="index" open="(" separator="," close=")">
        #{
    
    item}
      </foreach>
    </if>
  </select>

1.2.4 dao

 List<Product> selectByCategoryIdSet(@Param("categoryIdSet") Set<Integer> categoryIdSet);

1.2.5 mapper

  <select id="selectByCategoryIdSet" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from mall_product
    where status = 1
    <if test="categoryIdSet.size() > 0">
      and category_id in
      <foreach collection="categoryIdSet" item="item" index="index" open="(" separator="," close=")">
        #{
    
    item}
      </foreach>
    </if>
  </select>

Note: To
determine whether there is a value in the set, try not to use categoryIdSet! =null, but use categoryIdSet.size()> 0

<if test="categoryIdSet!=null">

Otherwise it will cause not all categories to be found
Insert picture description here

Insert picture description here

2 Product details

2.1 controller

@GetMapping("/products/{productId}")
	public ResponseVo<ProductDetailVo> detail(@PathVariable Integer productId) {
    
    
		return productService.detail(productId);
	}

2.2 service

	@Override
	public ResponseVo<ProductDetailVo> detail(Integer productId) {
    
    
		Product product = productMapper.selectByPrimaryKey(productId);

		//只对确定性条件判断  下架,删除抛出异常
		// 不用 !product.getStatus().equals(ON_SALE.getCode() ,如果在加其他状态就不对啦
		if (product.getStatus().equals(OFF_SALE.getCode())
				|| product.getStatus().equals(DELETE.getCode())) {
    
    
			return ResponseVo.error(PRODUCT_OFF_SALE_OR_DELETE);
		}

		ProductDetailVo productDetailVo = new ProductDetailVo();
		BeanUtils.copyProperties(product, productDetailVo);
		//敏感数据处理
		productDetailVo.setStock(product.getStock() > 100 ? 100 : product.getStock());
		return ResponseVo.success(productDetailVo);
	}

In the project, define the entity class product. Input parameter packaged object ProductVo, and return result packaged object ProductDetailVo
Insert picture description here

note

BeanUtils.copyProperties(orderMasterDTO, orderMasterDO);

Function: Assign the attribute values ​​in the orderMasterDTO object to orderMasterDO. The main purpose is to use the reflection mechanism to copy the attributes of the JavaBean.

benefit:

Without using the BeanUtils.copyProperties(orderMasterDTO, orderMasterDO) method, the traditional approach is: manually set the property value of orderMasterDTO to orderMasterDO

OrderMasterDO orderMasterDO = new OrderMasterDO();
orderMasterDO.setOrderId(orderMasterDTO.getOrderId());
orderMasterDO.setBuyerName(orderMasterDTO.getBuyerName());
orderMasterDO.setOrderStatus(orderMasterDTO.getOrderStatus());
orderMasterDO.setCreateTimestamp(orderMasterDTO.getCreateTimestamp());
orderMasterDO.setUpdateTimestamp(orderMasterDTO.getUpdateTimestamp());

Using the tool method of BeanUtils, only BeanUtils.copyProperties(orderMasterDTO, orderMasterDO) can ojbk, which is much simpler and more convenient.

Guess you like

Origin blog.csdn.net/Insist___/article/details/109155522