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": []
}
]
}