java自关联id与pid - 循环递归查询子菜单或者下拉框的实现

准备
课程树状数据查询 , 为CourseType增加children字段

//用来查询树状结构的 课程类型列表
@TableField(exist = false)
private List<CourseType> children = new ArrayList<>();

1.service实现类代码

courseTypes = baseMapper.selectForTreeData();

2.mapper层

//第1条:根据pid=0或pid=xxx查询出第1级课程分类列表
    List<CourseType> selectForTreeData();

3.mapper.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="cn.x.x.mapper.CourseTypeMapper">
    
    <select id="selectForTreeData" resultMap="BaseResultMap">
        SELECT *
        FROM t_course_type where pid = 0
    </select>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.x.x.domain.CourseType">
        <id column="id" property="id" />
        <!--嵌套子查询:查询当前分类下的子分类列表 :1037
        下面的sql:cn.itsource.hrm.mapper.CourseTypeMapper.selectByPid 是重点。相当于在查询返回的结果
        是当前map类型时,它会自动将查询出来的每一条数据的column="id"作为查询条件,来执行select语句
        property :对应的 type="cn.x.x.domain.CourseType"中的属性值。
        将collection查询出来的结果 ,保存到 CourseType中的指定属性中
        ofType : 当前collection执行之后,返回的集合中的对象类型
        column : 表中的字段的值作为select的查询条件
        select : 当前collection将要执行的sql语句(支持2种传入方式:1,Sql语句;2:当前mapper.xml文件中的某一个select的id)
        -->
        <collection property="children"
                    ofType="cn.x.x.domain.CourseType"
                    column="id"
                    select="cn.itsource.th.mapper.CourseTypeMapper.selectByPid" />
    </resultMap>
    <!--根据父分类id,查询列表-->
    <select id="selectByPid" resultMap="BaseResultMap">
     SELECT *
    FROM t_course_type where pid = #{pid}
</select>
</mapper>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值