Mybatis通过colliection属性递归获取菜单树
1、现有商品分类数据表category结构如下,三个字段都为varchar类型
2、创建商品分类对应的数据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、创建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的值作为下面的查询返回结果resultMap的值 -->
<!-- collection中的column属性可以为多个值,这里只有一个,它作为下面递归查询传递进去的参数 -->
<!-- ofType和javaType属性正好联合构成了数据Bean类Category中的childrenList属性的数据类型 -->
<!-- select的值为下面递归查询的select标签的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> <!-- 先查询菜单根级目录 -->
<!-- 这里的返回结果必须为resultMap,并且值为上面构建的resultMap的id的值 -->
<select id="getCategoryList" resultMap="categoryTree"> select * from category where pid = 'root' </select> <!-- 再利用上次查询结果colliection中column的值cid做递归查询,查出所有子菜单 -->
<!-- 这里的返回结果必须为resultMap,并且值为上面构建的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 {
@Autowired
private CategoryDao categoryDao;
/* (non-Javadoc)
* @see com.xdw.service.CategoryService#getCategoryList()
*/
@Override
public List<Category> getCategoryList() {
// TODO Auto-generated method stub
return categoryDao.getCategoryList();
} }
4、controller层
@RequestMapping("/getCategoryTree")
@ResponseBody
public List<Category> getCategoryTree() {
return categoryService.getCategoryList();
}
create by xiadewang
Mybatis通过colliection属性递归获取菜单树的更多相关文章
- c#递归读取菜单树
1.查询菜单节点下所有子节点id List<sys_module> menus = new List<sys_module>() { }; public async Task& ...
- java从数据库读取菜单,递归生成菜单树
首先看一下菜单的样子 根据这个样子我们定义菜单类 public class Menu { // 菜单id private String id; // 菜单名称 private String name; ...
- java递归构建菜单树
package testSimple; import java.util.ArrayList; import java.util.List; public class BuildTree { publ ...
- Java递归获取部门树 返回jstree数据
@GetMapping("/getDept")@ResponseBodypublic Tree<DeptDO> getDept(String deptId){ Tree ...
- java生成多级菜单树
使用java实现一个多级菜单树结构 先上数据库 ps_pid字段很重要,是父级菜单的id Menu类 Menu类要新增一个字段,用来存放子菜单 /** * 子菜单列表 */ private List& ...
- React + Antd Menu组件实现菜单树
准备好两个变量,一个用来保存平级菜单列表,一个用来保存遍历后的菜单树. 推荐后端返回平级菜单树,假如菜单比较多,可以直接结合find方法找到菜单,做搜索功能很省事. const [menuList, ...
- php递归获取无限分类菜单
从数据库获取所有菜单信息,需要根据id,pid字段获取主菜单及其子菜单,以及子菜单下的子菜单,可以通过函数递归来实现. <?php class Menu { public $menu = arr ...
- bootstrap treeview实现菜单树
本博客,介绍通过Bootstrap的treeview插件实现菜单树的功能. treeview链接:http://www.htmleaf.com/Demo/201502141380.html ORM框架 ...
- java实现的可以无限级别添加子节点的菜单树
网上大部分菜单树,都是单独用js代码来实现的,这样做的缺点是:用户无法动态的设置菜单项,比如,超级管理员可能需要根据每个用户的权限,赋予他们不同的系统功能,不同的功能对应着不同数量的菜单项. 对于此问 ...
随机推荐
- Samsung_tiny4412(驱动笔记09)----alloc_pages,kmalloc,vmalloc,kmem_cache,class
/*********************************************************************************** * * alloc_pages ...
- IPM简介
1.IPM包含3个函数. image2ground:将图像中的像素点(u, v)对应到地平面上(Z=1)IPM的像素点(x, y): ground2image:将IPM中的像素点(x, y)基于IPM ...
- 获取APP图片资源
iOS开发项目-斗鱼直播APP - 网易云课堂 一. 二.导出Assets.car中的图片资源 cartool
- [LeetCode&Python] Problem 876. Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- Unity 3D换装系统教程/Demo
Unity3D换装系统教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Costume Change ...
- js三级联动
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Complete the Word
ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring ...
- How Many Tables 简单并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- bitset与取数凑数类问题
bitset是C++中的一个东西,定义在头文件#include<bitset>里 所以可以使用#include<bitset>解决取数类的问题https://www.nowco ...
- Java的历史和大事记
Java的历史 在上世纪90 年代初,sun 公司有一个叫做Green 的项目,目的是为家用消费电子产品开发一个分布式代码系统,这样就可以对家用电器进行控制,和它们进行信息交流.詹姆斯·高斯林(Jam ...