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代码来实现的,这样做的缺点是:用户无法动态的设置菜单项,比如,超级管理员可能需要根据每个用户的权限,赋予他们不同的系统功能,不同的功能对应着不同数量的菜单项. 对于此问 ...
随机推荐
- 【c++基础】ifstream的构造函数
公共成员函数: ) ifstream(); initialization () explicit ifstream (const char* filename, ios_base::openmode ...
- make: *** No rule to make target `/thread_native.h', needed by `ossl.o'. Stop
修改 Makefile 增加 top_srcdir = ../.. 即可 该文件大多存于ruby源文件下 PS:有时也可能是makefile文件多了空格所致
- BZOJ4403: 序列统计【lucas定理+组合数学】
Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取模的结果. Input 输入第一行包含一个整数T,表示数据组 ...
- HDU 3342:Legal or Not(拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
.NET 中的 async / await 写异步代码用起来真的很爽,就像写同步一样.我们可以在各种各样的异步代码中看到 Task 返回值,这样大家便可以使用 await 等待这个方法.不过,有时需要 ...
- read()/fread()/mmap()执行效率对比
一. read()/fread()/mmap()执行效率对比 系统调用read.c: #include <sys/types.h> #include <sys/stat.h> ...
- MySQL Disk--SSD与RAID
===================================================SSD与RAID 51.在RAID 5这类Parity-RAID上存在partial-stripe ...
- 阿里云流计算专场-GitHub上相关文档
阿里云流计算专场-GitHub路径:https://github.com/Alibaba-Technology/hangzhouYunQi2017ppt
- CodeIgniter 安装指导
CodeIgniter 安装分为四个步骤: 解压缩安装包. 把 CodeIgniter 文件夹和里面的文件上传到你的服务器.通常 index.php 在根目录. 用任何文本编辑器打开 applicat ...
- 使用rewrite 让php 实现类似asp.net 的IHttpModule 进行带参数js文件的参数获取
asp.net 的IHttpModule 接口具有很大的作用,我们可以使用实现的模块进行全局的控制,但是在学习php 的过程中也想实现类似的功能,查找php 的文档,自己没有找到, 但是我们大家应该知 ...