MyBatis實現兩種查詢樹形數據的方法詳解(嵌套結果集和遞歸查詢)

    樹形結構數據在開發中十分常見,比如:菜單數、組織樹, 利用 MyBatis 提供嵌套查詢功能可以很方便地實現這個功能需求。而其具體地實現方法又有兩種,下面分別通過樣例進行演示。

方法一:使用嵌套結果集實現

1,準備工作

(1)假設我們有如下一張菜單表 menu,其中子菜單通過 parendId 與父菜單的 id 進行關聯:  

原文:MyBatis - 兩種查詢樹形數據的方法詳解(嵌套結果集、遞歸查詢)

(2)對應的實體類如下:

@Setter
@Getter
public class Menu {
    private Integer id;
    private String name;
    private List<Menu> children;
}

2,實現代碼

(1)假設目前菜單隻有兩級,MyBatis 語句如下。其原理是通過關聯查詢,一次性將數據查詢出來,然後根據 resultMap 的配置進行轉換,構建目標實體類。

優點:隻由於該方法需要訪問一次數據庫就可以瞭,不會造成嚴重的數據庫訪問消耗。

<?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.example.demo.mapper.MenuMapper">
    <resultMap type="com.example.demo.bean.Menu" id="BaseResultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="children" ofType="com.example.demo.bean.Menu">
            <id column="id2" property="id"/>
            <result column="name2" property="name"/>
        </collection>
    </resultMap>
    <select id="getAllMenus" resultMap="BaseResultMap">
        select
            m1.id as id,
            m1.name as name,
            m2.id as id2,
            m2.name as name2
        from menu m1,menu m2
        where m1.`id`=m2.`parentId`
    </select>
</mapper>

最終獲取到的結果如下:

原文:MyBatis - 兩種查詢樹形數據的方法詳解(嵌套結果集、遞歸查詢)

(2)如果菜單有三級的話,則 MyBatis 語句做如下修改,再增加一個嵌套結果級即可:

<?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.example.demo.mapper.MenuMapper">
    <resultMap type="com.example.demo.bean.Menu" id="BaseResultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="children" ofType="com.example.demo.bean.Menu">
            <id column="id2" property="id"/>
            <result column="name2" property="name"/>
            <collection property="children" ofType="com.example.demo.bean.Menu">
                <id column="id3" property="id"/>
                <result column="name3" property="name"/>
            </collection>
        </collection>
    </resultMap>
    <select id="getAllMenus" resultMap="BaseResultMap">
        select
            m1.id as id,
            m1.name as name,
            m2.id as id2,
            m2.name as name2,
            m3.id as id3,
            m3.name as name3
        from menu m1,menu m2,menu m3
        where m1.`id`=m2.`parentId` and m2.`id`=m3.`parentId`
    </select>
</mapper>

原文:MyBatis - 兩種查詢樹形數據的方法詳解(嵌套結果集、遞歸查詢)

(3)如果菜單級別不確定,可能隻有一級、或者有兩級、或者有三級(最多三級),可以對 SQL 語句稍作修改,改成左連接即可:

<?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.example.demo.mapper.MenuMapper">
    <resultMap type="com.example.demo.bean.Menu" id="BaseResultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="children" ofType="com.example.demo.bean.Menu">
            <id column="id2" property="id"/>
            <result column="name2" property="name"/>
            <collection property="children" ofType="com.example.demo.bean.Menu">
                <id column="id3" property="id"/>
                <result column="name3" property="name"/>
            </collection>
        </collection>
    </resultMap>
    <select id="getAllMenus" resultMap="BaseResultMap">
        select
            m1.id as id,
            m1.name as name,
            m2.id as id2,
            m2.name as name2,
            m3.id as id3,
            m3.name as name3
        from menu m1
            left join menu m2 on m1.id=m2.parentId
            left join menu m3 on m2.id=m3.parentId
        where m1.parentId=0
    </select>
</mapper>

原文:MyBatis - 兩種查詢樹形數據的方法詳解(嵌套結果集、遞歸查詢)

方法二:使用遞歸查詢實現

(1)下面代碼使用遞歸查詢出所有菜單(無論層級有多深):

    遞歸查詢好處在於簡單易懂,通過簡單的配置就可以達到目標效果。不足之處在於由於需要多次查詢數據庫,如果結果集記錄條數過大,會造成較大的數據庫訪問消耗。

<?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.example.demo.mapper.MenuMapper">
    <resultMap type="com.example.demo.bean.Menu" id="BaseResultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="children" select="findMenuByParentId" column="id"/>
    </resultMap>
 
    <!--級聯查詢父菜單-->
    <select id="getAllMenus" resultMap="BaseResultMap" >
         select * from menu where parentId = 0
    </select>
 
    <!--級聯查詢子菜單-->
    <select id="findMenuByParentId" resultMap="BaseResultMap" >
         select * from menu where parentId = #{id}
    </select>
</mapper>

原文:MyBatis - 兩種查詢樹形數據的方法詳解(嵌套結果集、遞歸查詢)

(2)關聯查詢還可以傳遞多個參數,此時傳遞部分 column 的值為多個鍵值對(由於這裡傳遞的 name 其實沒有用到,隻是做個演示,下面的查詢結果同前面的是一樣的):

<?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.example.demo.mapper.MenuMapper">
    <resultMap type="com.example.demo.bean.Menu" id="BaseResultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="children" select="findMenuByParentId" column="{id=id,name=name}"/>
    </resultMap>
 
    <!--級聯查詢父菜單-->
    <select id="getAllMenus" resultMap="BaseResultMap" >
         select * from menu where parentId = 0
    </select>
 
    <!--級聯查詢子菜單-->
    <select id="findMenuByParentId" resultMap="BaseResultMap" >
         select * from menu where parentId = #{id}
    </select>
</mapper>

到此這篇關於MyBatis實現兩種查詢樹形數據的方法詳解(嵌套結果集和遞歸查詢)的文章就介紹到這瞭,更多相關MyBatis 查詢樹形數據內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: