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 依賴:
<modelVersion>4.0.0</modelVersion>
<groupId>springboot</groupId>
<artifactId>mybatis-collection-tree</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyBatis :: collection 集合嵌套查詢樹形節點</name>
<!-- Spring Boot 啓動父依賴 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
</properties>
<dependencies>
<!-- Spring Boot Web 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Mybatis 依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 連接驅動依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
在 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 代碼如下:
<mapper namespace="org.mybatis.dao.NodeDao">
<resultMap id="BaseTreeResultMap" type="org.mybatis.domain.Node">
<result column="id" property="id"/>
<result column="name" property="name"/>
<collection column="id" property="next" javaType="java.util.ArrayList"
ofType="org.mybatis.domain.Node" select="getNextNodeTree"/>
</resultMap>
<resultMap id="NextTreeResultMap" type="org.mybatis.domain.Node">
<result column="id" property="id"/>
<result column="name" property="name"/>
<collection column="id" property="next" javaType="java.util.ArrayList"
ofType="org.mybatis.domain.Node" select="getNextNodeTree"/>
</resultMap>
<sql id="Base_Column_List">
id, name
</sql>
<select id="getNextNodeTree" resultMap="NextTreeResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM node
WHERE parent_id = #{id}
</select>
<select id="getNodeTree" resultMap="BaseTreeResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM node
WHERE parent_id = 0
</select>
</mapper>
在 dao 層,我們只調用 getNodeTree
方法,parent_id = 0
代表頂級節點。然後通過 collection 節點繼續調用 getNextNodeTree
方法進行循環調用。
<collection column="id" property="next" javaType="java.util.ArrayList"
ofType="org.mybatis.domain.Node" select="getNextNodeTree"/>
以下是關鍵的知識點:
- column 代表會拿父節點 id ,作爲參數獲取 next 對象
- javaType 代表 next 對象是個列表,其實可以省略不寫
- ofType 用來區分 JavaBean 屬性類型和集合包含的類型
- select 是用來執行循環哪個 SQL
工程代碼地址:https://github.com/JeffLi1993/myabtis-learning-example
工程演示後的結果如圖所示:
image.png
思考小結
這樣的實現原理,嵌套 SQL 執行,這裏就存在一個性能上的問題。比如 10 萬條數據,需要執行 10 萬次 SELECT 語句。所以不推薦數據量級大的樹形結構。
如果結構不經常改變,數量級還行,可以考慮加緩存。這樣,讀取的數據庫的次數大大減少,比如省市區。
還有一種常用的樹形節點實現是,讀取幾次,內存處理。這樣的好處就是減少對數據庫查詢次數,內存處理速度很快,性能大大提升。