怎么用springboot+mybatis plus实现树形结构查询
来源:亿速云
时间:2024-03-31 11:36:31 140浏览 收藏
目前golang学习网上已经有很多关于文章的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《怎么用springboot+mybatis plus实现树形结构查询》,也希望能帮助到大家,如果阅读完后真的对你学习文章有帮助,欢迎动动手指,评论留言并分享~
背景
实际开发过程中经常需要查询节点树,根据指定节点获取子节点列表,以下记录了获取节点树的操作,以备不时之需。
使用场景
可以用于系统部门组织机构、商品分类、城市关系等带有层级关系的数据结构;
设计思路
递归模型
即根节点、枝干节点、叶子节点,数据模型如下:
| id | code | name | parent_code |
|---|---|---|---|
| 1 | 10000 | 电脑 | 0 |
| 2 | 20000 | 手机 | 0 |
| 3 | 10001 | 联想笔记本 | 10000 |
| 4 | 10002 | 惠普笔记本 | 10000 |
| 5 | 1000101 | 联想拯救者 | 10001 |
| 6 | 1000102 | 联想小新系列 | 10001 |
实现代码
表结构
CREATE TABLE `tree_table` ( `id` int NOT NULL AUTO_INCREMENT COMMENT "主键ID", `code` varchar(10) NOT NULL COMMENT "编码", `name` varchar(20) NOT NULL COMMENT "名称", `parent_code` varchar(10) NOT NULL COMMENT "父级编码", PRIMARY KEY (`id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT="树形结构测试表";
表数据
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10000", "电脑", "0");
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10001", "联想笔记本", "10000");
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10002", "惠普笔记本", "10000");
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("1000101", "联想拯救者", "10001");
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("1000102", "联想小新系列", "10001");实体
@Data
@TableName("tree_table")
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class TreeTable {
/**
* 主键ID
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 编码
*/
private String code;
/**
* 名称
*/
private String name;
/**
* 父级编码
*/
private String parentCode;
/**
* 子节点
*/
@TableField(exist = false)
private List<TreeTable> childNode;
}mybatis
mapper
public interface TreeTableMapper extends BaseMapper<TreeTable> {
/**
* 获取树形结构数据
*
* @return 树形结构
*/
public List<TreeTable> noteTree();
}xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.springboot.example.mysqltree.mapper.TreeTableMapper"> <resultMap id="BaseResultMap" type="com.springboot.example.mysqltree.model.entity.TreeTable"> <result column="id" property="id"/> <result column="code" property="code"/> <result column="name" property="name"/> <result column="parent_code" property="parentCode"/> </resultMap> <resultMap id="NodeTreeResult" type="com.springboot.example.mysqltree.model.entity.TreeTable" extends="BaseResultMap"> <collection property="childNode" column="code" ofType="com.springboot.example.mysqltree.model.entity.TreeTable" javaType="java.util.ArrayList" select="nextNoteTree"> </collection> </resultMap> <sql id="Base_Column_List"> id, code, `name`, parent_code </sql> <select id="nextNoteTree" resultMap="NodeTreeResult"> select <include refid="Base_Column_List"/> from tree_table where parent_code=#[code] </select> <select id="noteTree" resultMap="NodeTreeResult"> select <include refid="Base_Column_List"/> from tree_table where parent_code="0" </select> </mapper>
noteTree :获取所有父级节点数据;
nextNoteTree:循环获取子节点数据,知道叶子节点结束;
column:关联表的列名;
ofType:返回类型
启动类
@Slf4j
@Component
public class TreeTableCommandLineRunner implements CommandLineRunner {
@Resource
private TreeTableMapper treeTableMapper;
@Override
public void run(String... args) throws Exception {
log.info(JSONUtil.toJsonPrettyStr(treeTableMapper.noteTree()));
}
}最终效果
[
{
"code": "10000",
"childNode": [
{
"code": "10001",
"childNode": [
{
"code": "1000101",
"childNode": [
],
"parentCode": "10001",
"name": "联想拯救者",
"id": 5
},
{
"code": "1000102",
"childNode": [
],
"parentCode": "10001",
"name": "联想小新系列",
"id": 6
}
],
"parentCode": "10000",
"name": "联想笔记本",
"id": 3
},
{
"code": "10002",
"childNode": [
],
"parentCode": "10000",
"name": "惠普笔记本",
"id": 4
}
],
"parentCode": "0",
"name": "电脑",
"id": 1
}
]注意事项
使用mybatis时如加载不到mapper xml需在pom.xml添加以下配置:
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
终于介绍完啦!小伙伴们,这篇关于《怎么用springboot+mybatis plus实现树形结构查询》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布文章相关知识,快来关注吧!
声明:本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
221 收藏
-
161 收藏
-
157 收藏
-
281 收藏
-
105 收藏
-
177 收藏
-
267 收藏
-
106 收藏
-
448 收藏
-
420 收藏
-
161 收藏
-
258 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习