springboot+mybatis plus實現(xiàn)樹形結(jié)構(gòu)查詢
實際開發(fā)過程中經(jīng)常需要查詢節(jié)點樹,根據(jù)指定節(jié)點獲取子節(jié)點列表,以下記錄了獲取節(jié)點樹的操作,以備不時之需。
使用場景可以用于系統(tǒng)部門組織機構(gòu)、商品分類、城市關(guān)系等帶有層級關(guān)系的數(shù)據(jù)結(jié)構(gòu);
設(shè)計思路遞歸模型即根節(jié)點、枝干節(jié)點、葉子節(jié)點,數(shù)據(jù)模型如下:
id code name parent_code 1 10000 電腦 0 2 20000 手機 0 3 10001 聯(lián)想筆記本 10000 4 10002 惠普筆記本 10000 5 1000101 聯(lián)想拯救者 10001 6 1000102 聯(lián)想小新系列 10001
實現(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 ’父級編碼’, PRIMARY KEY (`id`) USING BTREE) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=’樹形結(jié)構(gòu)測試表’;
表數(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’);
實體
@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; /** * 子節(jié)點 */ @TableField(exist = false) private List<TreeTable> childNode;}
mybatis
mapper
public interface TreeTableMapper extends BaseMapper<TreeTable> { /** * 獲取樹形結(jié)構(gòu)數(shù)據(jù) * * @return 樹形結(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 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 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 resultMap='NodeTreeResult'>select<include refid='Base_Column_List'/>from tree_tablewhere parent_code=#[code] </select> <select resultMap='NodeTreeResult'>select<include refid='Base_Column_List'/>from tree_tablewhere parent_code=’0’ </select></mapper> noteTree :獲取所有父級節(jié)點數(shù)據(jù); nextNoteTree:循環(huán)獲取子節(jié)點數(shù)據(jù),知道葉子節(jié)點結(jié)束; column:關(guān)聯(lián)表的列名; ofType:返回類型
啟動類
@Slf4j@Componentpublic 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 }]注意事項
使用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>總結(jié)
使用遞歸方式是比較常見的方式,優(yōu)點是實現(xiàn)簡單,直觀的體現(xiàn)層級關(guān)系,但是數(shù)據(jù)量大的情況下效率會略低;歡迎使用其他方式的小伙伴分享自己的實現(xiàn)思路。
到此這篇關(guān)于springboot+mybatis plus實現(xiàn)樹形結(jié)構(gòu)查詢的文章就介紹到這了,更多相關(guān)springboot 樹形結(jié)構(gòu)查詢內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python如何換行輸出2. Python使用urlretrieve實現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼3. Python:UserWarning:此模式具有匹配組。要實際獲得組,請使用str.extract4. Android Studio中一套代碼多渠道打包的實現(xiàn)方法5. 詳解java google Thumbnails 圖片處理6. python如何計算圓的面積7. Java使用Tesseract-Ocr識別數(shù)字8. Android打包篇:Android Studio將代碼打包成jar包教程9. Java 接口和抽象類的區(qū)別詳解10. 解決Android Studio 格式化 Format代碼快捷鍵問題
