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();
    }

此时基于SSM的操作已经完毕,核心就是在写mybatis的xml,可以在浏览器中查看运行结果,返回的是json数据。运行结果如下

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值