collection、ofType、select的联合用法(Mybatis实现树状结构查询)

需求

得到树结构数据也可以用lambda表达式也行,也可以直接循环递归也行,本文采用的是直接在Mybatis层得到结果,各有各的优势。

代码

1、实体类

@Data
public class CourseChapterVO implements Serializable {
    private static final long serialVersionUID = -67718827554034368L;
    /**
     * 主键ID
     */
    private Integer id;
    /**
     * 课程ID
     */
    private Integer courseId;
    /**
     * 课程章节名称
     */
    private String chapterName;
    /**
     * 父ID
     */
    private Integer chapterParent;
    /**
     * 层级
     */
    private Integer chapterLevel;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)  //当children为空时不返回    private List<CourseChapterVO> children;
 }

2、mapper.xml

<resultMap type="com.ruoyi.manager.vo.CourseChapterVO" id="CourseChapterMap2">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="courseId" column="course_id" jdbcType="INTEGER"/>
        <result property="chapterName" column="chapter_name" jdbcType="VARCHAR"/>
        <result property="chapterParent" column="chapter_parent" jdbcType="INTEGER"/>
        <result property="chapterLevel" column="chapter_level" jdbcType="INTEGER"/>
        <collection property="children" ofType="com.ruoyi.manager.vo.CourseChapterVO" column="id" select="selectListTree2"/>
    </resultMap>

    <select id="selectListTree1" resultMap="CourseChapterMap2">
        select id , course_id, chapter_name, chapter_parent, chapter_level
        from course_chapter where chapter_parent =0 </select>

    <select id="selectListTree2" resultMap="CourseChapterMap2">
        select id , course_id, chapter_name, chapter_parent, chapter_level from course_chapter where chapter_parent =#{id}
    </select>

3、mapper.java

List<CourseChapterVO> selectListTree1(CourseChapter courseChapter);

4、serviceImpl.java

 @Override
    public List<CourseChapterVO> queryAll(CourseChapter courseChapter) {
        return this.courseChapterMapper.selectListTree1(courseChapter);
    }

5、service.java

List<CourseChapterVO> queryAll(CourseChapter courseChapter);

6、controller.java

 @GetMapping("list")
    public R queryAll(CourseChapter courseChapter) {
        return R.data(this.courseChapterService.queryAll(courseChapter));
    }

7、实现效果

{
    "msg": "",
    "code": 0,
    "data": [
        {
            "id": 1,
            "courseId": 5,
            "chapterName": "第一章节学习",
            "chapterParent": 0,
            "chapterLevel": 1,
            "children": [
                {
                    "id": 2,
                    "courseId": 5,
                    "chapterName": "第一章节学习子1",
                    "chapterParent": 1,
                    "chapterLevel": 2
                },
                {
                    "id": 3,
                    "courseId": 5,
                    "chapterName": "第一章节学习子2",
                    "chapterParent": 1,
                    "chapterLevel": 2
                },
                {
                    "id": 4,
                    "courseId": 5,
                    "chapterName": "第一章节学习子3",
                    "chapterParent": 1,
                    "chapterLevel": 2
                }
            ]
        },
        {
            "id": 5,
            "courseId": 5,
            "chapterName": "第二章节学习",
            "chapterParent": 0,
            "chapterLevel": 1,
            "children": [
                {
                    "id": 6,
                    "courseId": 5,
                    "chapterName": "第二章节学习子",
                    "chapterParent": 5,
                    "chapterLevel": 2,
                    "children": [
                        {
                            "id": 9,
                            "courseId": 5,
                            "chapterName": "第二章节学习子1子1",
                            "chapterParent": 6,
                            "chapterLevel": 3
                        }
                    ]
                },
                {
                    "id": 7,
                    "courseId": 5,
                    "chapterName": "第二章节学习子1",
                    "chapterParent": 5,
                    "chapterLevel": 2
                },
                {
                    "id": 8,
                    "courseId": 5,
                    "chapterName": "第二章节学习子2",
                    "chapterParent": 5,
                    "chapterLevel": 2
                }
            ]
        },
        {
            "id": 10,
            "courseId": 5,
            "chapterName": "第三章节学习gfdgdf",
            "chapterParent": 0,
            "chapterLevel": 1
        }
    ]
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用MyBatiscollection标签进行select查询时,有两种常见的用法。 第一种用法是关联查询,通过在resultMap中使用collection标签将两个表关联起来。在查询语句中使用嵌套select查询,以获取需要的数据。具体的配置如下: ```xml <resultMap type="A类路径" id="itemNew"> <id column="id" property="a_id"/> <result column="value" property="a_value"/> <collection column="{aId=a_id}" property="与A类字段名一致" ofType="B类路径" select="getList"/> </resultMap> <select id="getListNew(与dao中方法名保持一致)" parameterType="类路径" resultMap="itemNew"> select a.id a_id ,a.value a_value from table_a a </select> <resultMap type="类路径" id="itemList"> <result column="id" property="b_id"/> <result column="value" property="b_value"/> </resultMap> <select id="getList(与上面select标签值保持一致)" resultMap="itemList"> select b.id b_id , b.value b_value from table_b b where b.aid = #{aId} </select> ``` 第二种用法是一个SQL查询出所有需要的数据。在resultMap中使用collection标签将两个表关联起来,通过指定一个字段作为关联条件。具体的配置如下: ```xml <resultMap type="A类路径" id="item"> <id column="id" property="id"/> <result column="value" property="value"/> <collection column="id" property="与A类路径字段名一致" ofType="B类路径"> <result column="id" property="b_id"/> <result column="value" property="b_value"/> </collection> </resultMap> ``` 其中,column="{aId=a_id}"用于传值给子collection,如果需要传多个参数,可以用逗号隔开,例如column="{aId=a_id,e=e}"。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值