java从数据库读取菜单,递归生成菜单树

java从数据库读取菜单,递归生成菜单树

这里我以查询课程列表来判断
实体类

/**
 * <p>
 * 课程目录
 * </p>
 *
 * @author cgb
 * @since 2020-07-27
 */
@Data
@ToString
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_course_type")
public class CourseType extends Model<CourseType> {

    private static final long serialVersionUID=1L;

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

    @TableField("createTime")
    private Long createTime;

    @TableField("updateTime")
    private Long updateTime;

    /**
     * 类型名
     */
    private String name;

    /**
     * 父ID
     */
    private Long pid;

    /**
     * 图标
     */
    private String logo;

    /**
     * 描述
     */
    private String description;

    @TableField("sortIndex")
    private Integer sortIndex;

    /**
     * 路径
     */
    private String path;
    //把添加的字段忽略
    @TableField(exist = false)
    private List<CourseType> children = new ArrayList<>();
    /**
     * 课程数量
     */
    @TableField("totalCount")
    private Integer totalCount;


    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}

mapper层通过mybatis-plus生成方法,不会也没有关系,写一个查询所有的方法

<?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="cn.itsource.mapper.CourseTypeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.itsource.domain.CourseType">
        <id column="id" property="id" />
        <result column="createTime" property="createTime" />
        <result column="updateTime" property="updateTime" />
        <result column="name" property="name" />
        <result column="pid" property="pid" />
        <result column="logo" property="logo" />
        <result column="description" property="description" />
        <result column="sortIndex" property="sortIndex" />
        <result column="path" property="path" />
        <result column="totalCount" property="totalCount" />
        <!--<collection property="children" column="id" select="findChildren"/>-->
    </resultMap>
service层写业务逻辑,每一次调用自己的方法,最后一层由于没有获取到值跳出

  @Override
    public List<CourseType> findTree() {
        // 原始的数据一条一条的
        //查询所有课程类型
        List<CourseType> list = mapper.selectList(new QueryWrapper());
        // 构建好的菜单树,第一层菜单的pid是null
        List<CourseType> CourseTypeTree = buildMenuTree(list, 0L);
        return CourseTypeTree;
    }

    /**
     * 构建菜单树
     *
     * @param menuList
     * @param pid
     * @return
     */
    private List<CourseType> buildMenuTree(List<CourseType> menuList, Long pid) {
        List<CourseType> treeList = new ArrayList<>();
        menuList.forEach(menu -> {
       //Long类型不能直接==,需要调用方法才能判断,否则查不出值
            if (pid.longValue()==menu.getPid().longValue()) {
                menu.setChildren(buildMenuTree(menuList, menu.getId()));
                treeList.add(menu);
            }
        });
        return treeList;
    }

测试

CourseType(id=1037, createTime=1570692683279, updateTime=null, name=IT课程11, pid=0, logo=null, description=呵呵, sortIndex=100, path=.1037., children=[CourseType(id=1039, createTime=null, updateTime=null, name=Java技术, pid=1037, logo=null, description=嘿嘿, sortIndex=100, path=.1037.1039., children=[CourseType(id=1040, createTime=null, updateTime=null, name=java基础, pid=1039, logo=null, description=啦啦啦, sortIndex=100, path=.1037.1039.1040., children=[], totalCount=0), CourseType(id=1065, createTime=1569232763222, updateTime=null, name=1, pid=1039, logo=1, description=1, sortIndex=1, path=.1037.10391065, children=[], totalCount=0), CourseType(id=1066, createTime=1569233255858, updateTime=null, name=2, pid=1039, logo=2, description=2, sortIndex=2, path=.1037.1039.1066, children=[], totalCount=0)], totalCount=0), CourseType(id=1042, createTime=null, updateTime=null, name=UI初级, pid=1037, logo=null, description=null, sortIndex=100, path=null, children=[CourseType(id=1041, createTime=null, updateTime=null, name=UI基础, pid=1042, logo=null, description=null, sortIndex=100, path=null, children=[], totalCount=0), CourseType(id=1058, createTime=1569228646958, updateTime=null, name=9, pid=1042, logo=9, description=9, sortIndex=9, path=.1037.1058, children=[], totalCount=0)], totalCount=0), CourseType(id=1056, createTime=1569227829802, updateTime=null, name=7, pid=1037, logo=7, description=7, sortIndex=7, path=.1037.1056, children=[], totalCount=0), CourseType(id=1057, createTime=1569228581868, updateTime=null, name=8, pid=1037, logo=8, description=8, sortIndex=8, path=.1037.1057, children=[], totalCount=0), CourseType(id=1059, createTime=1569228725384, updateTime=null, name=11, pid=1037, logo=11, description=11, sortIndex=11, path=.1037.1059, children=[], totalCount=0), CourseType(id=1060, createTime=1569228751447, updateTime=null, name=22, pid=1037, logo=22, description=22, sortIndex=22, path=.1037.1060, children=[], totalCount=0), CourseType(id=1061, createTime=1569228766042, updateTime=null, name=33, pid=1037, logo=33, description=33, sortIndex=33, path=.1037.1061, children=[], totalCount=0), CourseType(id=1062, createTime=1569228804614, updateTime=null, name=111, pid=1037, logo=111, description=111, sortIndex=111, path=.1037.1062, children=[], totalCount=0), CourseType(id=1063, createTime=1569228986180, updateTime=null, name=333, pid=1037, logo=333, description=333, sortIndex=333, path=.1037.1063, children=[], totalCount=0), CourseType(id=1064, createTime=1569229000537, updateTime=null, name=4444, pid=1037, logo=4444, description=4444, sortIndex=4444, path=.1037.1064, children=[], totalCount=0)], totalCount=10)
CourseType(id=1038, createTime=null, updateTime=null, name=财务课程, pid=0, logo=null, description=哈哈, sortIndex=100, path=.1038., children=[], totalCount=0)
CourseType(id=1043, createTime=null, updateTime=null, name=设计, pid=0, logo=null, description=null, sortIndex=100, path=null, children=[CourseType(id=1044, createTime=null, updateTime=null, name=画图初级, pid=1043, logo=null, description=梵高梦想, sortIndex=100, path=null, children=[], totalCount=0), CourseType(id=1045, createTime=null, updateTime=null, name=画图中级, pid=1043, logo=null, description=梵高梦想, sortIndex=100, path=null, children=[], totalCount=0), CourseType(id=1046, createTime=null, updateTime=null, name=画图高级, pid=1043, logo=null, description=梵高梦想, sortIndex=100, path=null, children=[], totalCount=0), CourseType(id=1049, createTime=null, updateTime=null, name=1, pid=1043, logo=1, description=1, sortIndex=1, path=null, children=[], totalCount=0), CourseType(id=1054, createTime=1569226471931, updateTime=null, name=6, pid=1043, logo=6, description=6, sortIndex=6, path=.1043.1054., children=[], totalCount=0), CourseType(id=1055, createTime=1569226920152, updateTime=null, name=7, pid=1043, logo=7, description=7, sortIndex=7, path=.1043.1055, children=[], totalCount=0)], totalCount=0)
CourseType(id=1047, createTime=null, updateTime=null, name=匿名, pid=0, logo=null, description=null, sortIndex=100, path=null, children=[], totalCount=0)
CourseType(id=1048, createTime=null, updateTime=null, name=匿名, pid=0, logo=null, description=null, sortIndex=100, path=null, children=[], totalCount=0)
2020-07-28 11:32:48.866  INFO 5916 --- [       Thread-9] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@6d0b5baf: startup date [Tue Jul 28 11:32:12 CST 2020]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@44c73c26

成功查询出数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值