java框架树结构实现(带层级、编码、排序)

2024-06-06 14:04

本文主要是介绍java框架树结构实现(带层级、编码、排序),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、需求

实现一个影像资料库的功能,用树结构对资料进行分类

2、数据结构

通过id、pid表示父子关系

通过code表示层级关系

通过layer表示层级

通过sort进行排序

3、实体类

package org.jeecg.modules.image.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.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeecg.common.system.base.entity.BaseSearchDTO;
import org.jeecg.common.system.base.support.Condition;
import org.jeecg.common.system.base.support.Match;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.data.annotation.Transient;
import org.springframework.format.annotation.DateTimeFormat;import java.util.ArrayList;
import java.util.Date;
import java.util.List;/*** @Description: 影像资料分类* @Author: jeecg-boot* @Date: 2024-05-28* @Version: V1.0*/
@Data
@TableName("img_classification")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "img_classification对象", description = "影像资料分类")
public class ImgClassification extends BaseSearchDTO {/*** 主键*/@TableId(type = IdType.ASSIGN_ID)@ApiModelProperty(value = "主键")private String id;/*** 创建人*/@ApiModelProperty(value = "创建人")private String createBy;/*** 创建日期*/@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")@ApiModelProperty(value = "创建日期")private Date createTime;/*** 更新人*/@ApiModelProperty(value = "更新人")private String updateBy;/*** 更新日期*/@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")@ApiModelProperty(value = "更新日期")private Date updateTime;/*** 所属部门*/@ApiModelProperty(value = "所属部门")private String sysOrgCode;/*** 父id*/@Excel(name = "父id", width = 15)@ApiModelProperty(value = "父id")private String pid;/*** 分类编码*/@Excel(name = "分类编码", width = 15)@ApiModelProperty(value = "分类编码")@Condition(match = Match.LLIKE)private String code;/*** 分类名称*/@Excel(name = "分类名称", width = 15)@ApiModelProperty(value = "分类名称")@Condition(match = Match.LIKE)private String name;/*** 层级*/@Excel(name = "层级", width = 15)@ApiModelProperty(value = "层级")private Integer layer;/*** 排序*/@Excel(name = "排序", width = 15)@ApiModelProperty(value = "排序")private Integer sort;/*** 是否有子节点*/@Excel(name = "是否有子节点", width = 15)@ApiModelProperty(value = "是否有子节点")private Boolean hasChildren;不在库里/*** 父级名称*/@TableField(exist = false)@ApiModelProperty(value = "父级名称")private String parentName;/*** 子集*/@Transient@TableField(exist = false)@ApiModelProperty(value = "子集")private List<ImgClassification> children = new ArrayList<>();}

4、控制器

package org.jeecg.modules.image.controller;import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.service.BaseService;
import org.jeecg.modules.image.entity.ImgClassification;
import org.jeecg.modules.image.service.IImgClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;/*** @Description: 影像资料分类* @Author: jeecg-boot* @Date: 2024-05-28* @Version: V1.0*/
@Api(tags = "1.0.1.0 影像资料分类")
@RestController
@RequestMapping("/image/imgClassification")
@Slf4j
public class ImgClassificationController extends JeecgController<ImgClassification, IImgClassificationService> {@Autowiredprivate IImgClassificationService imgClassificationService;@Autowiredprivate BaseService<ImgClassification> baseService;//    /**
//     * 分页列表查询
//     *
//     * @param imgClassification
//     * @param pageNo
//     * @param pageSize
//     * @param req
//     * @return
//     */
//    //@AutoLog(value = "影像资料分类-分页列表查询")
//    @ApiOperation(value = "影像资料分类-分页列表查询", notes = "影像资料分类-分页列表查询")
//    @GetMapping(value = "/list")
//    public Result<IPage<ImgClassification>> queryPageList(ImgClassification imgClassification,
//                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
//                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
//                                                          HttpServletRequest req) {
//        QueryWrapper<ImgClassification> queryWrapper = QueryGenerator.initQueryWrapper(imgClassification, req.getParameterMap());
//        Page<ImgClassification> page = new Page<ImgClassification>(pageNo, pageSize);
//        IPage<ImgClassification> pageList = imgClassificationService.page(page, queryWrapper);
//        return Result.OK(pageList);
//    }/*** 分页列表查询** @param imgClassification* @return*///@AutoLog(value = "影像资料分类-分页列表查询")@ApiOperation(value = "影像资料分类-分页列表查询", notes = "影像资料分类-分页列表查询")@GetMapping(value = "/list")public Result<IPage<ImgClassification>> queryPageList(ImgClassification imgClassification) {IPage<ImgClassification> pageList = baseService.selectPageByDTO(imgClassification);return Result.OK(pageList);}/*** 列表查询** @param imgClassification* @return*/@ApiOperation(value = "影像资料分类-列表查询", notes = "影像资料分类-列表查询")@GetMapping(value = "/listAll")public Result<List<ImgClassification>> listAll(ImgClassification imgClassification) {List<ImgClassification> list = baseService.selectlistByDto(imgClassification);return Result.OK(list);}/*** 添加** @param imgClassification* @return*/@AutoLog(value = "影像资料分类-添加")@ApiOperation(value = "影像资料分类-添加", notes = "影像资料分类-添加")//@RequiresPermissions("image:img_classification:add")@PostMapping(value = "/add")public Result<String> add(@RequestBody ImgClassification imgClassification) {
//        imgClassificationService.save(imgClassification);imgClassificationService.add(imgClassification);return Result.OK("添加成功!");}/*** 批量添加** @param imgClassifications* @return*/@AutoLog(value = "影像资料分类-批量添加")@ApiOperation(value = "影像资料分类-批量添加", notes = "影像资料分类-批量添加")@PostMapping(value = "/addBatch")public Result<String> addBatch(@RequestBody List<ImgClassification> imgClassifications) {
//        imgClassificationService.save(imgClassification);imgClassificationService.addBatch(imgClassifications);return Result.OK("添加成功!");}/*** 编辑** @param imgClassification* @return*/@AutoLog(value = "影像资料分类-编辑")@ApiOperation(value = "影像资料分类-编辑", notes = "影像资料分类-编辑")//@RequiresPermissions("image:img_classification:edit")@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})public Result<String> edit(@RequestBody ImgClassification imgClassification) {imgClassificationService.updateById(imgClassification);return Result.OK("编辑成功!");}/*** 通过id删除** @param id* @return*/@AutoLog(value = "影像资料分类-通过id删除")@ApiOperation(value = "影像资料分类-通过id删除", notes = "影像资料分类-通过id删除")//@RequiresPermissions("image:img_classification:delete")@DeleteMapping(value = "/delete")public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
//        imgClassificationService.removeById(id);imgClassificationService.delete(id);return Result.OK("删除成功!");}/*** 批量删除** @param ids* @return*/@AutoLog(value = "影像资料分类-批量删除")@ApiOperation(value = "影像资料分类-批量删除", notes = "影像资料分类-批量删除")//@RequiresPermissions("image:img_classification:deleteBatch")@DeleteMapping(value = "/deleteBatch")public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.imgClassificationService.removeByIds(Arrays.asList(ids.split(",")));imgClassificationService.deleteBatch(ids);return Result.OK("批量删除成功!");}/*** 通过id查询** @param id* @return*///@AutoLog(value = "影像资料分类-通过id查询")@ApiOperation(value = "影像资料分类-通过id查询", notes = "影像资料分类-通过id查询")@GetMapping(value = "/queryById")public Result<ImgClassification> queryById(@RequestParam(name = "id", required = true) String id) {
//        ImgClassification imgClassification = imgClassificationService.getById(id);ImgClassification imgClassification = imgClassificationService.queryById(id);if (imgClassification == null) {return Result.error("未找到对应数据");}return Result.OK(imgClassification);}/*** 导出excel** @param request* @param imgClassification*///@RequiresPermissions("image:img_classification:exportXls")@RequestMapping(value = "/exportXls")public ModelAndView exportXls(HttpServletRequest request, ImgClassification imgClassification) {return super.exportXls(request, imgClassification, ImgClassification.class, "影像资料分类");}/*** 通过excel导入数据** @param request* @param response* @return*///@RequiresPermissions("image:img_classification:importExcel")@RequestMapping(value = "/importExcel", method = RequestMethod.POST)public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {return super.importExcel(request, response, ImgClassification.class);}/*** 获取树结构** @param dto* @return*/@PostMapping("/getTree")@ApiOperation(value = "获取树结构")public Result<?> getTree(@ApiParam(value = "查询条件")@RequestBody ImgClassification dto) {List<ImgClassification> tree = imgClassificationService.getTree(dto);return Result.OK(tree);}/*** 设置innercode码*/@ApiOperation(value = "设置innercode码", notes = "设置innercode码")@GetMapping(value = "/initCode")public Result<?> initCode() {imgClassificationService.initCode();return Result.OK("处理完成");}}

5、service层

package org.jeecg.modules.image.service;import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.modules.image.entity.ImgClassification;import java.util.List;/*** @Description: 影像资料分类* @Author: jeecg-boot* @Date: 2024-05-28* @Version: V1.0*/
public interface IImgClassificationService extends IService<ImgClassification> {List<ImgClassification> selectTree(ImgClassification dto);List<ImgClassification> getTree(ImgClassification dto);void initCode();void add(ImgClassification imgClassification);void delete(String id);void addBatch(List<ImgClassification> imgClassifications);void deleteBatch(String ids);ImgClassification queryById(String id);
}

package org.jeecg.modules.image.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.BusinessException;
import org.jeecg.common.system.base.service.BaseService;
import org.jeecg.common.util.StringUtil;
import org.jeecg.modules.image.entity.ImgClassification;
import org.jeecg.modules.image.entity.ImgLibrary;
import org.jeecg.modules.image.mapper.ImgClassificationMapper;
import org.jeecg.modules.image.service.IImgClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;/*** @Description: 影像资料分类* @Author: jeecg-boot* @Date: 2024-05-28* @Version: V1.0*/
@Service
public class ImgClassificationServiceImpl extends ServiceImpl<ImgClassificationMapper, ImgClassification> implements IImgClassificationService {@ResourceImgClassificationMapper mapper;@Autowiredprivate BaseService<ImgClassification> baseService;@Autowiredprivate BaseService<ImgLibrary> imgLibraryService;@Overridepublic List<ImgClassification> selectTree(ImgClassification dto) {return mapper.selectTree(dto);}@Overridepublic List<ImgClassification> getTree(ImgClassification dto) {List<ImgClassification> returndemo = this.selectTree(dto);List<ImgClassification> tree = Collections.synchronizedList(new ArrayList<>());String id = "1";if (StringUtil.isNotEmpty(dto.getId())) {id = dto.getId();}if (StringUtil.isNotEmpty(returndemo)) {String finalId = id;returndemo.parallelStream().forEach(x -> {if (finalId.equals(x.getId())) {//现在做全局递归,之后更改tree.add(findChildren(x, returndemo));}});}//根据日期进行升序排序for (ImgClassification e : tree) {e.setChildren(sort(e.getChildren()));}List<ImgClassification> treeAsce = sort(tree);return treeAsce;}private List<ImgClassification> sort(List<ImgClassification> source) {List<ImgClassification> treeAsce= source.stream().sorted(Comparator.comparing(ImgClassification::getSort)).collect(Collectors.toList());for (ImgClassification e : source) {if (e.getChildren().size() > 0) {e.setChildren(sort(e.getChildren()));}}return treeAsce;}/*** @param node  .* @param lists .* @return .*/private static ImgClassification findChildren(ImgClassification node, List<ImgClassification> lists) {lists.parallelStream().forEach(y -> {if (node.getId().equals(y.getPid())) {if (node.getChildren() == null) {node.setChildren(new ArrayList<ImgClassification>());}node.getChildren().add(findChildren(y, lists));}});return node;}@Overridepublic void initCode() {ImgClassification dto = new ImgClassification();List<ImgClassification> treeList = getTree(dto);dealInnerCode(treeList, true);}@Overridepublic void addBatch(List<ImgClassification> imgClassifications) {for (ImgClassification imgClassification : imgClassifications) {this.add(imgClassification);}}@Overridepublic void add(ImgClassification imgClassification) {//更新父节点的hasChildrenImgClassification parent = this.queryById(imgClassification.getPid());if (StringUtil.isEmpty(parent)) {throw new BusinessException("未找到父节点");}parent.setHasChildren(true);this.updateById(parent);//更新本节点的层级、编码、排序imgClassification = this.reDoEntity(parent, imgClassification);//新增this.save(imgClassification);}/*** 获取分类的 层级layer、 编码code、 排序sort*/private ImgClassification reDoEntity(ImgClassification parent, ImgClassification imgClassification) {String code = null;//当前父节点有几个子节点List<ImgClassification> childrenList = parent.getChildren();int lastNumber = 1;if (StringUtil.isEmpty(childrenList)) {// 列表为空或null,处理这种情况code = parent.getCode() + "-1";} else {//查询父节点下最新的一条记录ImgClassification latestItem = childrenList.get(0); // 假设第一个元素是最新的String latestCode = latestItem.getCode();// 按 "-" 分割字符串String[] parts = latestCode.split("-");// 检查是否至少有一个部分if (parts.length == 0) {System.out.println("没有分隔符,找不到数值");}// 找到最后一个编号,并转换为整数lastNumber = Integer.parseInt(parts[parts.length - 1]);// 将最后一个编号加1lastNumber++;// 将加1后的整数转换回字符串String lastNumberStr = String.valueOf(lastNumber);// 替换原来的最后一个编号,并用 "-" 重新连接所有编号// 如果只有一个部分,则不需要"-"StringBuilder newS = new StringBuilder();for (int i = 0; i < parts.length - 1; i++) {newS.append(parts[i]).append("-");}newS.append(lastNumberStr);code = newS.toString();}imgClassification.setCode(code);imgClassification.setLayer(parent.getLayer() + 1);if (StringUtil.isEmpty(imgClassification.getSort())) {imgClassification.setSort(lastNumber + 1);}//更新本节点的hasChildrenimgClassification.setHasChildren(false);return imgClassification;}@Overridepublic void deleteBatch(String ids) {List<String> idList = Arrays.asList(ids.split(","));if (StringUtil.isNotEmpty(idList)) {for (String id : idList) {this.delete(id);}}}@Overridepublic ImgClassification queryById(String id) {ImgClassification imgClassification = this.getById(id);//获取子节点ImgClassification dto = new ImgClassification();dto.setPid(id);dto.setOrderby("create_time desc");List<ImgClassification> childrenList = baseService.selectlistByDto(dto);imgClassification.setChildren(childrenList);return imgClassification;}/*** 删除分类* 1.根节点不允许删除* 2.节点下挂了资料,不能删除* 3.更新父节点的hasChildren* 4.删除本节点** @param id*/@Overridepublic void delete(String id) {ImgClassification imgClassification = this.getById(id);//根节点不允许删除if (id.equals("1")) {throw new BusinessException("根节点不允许删除。");}//判断分类下是否有资料,如果有,则不能删除if (hasData(imgClassification)) {throw new BusinessException(imgClassification.getName() + "分类下存在资料,不能删除。" + imgClassification.getCode() + ":" + imgClassification.getId());}//更新父节点的hasChildrenif (imgClassification != null) {     //如果删除的是顶级节点,则父节点的hasChildren置为falseImgClassification parent = this.queryById(imgClassification.getPid());if (parent != null) {List<ImgClassification> childrenList = parent.getChildren();if (StringUtil.isNotEmpty(childrenList) && childrenList.size() == 1) {parent.setHasChildren(false);this.updateById(parent);}}}//删除本节点this.removeById(id);}private Boolean hasData(ImgClassification imgClassification) {ImgLibrary dto = new ImgLibrary();dto.setClsCode(imgClassification.getCode());List<ImgLibrary> imgLibraryList = imgLibraryService.selectlistByDto(dto);if (imgLibraryList != null && !imgLibraryList.isEmpty()) {return true;}return false;}public void dealInnerCode(List<ImgClassification> treeList, Boolean isTop) {for (int i = 0; i < treeList.size(); i++) {String thisId = treeList.get(i).getId();String pId = treeList.get(i).getPid();ImgClassification imgClassification = this.getById(thisId);if (isTop) {imgClassification.setCode("0");} else {imgClassification.setCode(getCodeById(pId) + (i + 1));}//更新this.updateById(imgClassification);List<ImgClassification> childrenList = treeList.get(i).getChildren();if (StringUtil.isNotEmpty(childrenList)) {dealInnerCode(childrenList, false);}}}public String getCodeById(String id) {ImgClassification imgClassification = this.getById(id);return imgClassification.getCode();}
}

6、dao层

package org.jeecg.modules.image.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.jeecg.modules.image.entity.ImgClassification;import java.util.List;/*** @Description: 影像资料分类* @Author: jeecg-boot* @Date: 2024-05-28* @Version: V1.0*/
public interface ImgClassificationMapper extends BaseMapper<ImgClassification> {List<ImgClassification> selectTree(@Param("dto") ImgClassification dto);
}

7、xml层

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.jeecg.modules.image.mapper.ImgClassificationMapper"><!-- 树结构-查父节点的名字 --><select id="selectTree"parameterType="Object"resultType="org.jeecg.modules.image.entity.ImgClassification">SELECT t1.name AS parent_name,t.*FROM IMG_CLASSIFICATION tLEFT JOIN IMG_CLASSIFICATION t1 ON t.pid = t1.idWHERE 1 = 1<if test="dto.name !=null and dto.name != ''">AND t.name like concat(concat('%',#{dto.name}),'%')</if>ORDER BYt1.CREATE_TIME,t.CREATE_TIME</select></mapper>

这篇关于java框架树结构实现(带层级、编码、排序)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1036311

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听