美文网首页
Mybatis完成多级树的查询——制造多级树映射的resultM

Mybatis完成多级树的查询——制造多级树映射的resultM

作者: 路上捡只猫 | 来源:发表于2020-02-29 19:14 被阅读0次

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

    相关文章

      网友评论

          本文标题:Mybatis完成多级树的查询——制造多级树映射的resultM

          本文链接:https://www.haomeiwen.com/subject/ibmmhhtx.html