【springboot+mybatis-plus】树状菜单

Springboot+Mybatis-plus 专栏收录该内容
4 篇文章 0 订阅

第一种方法:在xml中实现 最后没采用 因为要一直查询数据库

实体类:

public class FormulaClothClass extends Model<FormulaClothClass> {
    @TableId(value = "formula_cloth_class_id", type = IdType.AUTO)
    private Integer formulaClothClassId;
    private String formulaClothClassName;
    private Integer parentId;
    private Integer status;
    private Integer createId;
    private Date createTime;
    private Integer updateId;
    private Date updateTime;   //省略了getset

VO:

public class FormulaClothClassVO {
    @TableId(value = "formula_cloth_class_id", type = IdType.AUTO)
    private Integer formulaClothClassId;
    private String formulaClothClassName;
    private Integer parentId;
    private Integer status;
    private Integer createId;
    private Date createTime;
    private Integer updateId;
    private Date updateTime;
    private List<FormulaClothClassVO> treeNode;    //省略getset

xml:

<resultMap id="BaseTreeResultMap" type="com.hgfzp.textile.erp.vo.FormulaClothClassVO">
        <id column="formula_cloth_class_id" property="formulaClothClassId" />
        <result column="formula_cloth_class_name" property="formulaClothClassName" />
        <result column="parent_id" property="parentId" />
        <result column="status" property="status" />
        <result column="create_id" property="createId" />
        <result column="create_time" property="createTime" />
        <result column="update_id" property="updateId" />
        <result column="update_time" property="updateTime" />
        <collection column="formula_cloth_class_id" property="treeNode" javaType="java.util.ArrayList"
                    ofType="com.hgfzp.textile.erp.vo.FormulaClothClassVO" select="getNextNodeTree"/>
    </resultMap>

    <resultMap id="NextTreeResultMap" type="com.hgfzp.textile.erp.vo.FormulaClothClassVO">
        <id column="formula_cloth_class_id" property="formulaClothClassId" />
        <result column="formula_cloth_class_name" property="formulaClothClassName" />
        <result column="parent_id" property="parentId" />
        <result column="status" property="status" />
        <result column="create_id" property="createId" />
        <result column="create_time" property="createTime" />
        <result column="update_id" property="updateId" />
        <result column="update_time" property="updateTime" />
        <collection column="formula_cloth_class_id" property="treeNode" javaType="java.util.ArrayList"
                    ofType="com.hgfzp.textile.erp.vo.FormulaClothClassVO" select="getNextNodeTree"/>
    </resultMap>

    <select id="getNextNodeTree" resultMap="NextTreeResultMap">
        SELECT
        *
        FROM formula_cloth_class
        WHERE parent_id = #{formula_cloth_class_id} AND status = 1
    </select>

    <select id="getNodeTree" resultMap="BaseTreeResultMap">
        SELECT
        *
        FROM formula_cloth_class
        WHERE parent_id = 0 AND status = 1
    </select>

Dao:

FormulaClothClassVO getNodeTree();

Controller:

@GetMapping(value = "formulaClothClassTree")
    @ApiOperation(value="树", notes="树",httpMethod = "GET")
    public Result<FormulaClothClassVO> formulaClothClassTree(){
        return ResultGenerator.genSuccessResult(formulaClothClassService.formulaClothClassTree());
    }

Service:

Result<FormulaClothClassVO> formulaClothClassTree();

impl:

@Override
    public Result<FormulaClothClassVO> formulaClothClassTree() {
        return ResultGenerator.genSuccessResult(formulaClothClassMapper.getNodeTree());
    }

第二种方法:一次性全查询 再用查询出的list做遍历
Controller:

@GetMapping(value = "formulaClothClassGetTree")
    @ApiOperation(value="树", notes="树",httpMethod = "GET")
    public Result<FormulaClothClassVO> formulaClothClassGetTree(){
        return ResultGenerator.genSuccessResult(formulaClothClassService.formulaClothClassGetTree());
    }

service:

List<FormulaClothClassVO> formulaClothClassGetTree();

List<FormulaClothClassVO> formulaClothClassNextTree(List<FormulaClothClass> list, int formulaClothClassId);

impl:

@Override
    public List<FormulaClothClassVO> formulaClothClassGetTree() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", 1);
        List<FormulaClothClass> list = formulaClothClassMapper.selectList(queryWrapper);
        List<FormulaClothClassVO> list1 = new ArrayList();
        for(FormulaClothClass formulaClothClass : list){
            FormulaClothClassVO formulaClothClassVO = new FormulaClothClassVO();
            if(formulaClothClass.getParentId().equals(0)){
                BeanUtils.copyProperties(formulaClothClass, formulaClothClassVO);
                formulaClothClassVO.setTreeNode(formulaClothClassNextTree(list,(formulaClothClass.getFormulaClothClassId())));
                list1.add(formulaClothClassVO);
            }
        }
        return list1;
    }

    @Override
    public List<FormulaClothClassVO> formulaClothClassNextTree(List<FormulaClothClass> list, int formulaClothClassId) {
        List<FormulaClothClassVO> list1 = new ArrayList();
        for(FormulaClothClass formulaClothClass : list){
            FormulaClothClassVO formulaClothClassVO = new FormulaClothClassVO();
            if(formulaClothClass.getParentId().equals(formulaClothClassId)){
                BeanUtils.copyProperties(formulaClothClass, formulaClothClassVO);
                formulaClothClassVO.setTreeNode(formulaClothClassNextTree(list,formulaClothClass.getFormulaClothClassId()));
                list1.add(formulaClothClassVO);
            }
        }
        return list1;
    }

第三种方法:直接调用方法…

@Override
    public List<RootMemuVO> selectRootTree() {
        // 从数据库获取全部数据
        QueryWrapper queryWrapper=new QueryWrapper();
        List<OaRoot> menus = oaRootMapper.selectList(queryWrapper);
        //OaRoot对象复制给rootMemuVO方便进行转换
        List<RootMemuVO> rootMemuVOS = new ArrayList<>();
        for (OaRoot oaRoot : menus) {
            RootMemuVO rootMemuVO = new RootMemuVO();
            BeanUtils.copyProperties(oaRoot, rootMemuVO);
            rootMemuVOS.add(rootMemuVO);
        }
        // 调用转换方法
        List<RootMemuVO> ros = TreeUtil.toTree(rootMemuVOS);
        return ros;
    }

测试结果:
在这里插入图片描述

参考链接:
https://blog.csdn.net/zhwxl_zyx/article/details/48995737
https://blog.csdn.net/qq_38164123/article/details/94358131

  • 4
    点赞
  • 1
    评论
  • 16
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

评论 1 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页

打赏作者

陈清纯

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值