Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use springboot+mybatis plus to realize Tree structure query

Shulou Source: shulou.com Published: 2022-05-31 13:39:33 10月25日 Update

This article mainly explains "how to use springboot+mybatis plus to realize tree structure query". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn "how to use springboot+mybatis plus to realize tree structure query" together!

background

In the actual development process, it is often necessary to query the node tree and obtain the child node list according to the specified node. The following records the operation of obtaining the node tree in case of emergency.

usage scenarios

It can be used for hierarchical data structures such as system department organization, commodity classification, city relationship, etc.

design ideas

recursive model

That is, root node, branch node and leaf node. The data model is as follows:

idcodenameparent_code110000 PC 0220000 Mobile 0310001 Lenovo Notebook 1000410002 HP Notebook 1000051000101 Lenovo Saver 1000161000102 Lenovo Xiaoxin Series 10001 Implementation Code

table structure

CREATE TABLE `tree_table`( `id` int NOT NULL AUTO_INCREMENT COMMENT "primary key ID", `code` varchar(10) NOT NULL COMMENT "code", `name` varchar(20) NOT NULL COMMENT "name", `parent_code` varchar(10) NOT NULL COMMENT "parent code", PRIMARY KEY (`id`) USING BTREE) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT="Tree Structure Test Table";

table data

INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10000", "Computer", "0");INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10001", "Lenovo Notebook", "10000");INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10002", "HP Notebook", "10000");INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("1000101", "Lenovo Savior", "10001");INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("1000102", "Lenovo Small New Series", "10001");

entity

@Data@TableName("tree_table")@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)public class TreeTable { /** * Primary key ID */ @TableId(type = IdType.AUTO) private Integer id; /** * encoding */ private String code; /** * name */ private String name; /** * parent code */ private String parentCode; /** * child node */ @TableField(exist = false) private List childNode;}

mybatis

mapper

public interface TreeTableMapper extends BaseMapper { /** * Acquire tree structure data * * @return Tree structure */ public List noteTree();}

xml

id, code, `name`, parent_code select from tree_table where parent_code=#[code] select from tree_table where parent_code="0"

noteTree: Get all parent node data;

nextNoteTree: loop to get child node data, know leaf node end;

column: column name of the associated table;

ofType: Return Type

startup class

@Slf4j@Componentpublic class TreeTableCommandLineRunner implements CommandLineRunner { @Resource private TreeTableMapper treeTableMapper; @Override public void run(String... args) throws Exception { log.info(JSONUtil.toJsonPrettyStr(treeTableMapper.noteTree())); }}

final effect

[ { "code": "10000", "childNode": [ { "code": "10001", "childNode": [ { "code": "1000101", "childNode": [ ], "parentCode": "10001", "name": "associate saviors", "id": 5 }, { "code": "1000102", "childNode": [ ], "parentCode": "10001", "name": "Lenovo Xiaoxin Series", "id": 6 } ], "parentCode": "10000", "name": "Lenovo Notebook", "id": 3 }, { "code": "10002", "childNode": [ ], "parentCode": "10000", "name": "HP Notebook", "id": 4 } ], "parentCode": "0", "name": "computer", "id": 1 }] Notes

If you cannot load mapper xml when using mybatis, add the following configuration to pom.xml:

src/main/resources true src/main/java **/*.xml Thank you for reading, the above is "how to use springboot+mybatis plus tree structure query" content, after the study of this article, I believe we have a deeper understanding of how to use springboot+mybatis plus tree structure query this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Nodes structures trees data notes notebooks queries coding computers Hewlett-Packard learning content leaves names ideas models emergencies events codes goods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB Shulou Technology vpn MySQL