分类管理Tree结构

1、目标

{
    "success": true,
    "status": 200,
    "message": "获取成功",
    "data": [
        {
            "id": "100",
            "className": "法外狂徒",
            "children": [
                {
                    "id": "101",
                    "className": "A站点",
                    "children": [
                        {
                            "id": "103",
                            "className": "A下站点a",
                            "children": [
                                {
                                    "id": "106",
                                    "className": "Aa3站点",
                                    "children": [],
                                    "ancestors": "0,100,101,103",
                                    "companyId": "40",
                                    "creatorId": null,
                                    "creatorTime": null,
                                    "updaterId": null,
                                    "updateTime": null
                                }
                            ],
                            "ancestors": "0,100,101",
                            "companyId": "40",
                            "creatorId": null,
                            "creatorTime": null,
                            "updaterId": null,
                            "updateTime": null
                        },
                        {
                            "id": "104",
                            "className": "A下站点b",
                            "children": [
                                {
                                    "id": "113",
                                    "className": "王五",
                                    "children": [],
                                    "ancestors": "0,100,101,104",
                                    "companyId": "40",
                                    "creatorId": "111",
                                    "creatorTime": null,
                                    "updaterId": null,
                                    "updateTime": null
                                }
                            ],
                            "ancestors": "0,100,101",
                            "companyId": "40",
                            "creatorId": null,
                            "creatorTime": null,
                            "updaterId": null,
                            "updateTime": null
                        }
                    ],
                    "ancestors": "0,100",
                    "companyId": "40",
                    "creatorId": null,
                    "creatorTime": null,
                    "updaterId": null,
                    "updateTime": null
                },
                {
                    "id": "102",
                    "className": "B站点",
                    "children": [
                        {
                            "id": "105",
                            "className": "B站点a",
                            "children": [],
                            "ancestors": "0,100.102",
                            "companyId": "40",
                            "creatorId": null,
                            "creatorTime": null,
                            "updaterId": null,
                            "updateTime": null
                        }
                    ],
                    "ancestors": "0,100",
                    "companyId": "40",
                    "creatorId": null,
                    "creatorTime": null,
                    "updaterId": null,
                    "updateTime": null
                }
            ],
            "ancestors": "0",
            "companyId": "40",
            "creatorId": null,
            "creatorTime": "2021-08-20 16:16:06",
            "updaterId": null,
            "updateTime": null
        },
        {
            "id": "112",
            "className": "王五",
            "children": [],
            "ancestors": "0",
            "companyId": "40",
            "creatorId": "111",
            "creatorTime": null,
            "updaterId": null,
            "updateTime": null
        },
        {
            "id": "114",
            "className": "13分点",
            "children": [],
            "ancestors": "0",
            "companyId": "40",
            "creatorId": null,
            "creatorTime": null,
            "updaterId": null,
            "updateTime": null
        },
        {
            "id": "115",
            "className": "14分点",
            "children": [],
            "ancestors": "0",
            "companyId": "40",
            "creatorId": null,
            "creatorTime": null,
            "updaterId": null,
            "updateTime": null
        }
    ]
}

2、数据库表
表设计
表内容
表内容
3.实体类

@Data
public class EquipmentClass {
    private  Long id;                   //分类ID
    private  String className;          //分类名称
    private  String parentId;            //父级ID
    private  String ancestors;          //祖级列表
    private  String companyId;          //公司ID
    private  String  creatorId;           //创建人ID
    private  String creatorTime;        //创建时间
    private  String   updaterId;          //更新人
    private  String updateTime;         //更新时间
}

tree实体类

//分类节点类
@Data
public class TreeNode implements Serializable {

    private  String id;      //分类ID
    private  String className;  //分类名称
    private  List<TreeNode> children = new ArrayList<TreeNode>();
    private  String ancestors;  //祖级列表
    private  String companyId;    //公司ID
    private  String  creatorId;           //创建人ID
    private  String creatorTime;        //创建时间
    private  String   updaterId;          //更新人
    private  String updateTime;         //更新时间

}

4、Mapper

    <!--查询列表 getProjectList-->
    <select id="getProjectList"  resultMap="ProjectMap">
        <bind name="notIdlist" value="(pageNum-1)*rowCount"/>
        select
        *
        from
        om_fnd_project_mgmt
        where
        company_id = #{companyId}
        <if test="searchContent !=null">
            and  project_name like '%${searchContent}%'
        </if>
        order by
        project_id
        desc
        <if test="pageNum!=0">
            LIMIT
            #{notIdlist},#{rowCount}
        </if>
    </select>

5、DAO层

@Mapper
public interface EquipmentClassDao {
	//获取分类
   public List<EquipmentClass> getEquipmentClassList(String companyId) throws  Exception;
   }

6、service层

public interface EquipmentClassService {
 public Object getEquipmentClassList(String companyId) throws  Exception;
 }

7、serviceImpl层

@Service("EquipmentClassService")
@Transactional
public class EquipmentClassServiceImpl implements EquipmentClassService {

    @Autowired
    private EquipmentClassDao equipmentClassDao;
     @Override
    public Object getEquipmentClassList(String companyId) throws Exception {
        List<EquipmentClass> equipmentClassList = equipmentClassDao.getEquipmentClassList(companyId);

        System.out.println(equipmentClassList);

        List<EquipmentClass> dList = equipmentClassList;
        Set<String> idSet=new HashSet<>();
        Map<String, List<TreeNode>> map=new HashMap<String, List<TreeNode>>();
        TreeNode node=null;

        for (EquipmentClass equipmentClass : dList) {
            if(!idSet.contains(equipmentClass.getId())) {
                node = new TreeNode();
                node.setId(equipmentClass.getId()+"");     //ID
                node.setClassName(equipmentClass.getClassName());   //名字
                node.setAncestors(equipmentClass.getAncestors());   //祖级列表
                node.setCompanyId(equipmentClass.getCompanyId());   //公司ID
                node.setCreatorId(equipmentClass.getCreatorId());   //创建人ID
                node.setCreatorTime(equipmentClass.getCreatorTime());//创建时间
                node.setUpdaterId(equipmentClass.getUpdaterId());//更新人
                node.setCreatorTime(equipmentClass.getUpdateTime());//更新时间
                if(map.get(equipmentClass.getParentId())==null) {       //生成父ID节点
                    map.put(equipmentClass.getParentId()+"", new ArrayList<>());
                }

                map.get(equipmentClass.getParentId()).add(node);

                idSet.add(equipmentClass.getId()+"");
            }
        }
        Iterator<String> it=map.keySet().iterator();
        String id=null;
        while(it.hasNext()) {
            id = it.next();
            List<TreeNode> childeNodes=map.get(id);
            for (TreeNode treeNode : childeNodes) {
                if(map.get(treeNode.getId())!=null) {
                    treeNode.getChildren().addAll(map.get(treeNode.getId()));
                }
            }
        }

//        System.out.println(JSONObject.toJSON(map.get("0")));
//        Object object = JSONObject.toJSON(map.get(100));
//        System.out.println(map.get("0"));
        return new Result(200,"获取成功",true,map.get("0"));

    }
    }

8、Controller层

@Api(tags = "设备分类")
@ApiSort(3)
@RequestMapping("/equipmentClass")
@RestController
@CrossOrigin
public class EquipmentClassController {

    @Autowired
    private EquipmentClassService equipmentClassService;
    
    @ApiOperation(value = "设备分类tree结构")
    @RequestMapping(value="/getEquipmentClassList",method= RequestMethod.GET)
    @CrossOrigin
    public Object getEquipmentClassList(@RequestParam String companyId) throws Exception {
        return equipmentClassService.getEquipmentClassList(companyId);
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值