日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis通过colliection属性递归获取菜单树

發(fā)布時間:2025/3/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis通过colliection属性递归获取菜单树 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、現(xiàn)有商品分類數(shù)據(jù)表category結(jié)構(gòu)如下,三個字段都為varchar類型


2、創(chuàng)建商品分類對應(yīng)的數(shù)據(jù)Bean

/*** */ package com.xdw.dao;import java.util.List;import com.xdw.model.Category;/*** @author xiadewang*2018年4月16日*/ public interface CategoryDao {List<Category> getCategoryList(); }

3、創(chuàng)建CategoryDao.xml

<?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.xdw.dao.CategoryDao"><!-- 初始化菜單樹 --><!-- 這里的id的值作為下面的查詢返回結(jié)果resultMap的值 --><!-- collection中的column屬性可以為多個值,這里只有一個,它作為下面遞歸查詢傳遞進(jìn)去的參數(shù) --><!-- ofType和javaType屬性正好聯(lián)合構(gòu)成了數(shù)據(jù)Bean類Category中的childrenList屬性的數(shù)據(jù)類型 --><!-- select的值為下面遞歸查詢的select標(biāo)簽的id值 --><resultMap type="Category" id="categoryTree"><result column="cid" property="cid" javaType="java.lang.String" /><result column="cname" property="cname" javaType="java.lang.String" /><result column="pid" property="pid" javaType="java.lang.String" /><collection column="cid" property="childrenList" ofType="Category" javaType="java.util.ArrayList" select="selectCategoryChildrenByCid"/></resultMap><!-- 先查詢菜單根級目錄 --><!-- 這里的返回結(jié)果必須為resultMap,并且值為上面構(gòu)建的resultMap的id的值 --><select id="getCategoryList" resultMap="categoryTree">select * from category where pid = 'root'</select><!-- 再利用上次查詢結(jié)果colliection中column的值cid做遞歸查詢,查出所有子菜單 --><!-- 這里的返回結(jié)果必須為resultMap,并且值為上面構(gòu)建的resultMap的id的值 --><select id="selectCategoryChildrenByCid" resultMap="categoryTree" parameterType="String">select * from category where pid = #{cid}</select> </mapper>

4、service層

/*** */ package com.xdw.service;import java.util.List;import com.xdw.model.Category;/*** @author xiadewang*2018年4月16日*/ public interface CategoryService {List<Category> getCategoryList(); }/*** */ package com.xdw.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.xdw.dao.CategoryDao; import com.xdw.model.Category; import com.xdw.service.CategoryService;/*** @author xiadewang*2018年4月16日*/ @Service public class CategoryServiceImpl implements CategoryService {@Autowiredprivate CategoryDao categoryDao;/* (non-Javadoc)* @see com.xdw.service.CategoryService#getCategoryList()*/@Overridepublic List<Category> getCategoryList() {// TODO Auto-generated method stubreturn categoryDao.getCategoryList();}}

4、controller層

@RequestMapping("/getCategoryTree") @ResponseBodypublic List<Category> getCategoryTree() {return categoryService.getCategoryList();} 此時基于SSM的操作已經(jīng)完畢,核心就是在寫mybatis的xml,可以在瀏覽器中查看運行結(jié)果,返回的是json數(shù)據(jù)。運行結(jié)果如下

create by xiadewang

轉(zhuǎn)載于:https://www.cnblogs.com/xiadewang/p/8879837.html

總結(jié)

以上是生活随笔為你收集整理的Mybatis通过colliection属性递归获取菜单树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。