


How to use SpringBoot+MyBatisPlus+MySQL8 to implement tree structure query
Scenario:
When implementing the permission function module, the queried permission data needs to be returned to the front end in a tree structure.
Function implementation:
Step one:Permission table structure definition and its function demonstration data.
DROP TABLE IF EXISTS `baoan_privilege`; CREATE TABLE `baoan_privilege` ( `id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主键', `privilege_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '权限名称', `privilege_code` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '权限编码', `pid` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '父Id', `url` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '菜单路由', `order_rank` int(3) NULL DEFAULT NULL COMMENT '序号', `privilege_type` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '权限类型1:项目,2菜单,3按钮', `privilege_description` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '权限描述', `state` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '2' COMMENT '状态(1:禁用,2:启用)', `created_by` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建人', `created_dt` datetime(0) NULL DEFAULT NULL COMMENT '创建时间', `version` int(9) NULL DEFAULT 1 COMMENT '版本号', `updated_by` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新人', `updated_dt` datetime(0) NULL DEFAULT NULL COMMENT '更新时间', `icon_name` int(15) NULL DEFAULT NULL COMMENT '图标名称', `delete_flag` int(1) NULL DEFAULT 1 COMMENT '删除标识(1:未删除,2:已删除)', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '权限表' ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of baoan_privilege -- ---------------------------- INSERT INTO `baoan_privilege` VALUES ('1', '首页', 'A', '0', NULL, NULL, '1', '首页', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('10', '通知管理', 'F_02', '6', NULL, NULL, '2', '通知管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('11', '操作日志', 'F_03', '6', NULL, NULL, '2', '操作日志', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('12', '角色管理', 'F_04', '6', NULL, NULL, '2', '角色管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('13', '存储管理', 'F_05', '6', NULL, NULL, '2', '存储管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('14', '权限管理', 'F_06', '6', NULL, NULL, '2', '权限管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('15', '新增', 'F_01_add', '9', NULL, NULL, '3', '管理员新增', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('16', '修改', 'F_01_update', '9', NULL, NULL, '3', '管理员修改', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('17', '查询', 'F_01_search', '9', NULL, NULL, '3', '管理员查询', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('18', '删除', 'F_01_delete', '9', NULL, NULL, '3', '管理员删除', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('19', '导出', 'F_01_export', '9', NULL, NULL, '3', '管理员导出', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('2', '用户管理', 'B', '0', NULL, NULL, '1', '用户管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('3', '商场管理', 'C', '0', NULL, NULL, '1', '商场管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('4', '商品管理', 'D', '0', NULL, NULL, '1', '商品管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('5', '推广管理', 'E', '0', NULL, NULL, '1', '推广管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('6', '系统管理', 'F', '0', NULL, NULL, '1', '系统管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('7', '配置管理', 'G', '0', NULL, NULL, '1', '配置管理', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('8', '统计报表', 'H', '0', NULL, NULL, '1', '统计报表', '2', NULL, NULL, 1, NULL, NULL, NULL, 1); INSERT INTO `baoan_privilege` VALUES ('9', '管理员', 'F_01', '6', NULL, NULL, '2', '管理员', '2', NULL, NULL, 1, NULL, NULL, NULL, 1);
Second step:Permission table entity definition and its extended objects
Basic objects
package com.zzg.entity; import java.io.Serializable; import java.util.Date; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @SuppressWarnings("serial") @TableName(value = "baoan_privilege") @Data public class BaoanPrivilege implements Serializable { private String id; private String privilegeName; private String privilegeCode; private String pid; private String url; private Integer orderRank; private String privilegeType; private String privilegeDescription; private String state; private String createdBy; private Date createdDt; private Integer version; private String updatedBy; private Date updatedDt; private Integer iconName; private Integer deleteFlag; }
Extended objects
package com.zzg.vo; import java.util.List; import com.zzg.entity.BaoanPrivilege; import lombok.Data; @SuppressWarnings("serial") @Data public class BaoanPrivilegeVo extends BaoanPrivilege { private List<baoanprivilege> children; }</baoanprivilege>
Step 3:Permission table Mapper definition
mapper interface definition
package com.zzg.mapper; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.zzg.entity.BaoanPrivilege; import com.zzg.vo.BaoanPrivilegeVo; public interface BaoanPrivilegeMapper extends BaseMapper<baoanprivilege> { List<baoanprivilegevo> selectList(Map<string> parameter); IPage<baoanprivilegevo> selectPage(Page page, @Param("vo")Map<string> parameter); }</string></baoanprivilegevo></string></baoanprivilegevo></baoanprivilege>
mapper.xml file definition
<?xml version="1.0" encoding="UTF-8"?> nbsp;mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper> <resultmap> <id></id> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> <result></result> </resultmap> <resultmap> <collection oftype="com.zzg.entity.BaoanPrivilege"></collection> </resultmap> <sql> id, privilege_name, privilege_code, pid, url, order_rank, privilege_type, privilege_description, state, created_by, created_dt, version, updated_by, updated_dt, icon_name, delete_flag </sql> <sql> <if> and baoan_privilege.privilege_type = #{privilegeType} </if> <if> and baoan_privilege.pid = #{pid} </if> </sql> <sql> <if> and baoan_privilege.privilege_type = #{vo.privilegeType} </if> <if> and baoan_privilege.pid = #{vo.pid} </if> </sql> <select> SELECT <include></include> FROM baoan_privilege WHERE pid = #{id} </select> <select> select <include></include> FROM baoan_privilege WHERE 1 = 1 <include></include> </select> <select> select <include></include> FROM baoan_privilege WHERE 1 = 1 <include></include> </select> </mapper>
Step 3: Permission table Service definition
package com.zzg.service; import java.util.List; import java.util.Map; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; import com.zzg.entity.BaoanPrivilege; import com.zzg.vo.BaoanPrivilegeVo; public interface BaoanPrivilegeService extends IService<baoanprivilege> { List<baoanprivilegevo> selectList(Map<string> parameter); IPage<baoanprivilegevo> selectPage(Page<baoanprivilegevo> page, Map<string> parameter); }</string></baoanprivilegevo></baoanprivilegevo></string></baoanprivilegevo></baoanprivilege>
package com.zzg.service.impl; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zzg.entity.BaoanPrivilege; import com.zzg.mapper.BaoanPrivilegeMapper; import com.zzg.service.BaoanPrivilegeService; import com.zzg.vo.BaoanPrivilegeVo; @Service public class BaoanPrivilegeServiceImpl extends ServiceImpl<baoanprivilegemapper> implements BaoanPrivilegeService { @Autowired private BaoanPrivilegeMapper mapper; @Override public List<baoanprivilegevo> selectList(Map<string> parameter) { // TODO Auto-generated method stub return mapper.selectList(parameter); } @Override public IPage<baoanprivilegevo> selectPage(Page<baoanprivilegevo> page, Map<string> parameter) { // TODO Auto-generated method stub return mapper.selectPage(page, parameter); } }</string></baoanprivilegevo></baoanprivilegevo></string></baoanprivilegevo></baoanprivilegemapper>
Step 4:The controller layer interface provides external services
// 查 @ApiOperation(httpMethod = "POST", value = "基于分页查询符合条件权限记录") @RequestMapping(value = "/getPage", method = { RequestMethod.POST }) @ApiImplicitParams({ @ApiImplicitParam(name = "username", value = "管理员名称", required = false, dataType = "String", paramType = "query") }) public Result getPage(@RequestBody Map<string> parame) { // 动态构建添加参数 // QueryWrapper<baoanprivilegevo> query = new QueryWrapper<baoanprivilegevo>(); // this.buildQuery(parame, query); PageParame pageParame = this.initPageBounds(parame); Page<baoanprivilegevo> page = new Page<baoanprivilegevo>(pageParame.getPage(), pageParame.getLimit()); IPage<baoanprivilegevo> list = baoanPrivilegeService.selectPage(page, parame); return Result.ok().setDatas(list); }</baoanprivilegevo></baoanprivilegevo></baoanprivilegevo></baoanprivilegevo></baoanprivilegevo></string>
Front-end effect display:
The above is the detailed content of How to use SpringBoot+MyBatisPlus+MySQL8 to implement tree structure query. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings
