【springboot+mybatis-plus】树状菜单

第一种方法:在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;
    }

测试结果:
【springboot+mybatis-plus】树状菜单_第1张图片

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

你可能感兴趣的:(【springboot+mybatis-plus】树状菜单)