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.shop.core.common.mapper.ClubAdminModuleMapper">
  <resultMap id="BaseResultMap" type="com.shop.core.beans.ClubAdminModule">

    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="module_name" jdbcType="VARCHAR" property="moduleName" />
    <result column="parent_id" jdbcType="INTEGER" property="parentId" />
    <result column="url" jdbcType="VARCHAR" property="url" />
    <result column="user_role" jdbcType="INTEGER" property="userRole" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>

  <resultMap id="ResultModuleMap" extends="BaseResultMap" type="com.shop.core.beans.ClubAdminModuleParam" >
    <collection property="moduleList" column="id" select="com.shop.core.common.mapper.ClubAdminModuleMapper.selectByParentId"/>
    <collection property="moduleJurList" column="id" select="com.shop.core.common.mapper.ClubAdminModuleJurMapper.selectByModuleId"/>
    <collection property="role" column="user_role" select="com.shop.core.common.mapper.ClubAdminUserRoleMapper.selectByPrimaryKey"/>
    <collection property="parentModuleName" column="parent_id" select="com.shop.core.common.mapper.ClubAdminModuleMapper.selectParentName"/>
  </resultMap>



 <select id="selectByParentId" resultMap="ResultModuleMap" parameterType="java.lang.Integer" >
    select *
    from club_admin_module
    where parent_id = #{id,jdbcType=INTEGER} order by id
  </select>
 <select id="selectAll" parameterType="java.lang.Integer" resultMap="ResultModuleMap">
    select *
    from club_admin_module
    where 1=1
    <if test="roleId != null and roleId !='' ">
      and user_role = #{roleId,jdbcType=INTEGER}
    </if>
  </select>

首先是,调用selectAll方法进入mapper.xml文件 返回ResultModuleMap 结果映射,ResultModuleMap包含 这条语句,这条语句查询返回ResultModuleMap,一直这样递归调用,就分级查询子集的结果。

注意返回的对象要封装子集的结果集。List moduleList;

是不是很简单!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值