14商品(BeanUtils.copyProperties())

1 商品列表

1.1 接口定义

传参

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

返回值

{
    "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代码编写

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查询子类和子子类

在这里插入图片描述
改进:
在这里插入图片描述

	@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>

注意:
判断set里边是否有值,尽量不用categoryIdSet!=null,而是用categoryIdSet.size() > 0

<if test="categoryIdSet!=null">

否则会造成查出来的结果不是所有类目
在这里插入图片描述

在这里插入图片描述

2 商品详情

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);
	}

在项目中,定义啦实体类product。入参封装的对象ProductVo,和返回结果的封装对象ProductDetailVo
在这里插入图片描述

注意

BeanUtils.copyProperties(orderMasterDTO, orderMasterDO);

作用:将orderMasterDTO对象中的属性值,赋值到orderMasterDO中,其主要目的是利用反射机制对JavaBean的属性进行拷贝。

好处:

不使BeanUtils.copyProperties(orderMasterDTO, orderMasterDO)方法的话,传统的做法是:手动将orderMasterDTO的属性值set到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());

而使用了BeanUtils的工具方法,只需BeanUtils.copyProperties(orderMasterDTO, orderMasterDO)就可以ojbk,简单方便多了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值