读取数据库数据返回树形结构List

该博客展示了如何利用Lombok注解简化Java实体类,结合MyBatis从数据库获取数据,然后通过工具类将查询到的菜单数据转换成树形结构,用于前端展示。主要涉及Maven依赖、实体类、工具类、Dao、Service及Controller层的代码实现。
摘要由CSDN通过智能技术生成

数据库字段以及测试数据

 Maven所需依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

工具类

import com.computer.entity.Tree;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MenuTreeUtil {
    public List<Tree> menuCommon;
    public List<Object> list = new ArrayList<>();

    public List<Object> menuList(List<Tree> menu) {//controller层调用的方法,并将数据以list的形式返回
        this.menuCommon = menu;
        for (Tree t : menu) {
            LinkedHashMap<String, Object> mapArr = new LinkedHashMap<>();
            if (t.getIsparent().equals("true") && t.getParent_code().equals("0")) {//判断是否为父级
                mapArr.put("id",t.getId());
                mapArr.put("tree_name",t.getTree_name());
                mapArr.put("parent_code",t.getParent_code());
                mapArr.put("hasChildren",t.getIsparent());
                mapArr.put("tree_img",t.getTree_img());

                mapArr.put("child",menuChild(t.getId()));//去子集查找遍历
                list.add(mapArr);
            }
        }
        return list;
    }
    public List<?> menuChild(String id){//子集查找遍历
        List<Object> lists = new ArrayList<>();
        for (Tree a : menuCommon) {
            Map<String, Object> childArray = new LinkedHashMap<>();
            if (a.getParent_code().equals(id)){
                childArray.put("id",a.getId());
                childArray.put("tree_name",a.getTree_name());
                childArray.put("parent_code",a.getParent_code());
                childArray.put("tree_img",a.getTree_img());
                childArray.put("hasChildren",a.getIsparent());
                childArray.put("child",menuChild(a.getId()));
                lists.add(childArray);
            }
        }
        return lists;
    }
}

实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class PageData {
    private String id;//主键
    private String parentCode;//父级编码
    private String treeName;//树结构的名字
    private String isParent;//是否是父级
    private String treeImg;//树结构的图片

}
package com.computer.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Tree {
    private String id;//主键
    private String parent_code;//父级编码
    private String tree_name;//树结构的名字

    private String isparent;//是否是父级
    private String tree_img;//树结构的图片
}

Dao

import com.computer.entity.PageData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MenuTreeDao {

    @Select("select * from pageData")
    List<PageData> selectMenuTree();
}

Service

import com.computer.entity.PageData;

import java.util.List;

public interface RegionTreeService {
    List<PageData> findReagionTreePage();
}
import com.computer.dao.MenuTreeDao;
import com.computer.entity.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionTreeServiceImpl implements RegionTreeService {
    @Autowired
    MenuTreeDao menuTreeDao;
    @Override
    public List<PageData> findReagionTreePage() {
        return menuTreeDao.selectMenuTree();
    }
}

Controller

import com.computer.entity.PageData;
import com.computer.entity.Tree;
import com.computer.service.RegionTreeServiceImpl;
import com.computer.utils.MenuTreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MenuTreeController {
    @Autowired
    RegionTreeServiceImpl regionTreeService;

    @GetMapping(value = "/queryTree")
    public Object findPage(){
        Map<String, Object> returnMap = new HashMap<>();
        List<PageData> lists = regionTreeService.findReagionTreePage();//所有数据未转换成树结构

        MenuTreeUtil menuTree = new MenuTreeUtil();//定义工具类
        List<Tree> lt = new ArrayList<>();//定义实体类
        for (int i = 0; i < lists.size(); i++) {
            Tree t = new Tree();//转换成对象
            t.setId(lists.get(i).getId());//将数据赋给实体类
            t.setTree_name(lists.get(i).getTreeName());
            t.setParent_code(lists.get(i).getParentCode());
            t.setIsparent(lists.get(i).getIsParent());
            t.setTree_img(lists.get(i).getTreeImg());
            lt.add(t);
        }
        List<Object> menuList = menuTree.menuList(lt);//将所有数据转换成树结构
        returnMap.put("list",menuList);
        return returnMap;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值