這篇文章主要講解了“怎么用springboot+mybatis plus實(shí)現(xiàn)樹形結(jié)構(gòu)查詢”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用springboot+mybatis plus實(shí)現(xiàn)樹形結(jié)構(gòu)查詢”吧!
創(chuàng)新互聯(lián)是專業(yè)的文縣網(wǎng)站建設(shè)公司,文縣接單;提供成都網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行文縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
實(shí)際開發(fā)過程中經(jīng)常需要查詢節(jié)點(diǎn)樹,根據(jù)指定節(jié)點(diǎn)獲取子節(jié)點(diǎn)列表,以下記錄了獲取節(jié)點(diǎn)樹的操作,以備不時(shí)之需。
可以用于系統(tǒng)部門組織機(jī)構(gòu)、商品分類、城市關(guān)系等帶有層級(jí)關(guān)系的數(shù)據(jù)結(jié)構(gòu);
即根節(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 |
表結(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="樹形結(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> { /** * 獲取樹形結(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 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 } ]
使用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>
感謝各位的閱讀,以上就是“怎么用springboot+mybatis plus實(shí)現(xiàn)樹形結(jié)構(gòu)查詢”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用springboot+mybatis plus實(shí)現(xiàn)樹形結(jié)構(gòu)查詢這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
本文題目:怎么用springboot+mybatisplus實(shí)現(xiàn)樹形結(jié)構(gòu)查詢
網(wǎng)站路徑:http://muchs.cn/article18/jpdsdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、軟件開發(fā)、定制開發(fā)、外貿(mào)建站、關(guān)鍵詞優(yōu)化、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)