springboot 整合 mybatis plus -- 树状图

 

先根据 springboot 整合 mybatis plus  这篇生成各种文件

 

controller:

package com.lifan.controller;


import com.lifan.service.AcUserTreeService;
import com.lifan.vo.AcUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lifan
 * @since 2021-07-09
 */
@RestController
public class AcUserTreeController {

    @Autowired
    private AcUserTreeService acUserTreeService;

    @PostMapping("/treeQuery")
    public List<AcUserVo> SspFxProQuery() {
        List<AcUserVo> fxCompanyVos = acUserTreeService.formulaClothClassGetTree();

        return fxCompanyVos;
    }

}

 

service:

package com.lifan.service;

import com.lifan.vo.AcUserVo;
import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author lifan
 * @since 2021-07-09
 */
public interface AcUserTreeService {

    List<AcUserVo> formulaClothClassGetTree();
}

 

AcUserTreeServiceImpl :

package com.lifan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lifan.entity.AcUserTree;
import com.lifan.mapper.AcUserTreeMapper;
import com.lifan.service.AcUserTreeService;
import com.lifan.vo.AcUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lifan
 * @since 2021-07-09
 */
@Service
public class AcUserTreeServiceImpl implements AcUserTreeService {

    @Autowired
    private AcUserTreeMapper acUserTreeMapper;

    public List<AcUserVo> formulaClothClassGetTree() {
        QueryWrapper queryWrapper = new QueryWrapper();
        //只查询 启用状态的
//        queryWrapper.eq("`show`",'Y');
        List<AcUserTree> list = acUserTreeMapper.selectList(queryWrapper);
        List<AcUserVo> list1 = new ArrayList();
        for(AcUserTree sspFxCompany : list){
            AcUserVo fxCompanyVos = new AcUserVo();
            if(sspFxCompany.getPid().equals("1")){
                BeanUtils.copyProperties(sspFxCompany, fxCompanyVos);
                fxCompanyVos.setChildren(formulaClothClassNextTree(list,(sspFxCompany.getId())));
                list1.add(fxCompanyVos);

            }
        }
        return list1;
    }

    public List<AcUserVo> formulaClothClassNextTree(List<AcUserTree> list, Long formulaClothClassId) {
        List<AcUserVo> list1 = new ArrayList();
        for(AcUserTree sspFxCompany : list){
            AcUserVo fxCompanyVos = new AcUserVo();
            if(sspFxCompany.getPid().equals(formulaClothClassId.toString())){
                BeanUtils.copyProperties(sspFxCompany, fxCompanyVos);
                fxCompanyVos.setChildren(formulaClothClassNextTree1(list,sspFxCompany.getId()));
                list1.add(fxCompanyVos);
            }
        }
        return list1;
    }

    public List<AcUserVo> formulaClothClassNextTree1(List<AcUserTree> list, Long formulaClothClassId) {
        List<AcUserVo> list1 = new ArrayList();
        for(AcUserTree sspFxCompany : list){
            AcUserVo fxCompanyVos = new AcUserVo();
            if(sspFxCompany.getPid().equals(formulaClothClassId.toString())){
                BeanUtils.copyProperties(sspFxCompany, fxCompanyVos);
                fxCompanyVos.setChildren(formulaClothClassNextTree1(list,sspFxCompany.getId()));
                list1.add(fxCompanyVos);
            }
        }
        return list1;
    }

}

 

实体类:

package com.lifan.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 *
 * @author lifan
 * @since 2021-07-09
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class AcUserTree implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    private Long id;

    private String name;

    private String pid;

    //show 是数据库关键字,起个别名
    @TableField("`show`")
    private String show;
}

 

接受请求类:

package com.lifan.request;

import lombok.Data;


/**
 *
 * @author lifan
 * @since 2021-07-09
 */
@Data
public class AcUserTreeReq {

    private Long id;

    private String name;

    private String pid;

    private String show;

}

 

响应扩展类:

package com.lifan.vo;

import lombok.Data;

import java.util.List;

/**
 *
 * @author lifan
 * @since 2021-07-09
 */
@Data
public class AcUserVo {

    private Long id;

    private String name;

    private String pid;

    private String show;

    private List<AcUserVo> children;

}

 

Mapper 接口:

package com.lifan.mapper;

import com.lifan.entity.AcUserTree;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lifan
 * @since 2021-07-09
 */
@Mapper
public interface AcUserTreeMapper extends BaseMapper<AcUserTree> {

}

 

mapper.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="com.lifan.mapper.AcUserTreeMapper">

</mapper>

 

测试:

 

 

数据库:

pid为1 是一级结构,一级结构的id对应二级结构的pid, 下表最多的是5级

 

 

SQL :

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80023
 Source Host           : localhost:3306
 Source Schema         : ac-new

 Target Server Type    : MySQL
 Target Server Version : 80023
 File Encoding         : 65001

 Date: 09/07/2021 18:46:34
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for ac_user_tree
-- ----------------------------
DROP TABLE IF EXISTS `ac_user_tree`;
CREATE TABLE `ac_user_tree`  (
  `id` bigint(0) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `pid` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `show` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of ac_user_tree
-- ----------------------------
INSERT INTO `ac_user_tree` VALUES (1001, 'li', '1', 'Y');
INSERT INTO `ac_user_tree` VALUES (1002, 'wang', '1', 'Y');
INSERT INTO `ac_user_tree` VALUES (1003, 'zhao', '1', 'Y');
INSERT INTO `ac_user_tree` VALUES (1004, 'li-1', '1001', 'Y');
INSERT INTO `ac_user_tree` VALUES (1005, 'wang-1', '1002', 'Y');
INSERT INTO `ac_user_tree` VALUES (1006, 'wang-2', '1002', 'Y');
INSERT INTO `ac_user_tree` VALUES (1007, 'wang-3', '1002', 'Y');
INSERT INTO `ac_user_tree` VALUES (1008, 'zhao-1', '1003', 'Y');
INSERT INTO `ac_user_tree` VALUES (1009, 'zhao-2', '1003', 'Y');
INSERT INTO `ac_user_tree` VALUES (1010, 'li-1-1', '1004', 'Y');
INSERT INTO `ac_user_tree` VALUES (1011, 'li-1-2', '1004', 'Y');
INSERT INTO `ac_user_tree` VALUES (1012, 'li-1-1-1', '1010', 'Y');
INSERT INTO `ac_user_tree` VALUES (1013, 'li-1-1-2', '1010', 'Y');
INSERT INTO `ac_user_tree` VALUES (1014, 'li-1-1-3', '1010', 'Y');
INSERT INTO `ac_user_tree` VALUES (1015, 'li-1-2-1', '1011', 'Y');
INSERT INTO `ac_user_tree` VALUES (1016, 'li-1-2-2', '1011', 'Y');
INSERT INTO `ac_user_tree` VALUES (1017, 'li-1-2-3', '1011', 'Y');
INSERT INTO `ac_user_tree` VALUES (1018, 'wang-1-1', '1005', 'Y');
INSERT INTO `ac_user_tree` VALUES (1019, 'wang-1-2', '1005', 'Y');
INSERT INTO `ac_user_tree` VALUES (1020, 'wang-1-1-1', '1018', 'Y');
INSERT INTO `ac_user_tree` VALUES (1021, 'li-1-1-1-1', '1012', 'Y');
INSERT INTO `ac_user_tree` VALUES (1022, 'li-1-1-1-2', '1012', 'Y');

SET FOREIGN_KEY_CHECKS = 1;

 

posted @ 2021-07-09 18:47  Li&Fan  阅读(760)  评论(0编辑  收藏  举报