树形结构给大家看看:
一.测试用例,(直接复制代码,就可以看到效果)
1.实体类TreeVo:
package com.xinqiao.utils;
import java.util.List;
/**
* @author 作者:wulanfei
* @version 创建时间:2020年8月13日 下午15:11:25
* @explain 类说明:TreeVo实体类
*/
public class TreeVo {
private String code;
private String name;
private String parentCode;//父级id
private String level;//等级
private String orderNum;//排序
private List<TreeVo> resourceTree;
public List<TreeVo> getResourceTree() {
return resourceTree;
}
public void setResourceTree(List<TreeVo> resourceTree) {
this.resourceTree = resourceTree;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
@Override
public String toString() {
return "TreeVo [code=" + code + ", name=" + name + ", parentCode=" + parentCode + ", level=" + level
+ ", orderNum=" + orderNum + ", resourceTree=" + resourceTree + "]";
}
}
2.树形结构的utils类:Test
直接运行main方法,后台就能生成树形结构数据。
getListData() 方法可以自己后台生成这种类型的数据,或者数据库增加数据,编写查询方法就可以展示。
package com.xinqiao.utils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
/**
* @author 作者:wulanfei
* @version 创建时间:2020年8月13日 下午15:25:48
* @explain 类说明:Test
*/
public class Test {
public static void main(String[] args) {
// 1.获得资源list数据
List<TreeVo> list = getListData();
// 2.如果sql未排序 则java函数排序
list.sort(Comparator.comparing(TreeVo::getLevel).thenComparing(TreeVo::getOrderNum));
// 3.构建资源树
List<TreeVo> tree = buildResourceTree(list, null);
System.out.println(tree);
}
//接口调用方法
public List<TreeVo> getTreeValue() {
// 1.获得资源list数据
// 1.获得资源list数据
List<TreeVo> list = getListData();
// 2.如果sql未排序 则java函数排序
list.sort(Comparator.comparing(TreeVo::getLevel).thenComparing(TreeVo::getOrderNum));
// 3.构建资源树
List<TreeVo> tree = buildResourceTree(list, null);
System.out.println(tree);
return tree;
}
/**
* 构建资源树
*/
private static List<TreeVo> buildResourceTree(List<TreeVo> list, String parentCode) {
List<TreeVo> resourceTree = new ArrayList<>();
list.forEach(resource -> {
if (Objects.equals(parentCode, resource.getParentCode())) {
resource.setResourceTree(buildResourceTree(list, resource.getCode()));
resourceTree.add(resource);
}
});
return resourceTree;
}
/**
* 模拟资源数据
*/
private static List<TreeVo> getListData() {
List<TreeVo> list = new ArrayList<>();
TreeVo r1 = new TreeVo();
r1.setCode("0001");
r1.setName("湖南省");
r1.setLevel("01");
r1.setOrderNum("2");
list.add(r1);
TreeVo r3 = new TreeVo();
r3.setCode("0011");
r3.setName("长沙市");
r3.setParentCode("0001");
r3.setLevel("02");
r3.setOrderNum("1");
list.add(r3);
TreeVo r2 = new TreeVo();
r2.setCode("0002");
r2.setName("湖北省");
r2.setLevel("01");
r2.setOrderNum("1");
list.add(r2);
TreeVo r5 = new TreeVo();
r5.setCode("0111");
r5.setName("天心区");
r5.setParentCode("0011");
r5.setLevel("03");
r5.setOrderNum("1");
list.add(r5);
TreeVo r4 = new TreeVo();
r4.setCode("0012");
r4.setName("娄底市");
r4.setParentCode("0001");
r4.setLevel("02");
r4.setOrderNum("2");
list.add(r4);
return list;
}
}
3.Controller接口调用
火狐浏览器直接调用getTreeValue()就可以查看树形结构数据了。
Test test= new Test();
@RequestMapping(value = "/getTreeValue")
@ResponseBody
public List<TreeVo> getTreeValue() {
return test.getTreeValue();
}
如果文章有用,麻烦点个赞,给个评论,谢谢。