实体类
/**
* 菜单表
*/
@Data
@EqualsAndHashCode(callSuper=true)
@TableName(value = "sys_menu")
public class SysMenu extends AbstractEntity implements Serializable {
/**
* 菜单名称
*/
@TableField(value = "`name`")
private String name;
/**
* 前端路径
*/
@TableField(value = "`path`")
private String path;
/**
* 图标
*/
@TableField(value = "icon")
private String icon;
/**
* 排序
*/
@TableField(value = "sort")
private Integer sort;
/**
* 菜单类型(0. 菜单 1. 按钮)
*/
@TableField(value = "`type`")
private Integer type;
/**
* 权限标识
*/
@TableField(value = "permission")
private String permission;
/**
* 父菜单ID
*/
@TableField(value = "parent_id")
private Long parentId;
private static final long serialVersionUID = 1L;
}
返回数据实体类:
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Map;
@Data
@Schema(description = "菜单返回数据")
public class SysDto {
@Schema(description = "ID")
private Long id;
@Schema(description = "父ID")
private Long parentId;
@Schema(description = "菜单名称")
private String name;
@Schema(description = "图标")
private String icon;
@Schema(description = "路径")
private String path;
@Schema(description = "规格名称")
private Map<Long, String> attr;
}
AuthConverter
package com.szzz.produce.modules.auth.converter;
import com.szzz.produce.api.auth.dto.SysDto;
import com.szzz.produce.api.industry.dto.IndustryCategoryDto;
import com.szzz.produce.modules.industry.entity.IndustryCategory;
import com.szzz.produce.modules.jurisdiction.entity.SysMenu;
import org.mapstruct.Mapper;
import java.util.List;
@Mapper(componentModel = "spring")
public interface AuthConverter {
SysDto toDto(SysMenu industryCategory);
List<SysDto> toDtoList(List<SysMenu> industryCategoryList);
}
AuthServiceImpl
@Override
public List<Tree<Long>> findLoginUserLeftMenu(){
//查看左侧菜单
List<SysMenu> menus = sysMenuMapper.selectMeans();
//转换
List<SysDto> icDto = authConverter.toDtoList(menus);
//构建树
List<Tree<Long>> build = TreeUtil.build(icDto, CommonConstant.PARENT_ID_LONG, (categoryDto, tree) -> {
tree.setId(categoryDto.getId());
tree.setName(categoryDto.getName());
tree.setParentId(categoryDto.getParentId());
tree.putExtra("icon", categoryDto.getIcon());
tree.putExtra("path", categoryDto.getPath());
});
return build;
};
CommonConstant
public class CommonConstant {
public static final String STORE_ID_COLUMN = "STORE_ID";
public static final String DEFAULT_SYSTEM_NAME = "produce_cloud_system";
public static final boolean NON_ENABLED = false;
public static final boolean ENABLED = true;
public static final boolean DELETED = true;
public static final Integer PARENT_ID_INT = 0;
public static final Long PARENT_ID_LONG = 0L;
}
pom.xml依赖
<!-- Hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!-- Hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!-- hutool -->
<hutool.version>5.5.4</hutool.version>
结果
父类下面嵌套子类
一般用于菜单表查询,和父id下包含多个子id;
快速查询,简单上手,一个底层工具类就可以搞定
每天努力一点点