怎么用springboot+mybatis plus实现树形结构查询

怎么用springboot+mybatis plus实现树形结构查询

这篇文章主要讲解了“怎么用springboot+mybatis plus实现树形结构查询”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用springboot+mybatis plus实现树形结构查询”吧!

背景

实际开发过程中经常需要查询节点树,根据指定节点获取子节点列表,以下记录了获取节点树的操作,以备不时之需。

怎么用springboot+mybatis plus实现树形结构查询

使用场景

可以用于系统部门组织机构、商品分类、城市关系等带有层级关系的数据结构;

设计思路

递归模型

即根节点、枝干节点、叶子节点,数据模型如下:

idcodenameparent_code
110000电脑0
220000手机0
310001联想笔记本10000
410002惠普笔记本10000
51000101联想拯救者10001
61000102联想小新系列10001

实现代码

表结构

CREATETABLE`tree_table`(`id`intNOTNULLAUTO_INCREMENTCOMMENT"主键ID",`code`varchar(10)NOTNULLCOMMENT"编码",`name`varchar(20)NOTNULLCOMMENT"名称",`parent_code`varchar(10)NOTNULLCOMMENT"父级编码",PRIMARYKEY(`id`)USINGBTREE)ENGINE=MyISAMAUTO_INCREMENT=1DEFAULTCHARSET=utf8ROW_FORMAT=DYNAMICCOMMENT="树形结构测试表";

表数据

INSERTINTO`tree_table`(`code`,`name`,`parent_code`)VALUES("10000","电脑","0");INSERTINTO`tree_table`(`code`,`name`,`parent_code`)VALUES("10001","联想笔记本","10000");INSERTINTO`tree_table`(`code`,`name`,`parent_code`)VALUES("10002","惠普笔记本","10000");INSERTINTO`tree_table`(`code`,`name`,`parent_code`)VALUES("1000101","联想拯救者","10001");INSERTINTO`tree_table`(`code`,`name`,`parent_code`)VALUES("1000102","联想小新系列","10001");

实体

@Data@TableName("tree_table")@EqualsAndHashCode(callSuper=false)@Accessors(chain=true)publicclassTreeTable{/***主键ID*/@TableId(type=IdType.AUTO)privateIntegerid;/***编码*/privateStringcode;/***名称*/privateStringname;/***父级编码*/privateStringparentCode;/***子节点*/@TableField(exist=false)privateList<TreeTable>childNode;}

mybatis

mapper

publicinterfaceTreeTableMapperextendsBaseMapper<TreeTable>{/***获取树形结构数据**@return树形结构*/publicList<TreeTable>noteTree();}

xml

<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.springboot.example.mysqltree.mapper.TreeTableMapper"><resultMapid="BaseResultMap"type="com.springboot.example.mysqltree.model.entity.TreeTable"><resultcolumn="id"property="id"/><resultcolumn="code"property="code"/><resultcolumn="name"property="name"/><resultcolumn="parent_code"property="parentCode"/></resultMap><resultMapid="NodeTreeResult"type="com.springboot.example.mysqltree.model.entity.TreeTable"extends="BaseResultMap"><collectionproperty="childNode"column="code"ofType="com.springboot.example.mysqltree.model.entity.TreeTable"javaType="java.util.ArrayList"select="nextNoteTree"></collection></resultMap><sqlid="Base_Column_List">id,code,`name`,parent_code</sql><selectid="nextNoteTree"resultMap="NodeTreeResult">select<includerefid="Base_Column_List"/>fromtree_tablewhereparent_code=#[code]</select><selectid="noteTree"resultMap="NodeTreeResult">select<includerefid="Base_Column_List"/>fromtree_tablewhereparent_code="0"</select></mapper>

  • noteTree :获取所有父级节点数据;

  • nextNoteTree:循环获取子节点数据,知道叶子节点结束;

  • column:关联表的列名;

  • ofType:返回类型

启动类

@Slf4j@ComponentpublicclassTreeTableCommandLineRunnerimplementsCommandLineRunner{@ResourceprivateTreeTableMappertreeTableMapper;@Overridepublicvoidrun(String...args)throwsException{log.info(JSONUtil.toJsonPrettyStr(treeTableMapper.noteTree()));}}

最终效果

[{"code":"10000","childNode":[{"code":"10001","childNode":[{"code":"1000101","childNode":[],"parentCode":"10001","name":"联想拯救者","id":5},{"code":"1000102","childNode":[],"parentCode":"10001","name":"联想小新系列","id":6}],"parentCode":"10000","name":"联想笔记本","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>

感谢各位的阅读,以上就是“怎么用springboot+mybatis plus实现树形结构查询”的内容了,经过本文的学习后,相信大家对怎么用springboot+mybatis plus实现树形结构查询这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是恰卡编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

发布于 2022-04-11 21:14:27
收藏
分享
海报
0 条评论
26
上一篇:Springboot如何读取自定义pro文件注入static静态变量 下一篇:SpringBoot随机端口启动怎么实现
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码