mybatis mysql查询树形结构_MyBatis collection 集合嵌套查询树形节点
原標題:MyBatis collection 集合嵌套查詢樹形節點
MyBatis collection 集合
MyBatis 是數據持久層框架,支持定制化 SQL、存儲過程以及高級映射。尤其強大在于它的映射語句,比如高級映射中的 collection 集合。
collection 集合,集合常用的兩個場景是集合的嵌套查詢、集合的嵌套結果。集合的嵌套結果就是查詢結果對應嵌套子對象。這里就是利用 collection 集合嵌套查詢樹形節點。下面來一一實現。
查詢樹形節點 Web 案例 創建數據庫表
節點表:
CREATE TABLE `node` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`parent_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COMMENT='節點表'
簡單的節點父子關系設計,下面插入幾條數據:
INSERT INTO node (name, parent_id) VALUES ('一級節點A', 0);
INSERT INTO node (name, parent_id) VALUES ('一級節點B', 0);
INSERT INTO node (name, parent_id) VALUES ('一級節點C', 0);
INSERT INTO node (name, parent_id) VALUES ('二級節點AA', 1);
INSERT INTO node (name, parent_id) VALUES ('二級節點aa', 1);
INSERT INTO node (name, parent_id) VALUES ('二級節點BB', 2);
INSERT INTO node (name, parent_id) VALUES ('三級級節點AAA', 4);
INSERT INTO node (name, parent_id) VALUES ('三級級節點aaa', 4);
INSERT INTO node (name, parent_id) VALUES ('三級級節點BBB', 6);mybatis-collection-tree 工程
pom.xml 添加 mybatis 依賴:
4.0.0
springboot
mybatis-collection-tree
0.0.1-SNAPSHOT
MyBatis :: collection 集合嵌套查詢樹形節點
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
1.2.0
5.1.39
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis-spring-boot}
mysql
mysql-connector-java
${mysql-connector}
junit
junit
4.12
在 application.properties 應用配置文件,增加 Mybatis 相關配置:
## 數據源配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
mybatis.typeAliasesPackage=org.mybatis.domain
mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.typeAliasesPackage 配置為 org.mybatis.domain,指向實體類包路徑。mybatis.mapperLocations 配置為 classpath 路徑下 mapper 包下,* 代表會掃描所有 xml 文件。
重要的還是看 collection 在 xml 的映射實現,NodeMapper.xml 代碼如下:
ofType="org.mybatis.domain.Node" select="getNextNodeTree"/>
ofType="org.mybatis.domain.Node" select="getNextNodeTree"/>
id, name
SELECT
FROM node
WHERE parent_id = #{id}
SELECT
FROM node
WHERE parent_id = 0
在 dao 層,我們只調用 getNodeTree方法, parent_id=0代表頂級節點。然后通過 collection 節點繼續調用 getNextNodeTree方法進行循環調用。
ofType="org.mybatis.domain.Node" select="getNextNodeTree"/>
以下是關鍵的知識點:
column 代表會拿父節點 id ,作為參數獲取 next 對象
javaType 代表 next 對象是個列表,其實可以省略不寫
ofType 用來區分 JavaBean 屬性類型和集合包含的類型
select 是用來執行循環哪個 SQL
工程代碼地址:https://github.com/JeffLi1993/myabtis-learning-example工程演示后的結果如圖所示:
思考小結
這樣的實現原理,嵌套 SQL 執行,這里就存在一個性能上的問題。比如 10 萬條數據,需要執行 10 萬次 SELECT 語句。所以不推薦數據量級大的樹形結構。
如果結構不經常改變,數量級還行,可以考慮加緩存。這樣,讀取的數據庫的次數大大減少,比如省市區。
還有一種常用的樹形節點實現是,讀取幾次,內存處理。這樣的好處就是減少對數據庫查詢次數,內存處理速度很快,性能大大提升。
參考文獻:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html
快掃一掃支持下
責任編輯:
總結
以上是生活随笔為你收集整理的mybatis mysql查询树形结构_MyBatis collection 集合嵌套查询树形节点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql数据库定点任务_MySQL数据
- 下一篇: mysql dataset_数据库 da