springboot微服务实战:利用vo重新构建pojo

工具类

PageUtils

/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有,侵权必究!
 */

package com.ctra.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
public class PageUtils implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 总记录数
	 */
	private int totalCount;
	/**
	 * 每页记录数
	 */
	private int pageSize;
	/**
	 * 总页数
	 */
	private int totalPage;
	/**
	 * 当前页数
	 */
	private int currPage;
	/**
	 * 列表数据
	 */
	private List<?> list;
	
	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalCount  总记录数
	 * @param pageSize    每页记录数
	 * @param currPage    当前页数
	 */
	public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.currPage = currPage;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
	}

	/**
	 * 分页
	 */
	public PageUtils(IPage<?> page) {
		this.list = page.getRecords();
		this.totalCount = (int)page.getTotal();
		this.pageSize = (int)page.getSize();
		this.currPage = (int)page.getCurrent();
		this.totalPage = (int)page.getPages();
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
	}
	
}

BeanUtils 为:package org.springframework.beans;

QueryWrapper 为mybaits-plus类库

vo层:接收实体类

package com.ctra.gulimall.product.vo;

import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

@Data
public class AttrVo {
    private Long attrId;
    /**
     * $column.comments
     */
    private String attrName;
    /**
     * $column.comments
     */
    private Integer searchType;
    /**
     * $column.comments
     */
    private String icon;
    /**
     * $column.comments
     */
    private String valueSelect;
    /**
     * $column.comments
     */
    private Integer attrType;
    /**
     * $column.comments
     */
    private Long enable;
    /**
     * $column.comments
     */
    private Long catelogId;
    /**
     * 快速展示【是否展示在介绍上:0否1是】,在sku张仍然可以调整
     * $column.comments
     */
    private Integer showDesc;

    // 非数据库字段
    private Long attrGroupId;
}

vo层:返回实体类

package com.ctra.gulimall.product.vo;
import lombok.Data;

@Data
public class AttrRespVo extends AttrVo {
//    "catelogName": "手机/数码/手机", //所属分类名字
//    "groupName": "主体", //所属分组名字


    private String catelogName;
    private String groupName;
}

利用流编程重新封装实体类

 // 商品属性-平台属性-规格参数 http://localhost:8001/product-baseattr
    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if(catelogId!=0){
            wrapper.eq("catelog_id",catelogId);
        }
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key){
            wrapper.and((obj)->{
                obj.eq("attr_id",key).or().like("attr_name",key);
            });
        }

        // 根据条件获得列表
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        // 获得上面查询到的所有 AttrEntity 集合
        List<AttrEntity> records = page.getRecords();
        PageUtils pageutil= new PageUtils(page); // new一个返回值
        // 从列表关联到未查询到的2个表中字段
        List<AttrRespVo> attrRespVos = records.stream().map((entity) -> {
            // 将查询到的实体类->copy 到封装的新的 AttrRespVo 中
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(entity, attrRespVo);

            // 设置分类和分组的名字

            // 1、获取 AttrAttrgroupRelationEntity 实体 根据 attr_id
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", entity.getAttrId()));

            if (attrAttrgroupRelationEntity != null) {
                // 获得分组 AttrGroupEntity 实体 根据  AttrGroupId
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
                // 设置到 AttrRespVo 字段中
                attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }

            // 2、获取 CategoryEntity 实体 根据 CatelogId
            CategoryEntity categoryEntity = categoryDao.selectById(entity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            // 最=====================终:返回这个组装好的新的实体类 attrRespVo
            return attrRespVo;
        })
                .collect(Collectors.toList());// 再转成list

        // 重新设置 pageutil 
        pageutil.setList(attrRespVos);
        return pageutil;
    }