建表语句
CREATE TABLE `sys_tree` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL COMMENT '名称',
`parent_id` bigint(11) DEFAULT NULL COMMENT '父id',
`type` tinyint(4) NOT NULL COMMENT '菜单类型',
`level` tinyint(2) DEFAULT NULL COMMENT '层级',
`instruction` text COMMENT '填写说明',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=80 DEFAULT CHARSET=utf8mb4;
实体类:
package com.platform.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @author chenxm66777123
* @version 1.0.0
* @Description 实体类
* @Date 2019/12/3 14:58
*/
@Setter
@Getter
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("sys_tree")
public class SysTree extends Model<SysTree> {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 名称
*/
private String name;
/**
* 父id
*/
private Long parentId;
/**
* 菜单类型
*/
private Integer type;
/**
* 层级
*/
private Integer level;
/**
* 填写说明
*/
private String instruction;
@Override
protected Serializable pkVal() {
return this.id;
}
}
返回DTO
package com.pos.business.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
@Accessors(chain=true)
@ApiModel("树形数据")
public class SysTreeDTO implements Serializable {
private static final long serialVersionUID = 4743926233389974323L;
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "名称")
private String name;
@ApiModelProperty(value = "父级id")
private Long parentId;
@ApiModelProperty("子菜单")
private List<SysTreeDTO> children;
/**
* @Author : chenxm66777123
* @Description 构造子菜单
* @Date : 2019/6/19 16:50
**/
public List<SysTreeDTO> isNull(){
if(CollectionUtils.isEmpty(children)){
this.children=new ArrayList<>();
}
return this.children;
}
}
Service
package com.platform.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.platform.entity.SysTree;
import com.pos.business.dto.SysTreeDTO;
import java.util.List;
public interface SysTreeService extends IService<SysTree> {
/**
* @Description 构建树形结构
* @author chenxm66777123
* @Date 2019/12/3 15:02
* @version 1.0.0
*/
List<SysTreeDTO> bulidTree(Integer type);
}
Service实现类
package com.platform.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.dao.SysTreeMapper;
import com.platform.entity.SysTree;
import com.platform.service.SysTreeService;
import com.platform.util.ConstantsUtil;
import com.pos.business.dto.SysTreeDTO;
import com.pos.common.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class SysTreeServiceImpl extends ServiceImpl<SysTreeMapper, SysTree> implements SysTreeService {
@Autowired
private SysTreeMapper treeMapper;
/**
* @Description 构建树形结构
* @author chenxm66777123
* @Date 2019/12/3 15:02
* @version 1.0.0
*/
@Override
public List<SysTreeDTO> bulidTree(Integer type) {
// 数据库查询出tag数据
List<SysTree> nodes = treeMapper.selectList(new QueryWrapper<>(new SysTree().setType(type)));
// 解析格式
List<SysTreeDTO> tags = BeanUtils.assemble(SysTreeDTO.class, nodes);
Map<Long, SysTreeDTO> map = tags.stream().collect(Collectors.toMap(SysTreeDTO::getId, tagsDTO -> tagsDTO));
List<SysTreeDTO> resloveTags =new ArrayList<>();
// 只遍历一次,把所有的子菜单往对应的父菜单中加
tags.stream().forEach(obj -> {
if (ConstantsUtil.LEVEL_ZERO.equals(obj.getParentId())) {
resloveTags.add(obj);
} else {
SysTreeDTO parent = map.get(obj.getParentId());
parent.isNull().add(obj);
}
});
return resloveTags;
}
}
BeanUtils
package com.pos.common.utils;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* notes 对象工具类
* author xiaoming.chen
* create 2018/11/2 0002 15:40
**/
public class BeanUtils {
/**
* 通过 tClass 生成示例并且将 source 对应的字段 copy
* @param source 源对象
* @param tClass 期望对象类型
* @param <S> 源对象
* @param <T> 期望对象
* @return 期望对象实例
*/
public static <S,T> T copyProperties(S source, Class<T> tClass) {
try {
T target = tClass.newInstance();
if (null == source) {
return target;
}
org.springframework.beans.BeanUtils.copyProperties(source, target);
return target;
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException( String.format("%s must declaring none arguments constructor!", tClass.getTypeName()));
}
}
/**
* 根据类型生成,生成转换的新列表
*
* @param tClass 目标列表类型
* @param list 原列表数据
* @param <S> source class
* @param <T> target class
* @return 目标列表
*/
public static <S, T> List<T> assemble(Class<T> tClass, List<S> list){
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
List<T> lists=new ArrayList<>(list.size());
for (S s : list) {
lists.add(BeanUtils.copyProperties(s, tClass));
}
return lists;
}
/**
* 提供源数据列表和转换规则,生成新的列表
*
* @param list 原列表
* @param transferInterface 转换规则接口实现
* @param <S> source class
* @param <T> target class
* @return 转换后的列表
*/
public static <S, T> List<T> assemble(List<S> list, TransferInterface<S, T> transferInterface){
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
List<T> lists=new ArrayList<>(list.size());
for (S s : list) {
lists.add(transferInterface.transfer(s));
}
return lists;
}
public interface TransferInterface<S, T> {
T transfer(S s);
}
}
ConstantsUtil
package com.platform.util;
public class ConstantsUtil {
public static final Long LEVEL_ZERO = 0L;
}