您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“MyBatis如何查詢樹形數據”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“MyBatis如何查詢樹形數據”這篇文章吧。
(1)假設我們有如下一張菜單表 menu,其中子菜單通過 parendId 與父菜單的 id 進行關聯:
(2)對應的實體類如下:
@Setter @Getter public class Menu { private Integer id; private String name; private List<Menu> children; }
(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>
最終獲取到的結果如下:
(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>
(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)下面代碼使用遞歸查詢出所有菜單(無論層級有多深):
遞歸查詢好處在于簡單易懂,通過簡單的配置就可以達到目標效果。不足之處在于由于需要多次查詢數據庫,如果結果集記錄條數過大,會造成較大的數據庫訪問消耗。
<?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>
(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如何查詢樹形數據”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。