mybatis 递归查询
现有一个表的数据如下:
ID | PID | NAME |
---|---|---|
a | 0 | 第一组 |
a_1 | a | 第一组第一个 |
a_2 | a | 第一组第二个 |
b | 0 | 第二组 |
b-1 | b | 第二组第一个 |
<mapper namespace="com.boseidong.cfzxzspzx.mapper.BusinessMapper">
<resultMap id="getSelf" type="com.entity.TreeNode">
<id column="id" property="idTree"/>
<result column="pid" property="pidTree"/>
<result column="name" property="nameTree"/>
<collection property="children" ofType="com.entity.TreeNode" select="getTreeList" column="id">
</collection>
</resultMap>
<select id="getBusinessById" resultMap="getSelf">
select id,pid,name from tree_data pid = #{pid}
</select>
</mapper>
这样是实现逻辑其实是,就是for循环向下查询
select id,pid,name from tree_data pid = '0'
select id,pid,name from tree_data pid = 'a'
select id,pid,name from tree_data pid = 'a-1'
select id,pid,name from tree_data pid = 'a-2'
select id,pid,name from tree_data pid = 'b'
select id,pid,name from tree_data pid = 'b-1'
查询出来的数据
[
{
"id":"a",
"pid":"0",
"name":"第一组",
"children":[
{"id":"a-1","pid":"a","name","第一组第一个"},
{"id":"a-2","pid":"a","name","第一组第二个"}
]
},
{
"id":"b",
"pid":"0",
"name":"第二组",
"children":[
{"id":"b-1","pid":"b","name","第二组第一个"}
]
}
]