您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“MyBatis如何查詢樹形數(shù)據(jù)”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“MyBatis如何查詢樹形數(shù)據(jù)”這篇文章吧。
(1)假設(shè)我們有如下一張菜單表 menu,其中子菜單通過 parendId 與父菜單的 id 進(jìn)行關(guān)聯(lián):
(2)對應(yīng)的實體類如下:
@Setter @Getter public class Menu { private Integer id; private String name; private List<Menu> children; }
(1)假設(shè)目前菜單只有兩級,MyBatis 語句如下。其原理是通過關(guān)聯(lián)查詢,一次性將數(shù)據(jù)查詢出來,然后根據(jù) resultMap 的配置進(jìn)行轉(zhuǎn)換,構(gòu)建目標(biāo)實體類。
優(yōu)點:只由于該方法需要訪問一次數(shù)據(jù)庫就可以了,不會造成嚴(yán)重的數(shù)據(jù)庫訪問消耗。
<?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>
最終獲取到的結(jié)果如下:
(2)如果菜單有三級的話,則 MyBatis 語句做如下修改,再增加一個嵌套結(jié)果級即可:
<?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>
(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>
(1)下面代碼使用遞歸查詢出所有菜單(無論層級有多深):
遞歸查詢好處在于簡單易懂,通過簡單的配置就可以達(dá)到目標(biāo)效果。不足之處在于由于需要多次查詢數(shù)據(jù)庫,如果結(jié)果集記錄條數(shù)過大,會造成較大的數(shù)據(jù)庫訪問消耗。
<?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> <!--級聯(lián)查詢父菜單--> <select id="getAllMenus" resultMap="BaseResultMap" > select * from menu where parentId = 0 </select> <!--級聯(lián)查詢子菜單--> <select id="findMenuByParentId" resultMap="BaseResultMap" > select * from menu where parentId = #{id} </select> </mapper>
(2)關(guān)聯(lián)查詢還可以傳遞多個參數(shù),此時傳遞部分 column 的值為多個鍵值對(由于這里傳遞的 name 其實沒有用到,只是做個演示,下面的查詢結(jié)果同前面的是一樣的):
<?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> <!--級聯(lián)查詢父菜單--> <select id="getAllMenus" resultMap="BaseResultMap" > select * from menu where parentId = 0 </select> <!--級聯(lián)查詢子菜單--> <select id="findMenuByParentId" resultMap="BaseResultMap" > select * from menu where parentId = #{id} </select> </mapper>
以上是“MyBatis如何查詢樹形數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。