SpringBoot树获取方法总结
2022/2/14 23:44:31
本文主要是介绍SpringBoot树获取方法总结,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
最近项目中有需要获取全国行政区划省-市-区县-乡镇、街道办的树状结构数据,现将自己获取树的方法总结如下,有不到之处,敬请批评指正!
一、全国行政区划数据的整理以及获取
- 获取地址:https://pan.baidu.com/s/19JxdWH1R4z_HrrodFJUfjw?pwd=7bey 提取码:7bey
- 创建表
二、java代码示例
- AreaTreeMapper.xml的编写
<select id="selectSymptomTreeNodeJson" resultType="com.boot.mybatis.entity.CategoryTreeNode">
SELECT dq_id as id,dq_name AS title,parent_id as parentId FROM ft_sys_dq
</select>
- CategoryTreeNode实体类的编写
-
/**
* @Project:
* @Description:
* @Auther: songwp
* @Date: 2022/2/14 19:41
**/
@Data
public class CategoryTreeNode {
private BigInteger id;
private String title;
private BigInteger parentId;
private List<CategoryTreeNode> children = new ArrayList<>();
}
-
- AreaTreeMapper接口编写
/**
* @Project:
* @Description:
* @Auther: songwp
* @Date: 2022/2/14 19:44
**/
@Mapper
public interface AreaTreeMapper {
List<CategoryTreeNode> selectSymptomTreeNodeJson();
}
- AreaTreeService业务接口编写
/**
* @Project:
* @Description:
* @Auther: songwp
* @Date: 2022/2/14 19:52
**/
public interface AreaTreeService {
List<CategoryTreeNode> getSymptomTreeJson();
}
- AreaTreeServiceImpl的业务实现类编写
-
/**
* @Project:
* @Description:
* @Auther: songwp
* @Date: 2022/2/14 19:53
**/
@Service
public class AreaTreeServiceImpl implements AreaTreeService {
@Resource
AreaTreeMapper areaTreeMapper;
@Override
public List<CategoryTreeNode> getSymptomTreeJson() {
//从数据库获取数据的list(一次性获取的所有数据)
List<CategoryTreeNode> list= areaTreeMapper.selectSymptomTreeNodeJson();
List<CategoryTreeNode> resultList = new ArrayList<>();
TreeUtil treeUtilNew = new TreeUtil();
resultList = treeUtilNew.build(list); //进行封装
return resultList; //返回树形结构json
}
}
-
- AreaTreeController的编写
-
/**
* @Project:
* @Description:
* @Auther: songwp
* @Date: 2022/2/14 19:57
**/
@RestController
public class AreaTreeController {
@Autowired
AreaTreeService areaTreeService;
@GetMapping("/getTreeList")
public List<CategoryTreeNode> getTreeList(){
return areaTreeService.getSymptomTreeJson();
}
}
-
- TreeUtil的编写
/**
* @Project:
* @Description: 构架树结构树形json--好用
* @Auther: songwp
* @Date: 2022/2/14 19:39
**/
public class TreeUtil {
/**
* 根据pid,构建树节点
*/
public static <T extends CategoryTreeNode> List<T> build(List<T> treeNodes, Integer pid) {
List<T> treeList = new ArrayList<>();
for(T treeNode : treeNodes) {
if (pid.equals(treeNode.getParentId())) {
treeList.add(findChildren(treeNodes, treeNode));
}
}
return treeList;
}
/**
* 查找子节点
*/
private static <T extends CategoryTreeNode> T findChildren(List<T> treeNodes, T rootNode) {
for(T treeNode : treeNodes) {
if(rootNode.getId().equals(treeNode.getParentId())) {
rootNode.getChildren().add(findChildren(treeNodes, treeNode));
}
}
return rootNode;
}
/**
* 构建树节点
*/
public static <T extends CategoryTreeNode> List<T> build(List<T> treeNodes) {
List<T> result = new ArrayList<>();
//list转map
Map<BigInteger, T> nodeMap = new LinkedHashMap<>(treeNodes.size());
for(T treeNode : treeNodes){
nodeMap.put(treeNode.getId(), treeNode);
}
for(T node : nodeMap.values()) {
T parent = nodeMap.get(node.getParentId());
if(parent != null && !(node.getId().equals(parent.getId()))){
parent.getChildren().add(node);
continue;
}
result.add(node);
}
return result;
}
}
三、调用接口获取数据如下:
这篇关于SpringBoot树获取方法总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南