springboot+mybatis plus實現樹形結構查詢
背景
實際開發過程中經常需要查詢節點樹,根據指定節點獲取子節點列表,以下記錄瞭獲取節點樹的操作,以備不時之需。
使用場景
可以用於系統部門組織機構、商品分類、城市關系等帶有層級關系的數據結構;
設計思路
遞歸模型
即根節點、枝幹節點、葉子節點,數據模型如下:
id | code | name | parent_code |
---|---|---|---|
1 | 10000 | 電腦 | 0 |
2 | 20000 | 手機 | 0 |
3 | 10001 | 聯想筆記本 | 10000 |
4 | 10002 | 惠普筆記本 | 10000 |
5 | 1000101 | 聯想拯救者 | 10001 |
6 | 1000102 | 聯想小新系列 | 10001 |
實現代碼
表結構
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 '父級編碼', PRIMARY KEY (`id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='樹形結構測試表';
表數據
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('10000', '電腦', '0'); INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('10001', '聯想筆記本', '10000'); INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('10002', '惠普筆記本', '10000'); INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('1000101', '聯想拯救者', '10001'); INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ('1000102', '聯想小新系列', '10001');
實體
@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; /** * 父級編碼 */ private String parentCode; /** * 子節點 */ @TableField(exist = false) private List<TreeTable> childNode; }
mybatis
mapper
public interface TreeTableMapper extends BaseMapper<TreeTable> { /** * 獲取樹形結構數據 * * @return 樹形結構 */ 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 :獲取所有父級節點數據;
- nextNoteTree:循環獲取子節點數據,知道葉子節點結束;
- column:關聯表的列名;
- ofType:返回類型
啟動類
@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": "聯想拯救者", "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 樹形結構查詢內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- mybatis映射和實際類型不一致的問題
- Mybatis之@ResultMap,@Results,@Result註解的使用
- Java面試題沖刺第九天–MyBatis2
- springboot使用mybatis一對多的關聯查詢問題記錄
- MyBatis映射關系詳解