In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.