springboot+mybatis plus實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)查詢
背景
實(shí)際開(kāi)發(fā)過(guò)程中經(jīng)常需要查詢節(jié)點(diǎn)樹(shù),根據(jù)指定節(jié)點(diǎn)獲取子節(jié)點(diǎn)列表,以下記錄了獲取節(jié)點(diǎn)樹(shù)的操作,以備不時(shí)之需。
使用場(chǎng)景
可以用于系統(tǒng)部門(mén)組織機(jī)構(gòu)、商品分類、城市關(guān)系等帶有層級(jí)關(guān)系的數(shù)據(jù)結(jié)構(gòu);
設(shè)計(jì)思路
遞歸模型
即根節(jié)點(diǎn)、枝干節(jié)點(diǎn)、葉子節(jié)點(diǎn),數(shù)據(jù)模型如下:
| id | code | name | parent_code |
|---|---|---|---|
| 1 | 10000 | 電腦 | 0 |
| 2 | 20000 | 手機(jī) | 0 |
| 3 | 10001 | 聯(lián)想筆記本 | 10000 |
| 4 | 10002 | 惠普筆記本 | 10000 |
| 5 | 1000101 | 聯(lián)想拯救者 | 10001 |
| 6 | 1000102 | 聯(lián)想小新系列 | 10001 |
實(shí)現(xiàn)代碼
表結(jié)構(gòu)
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 '父級(jí)編碼', PRIMARY KEY (`id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='樹(shù)形結(jié)構(gòu)測(cè)試表';
表數(shù)據(jù)
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('10000', '電腦', '0');
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('10001', '聯(lián)想筆記本', '10000');
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('10002', '惠普筆記本', '10000');
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('1000101', '聯(lián)想拯救者', '10001');
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('1000102', '聯(lián)想小新系列', '10001');
實(shí)體
@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;
/**
* 父級(jí)編碼
*/
private String parentCode;
/**
* 子節(jié)點(diǎn)
*/
@TableField(exist = false)
private List<TreeTable> childNode;
}
mybatis
mapper
public interface TreeTableMapper extends BaseMapper<TreeTable> {
/**
* 獲取樹(shù)形結(jié)構(gòu)數(shù)據(jù)
*
* @return 樹(shù)形結(jié)構(gòu)
*/
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 :獲取所有父級(jí)節(jié)點(diǎn)數(shù)據(jù);
- nextNoteTree:循環(huán)獲取子節(jié)點(diǎn)數(shù)據(jù),知道葉子節(jié)點(diǎn)結(jié)束;
- column:關(guān)聯(lián)表的列名;
- ofType:返回類型
啟動(dòng)類
@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": "聯(lián)想拯救者",
"id": 5
},
{
"code": "1000102",
"childNode": [
],
"parentCode": "10001",
"name": "聯(lián)想小新系列",
"id": 6
}
],
"parentCode": "10000",
"name": "聯(lián)想筆記本",
"id": 3
},
{
"code": "10002",
"childNode": [
],
"parentCode": "10000",
"name": "惠普筆記本",
"id": 4
}
],
"parentCode": "0",
"name": "電腦",
"id": 1
}
]
注意事項(xiàng)
使用mybatis時(shí)如加載不到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>
總結(jié)
使用遞歸方式是比較常見(jiàn)的方式,優(yōu)點(diǎn)是實(shí)現(xiàn)簡(jiǎn)單,直觀的體現(xiàn)層級(jí)關(guān)系,但是數(shù)據(jù)量大的情況下效率會(huì)略低;歡迎使用其他方式的小伙伴分享自己的實(shí)現(xiàn)思路。
到此這篇關(guān)于springboot+mybatis plus實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)查詢的文章就介紹到這了,更多相關(guān)springboot 樹(shù)形結(jié)構(gòu)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合mybatis-plus基于注解實(shí)現(xiàn)一對(duì)一(一對(duì)多)查詢功能
- oracle+mybatis-plus+springboot實(shí)現(xiàn)分頁(yè)查詢的實(shí)例
- SpringBoot+MyBatisPlus+MySQL8實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)查詢
- springboot整合mybatis-plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼
- springboot + mybatis-plus實(shí)現(xiàn)多表聯(lián)合查詢功能(注解方式)
- springboot整合mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢功能
- spring?boot?使用Mybatis-plus查詢方法解析
相關(guān)文章
SpringSecurity頁(yè)面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn)(內(nèi)存取值與數(shù)據(jù)庫(kù)取值)
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問(wèn)控制解決方案的安全框架,本文主要介紹了SpringSecurity頁(yè)面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫(kù)的步驟
這篇文章主要介紹了springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫(kù)的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-01-01
SpringBoot攔截器實(shí)現(xiàn)對(duì)404和500等錯(cuò)誤的攔截
本篇文章主要介紹了SpringBoot攔截器實(shí)現(xiàn)對(duì)404和500等錯(cuò)誤的攔截,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
解決Map集合使用get方法返回null拋出空指針異常問(wèn)題
這篇文章主要介紹了解決Map集合使用get方法返回null拋出空指針異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
重新認(rèn)識(shí)Java中的ThreadLocal
ThreadLocal是JDK包提供的,它提供線程本地變量,如果創(chuàng)建一個(gè)ThreadLocal變量,那么訪問(wèn)這個(gè)變量的每個(gè)線程都會(huì)有這個(gè)變量的一個(gè)副本,在實(shí)際多線程操作的時(shí)候,操作的是自己本地內(nèi)存中的變量,從而規(guī)避了線程安全問(wèn)題2021-05-05

