通过表关联查询方式实现 children List 树

SQL 语句

<!--说明:针对输入参数为简单类型#{}中可以是任意类型,判断参数是否为空要用 _parameter(它属于mybatis的内 置参数)-->
    <select id="selectList" resultMap="teachplanMap" parameterType="java.lang.String">
        select
            a.id one_id,
            a.pname one_name,
            b.id two_id,
            b.pname two_name,
            c.id three_id,
            c.pname three_name
        from
            table_name a left join table_name b on a.id = b.parentid left join table_name c on b.id = c.parentid
        where a.parentid = '0'
            <if test="_parameter != null and _parameter != ''">
                and a.id= #{id}
            </if>
        order by
            a.orderby,
            b.orderby,
            c.orderby
    </select>

<resultMap id="teachplanMap" type="com.xuecheng.framework.domain.course.ext.TeachplanNode">
        <id property="id" column="one_id"/>
        <result property="pname" column="one_name"/>
        <collection property="children" ofType="com.xuecheng.framework.domain.course.ext.TeachplanNode">
            <id property="id" column="two_id"/>
            <result property="pname" column="two_name"/>
            <collection property="children" ofType="com.xuecheng.framework.domain.course.ext.TeachplanNode">
                <id property="id" column="three_id"/>
                <result property="pname" column="three_name"/>
            </collection>
        </collection>
    </resultMap>

结构实体

@Data
@ToString
public class TeachplanNode extends Teachplan {

    List<TeachplanNode> children;

}
@Data
@ToString
@Entity
@Table(name="teachplan")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class Teachplan implements Serializable {
    private static final long serialVersionUID = -916357110051689485L;
    @Id
    @GeneratedValue(generator = "jpa-uuid")
    @Column(length = 32)
    private String id;
    private String pname;
    private String parentid;
    private String grade;
    private String ptype;
    private String description;
    private String courseid;
    private String status;
    private Integer orderby;
    private Double timelength;
    private String trylearn;

}
{
    "id": "402885816243d2dd016243f24c040003",
    "pname": "第一章",
    "parentid": null,
    "grade": null,
    "ptype": null,
    "description": null,
    "courseid": null,
    "status": null,
    "orderby": null,
    "timelength": null,
    "trylearn": null,
    "children": [
        {
            "id": "297e02f7639af61a01639afd3a7b0000",
            "pname": "第一节",
            "parentid": null,
            "grade": null,
            "ptype": null,
            "description": null,
            "courseid": null,
            "status": null,
            "orderby": null,
            "timelength": null,
            "trylearn": null,
            "children": []
        }
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会跑的葫芦怪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值