以下为Mybatis映射文件
<?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="com.org.mabatis.testDao">
<resultMap id="BaseTreeResultMap" type="com.org.model.NodeModel">
<id property="id" jdbcType="VARCHAR" column="id"/>
<result property="node" jdbcType="VARCHAR" column="node"/>
<result property="p_node_id" jdbcType="VARCHAR" column="pNodeId"/>
<collection property="subNode" ofType="nodeModel" column="id" jdbcType="java.util.ArrayList"
select="getSubNode"/>
</resultMap>
<resultMap id="SubTreeResultMap" type="com.org.model.NodeModel">
<id property="id" jdbcType="VARCHAR" column="id"/>
<result property="node" jdbcType="VARCHAR" column="node"/>
<result property="p_node_id" jdbcType="VARCHAR" column="pNodeId"/>
<collection property="subNode" ofType="nodeModel" column="id" jdbcType="java.util.ArrayList"
select="getSubNode"/>
</resultMap>
<sql id="Base_Column">
id, node, p_node_id
</sql>
<select id="getBaseNode" resultMap="BaseTreeResultMap">
select
<include refid="Base_Column"/>
from sys_node
where p_node_id = '0'
</select>
<select id="getSubNode">
select
<include refid="Base_Column"/>
from sys_node
where p_node_id = #{id}
</select>
</mapper>
以下为结果json
[
{
"id":"1",
"node":"一级节点",
"pNodeId":"0",
"subNode":[
{
"id":"2",
"node":"二级节点",
"pNodeId":"1",
"subNode":[
{
"id":"3",
"node":"三级节点",
"pNodeId":"2",
"subNode":[
{
"id":"4",
"node":"四级节点",
"pNodeId":"3",
"subNode":[
]
}
]
}
]
}
]
},
{
"id":"5",
"node":"一级节点",
"pNodeId":"0",
"subNode":[
{
"id":"6",
"node":"二级节点",
"pNodeId":"5",
"subNode":[
{
"id":"7",
"node":"三级节点",
"pNodeId":"6",
"subNode":[
{
"id":"8",
"node":"四级节点",
"pNodeId":"7",
"subNode":[
]
}
]
}
]
}
]
}
]
网友评论