mybatis递归查询树形结构

mybatis 实现递归查询出树结构节点

结构如下

[{
	"children": [{
		"id": 2,
		"parentId": "1",
		"sortName": "小文",
		"grade": "B",
		"children": []
	}],
	"id": 1,
	"parentId": "0",
	"sortName": "小张",
	"grade": "A",

}]

思路

  • 数据库sql:自查询,查询出所有的字段
  • mybatis:需要使用resultMap嵌套collection,然后在connection中嵌套resultMap,然后再在resultMap中嵌套collection,最后在collection中嵌套最后一个resultMap
  • 关于映射问题:因为从sql语句返回的就是具有三层关系的数据,所以需要用resultMap嵌套collection完成,

至于这三个entity会在代码上贴出
剩下的service层和controller就和普通的一样就可以了

代码实现

实体类

首先是实体类对象,需要通过resultMap和collection关联:

@Getter
@Setter
public class Sort {
    private Long id;
    private Long parentId;
    private String sortName;
    private String grade;
    private Timestamp createTime;
    private Timestamp updateTime;

    @Transient
    private List<Sort> children;

然后编写SQL语句,如下:

① mapper

List<Sort> getAllSort();

mapper.xml

单值传递
    <resultMap id="BaseResultTreeMap" type="org.sang.bean.Sort">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="parent_id" jdbcType="BIGINT" property="parentId"/>
        <result column="parent_Name" jdbcType="VARCHAR" property="parentName"/>
        <result column="sort_name" jdbcType="VARCHAR" property="sortName"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <!--使用mybatis  collection 进行集合查询-->
        <result column="grade" jdbcType="VARCHAR" property="grade"/>
        <collection property="children" ofType="Sort" select="selectTree" column="id" javaType="java.util.ArrayList"/>
    </resultMap>
    
    <!--父级查询-->
    <select id="getAllSort" resultMap="BaseResultTreeMap">
        select * from sort s where s.id parent_id = 0 //从parent_id为0开始递归
    </select>
    
    <!--关联集合查询-->
    <select id="selectTree" parameterType="String" resultMap="BaseResultTreeMap">
        select * from sort s where s.parent_id=#{id}
    </select>

注释:

  • collection 即为嵌套的List配置
  • property 为 private List children 的字段名 children
  • ofType 为private List children 的类型Sort
  • select 为要递归的sql语句
  • column 上一条语句查询的结果作为下一条语句的参数
多值传递

将column改为

column="{id=id,grade=grade}"

结果如下

    <resultMap id="BaseResultTreeMap" type="org.sang.bean.Sort">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="grade" jdbcType="VARCHAR" property="grade"/>
        <result column="parent_id" jdbcType="BIGINT" property="parentId"/>
        <result column="parent_Name" jdbcType="VARCHAR" property="parentName"/>
        <result column="sort_name" jdbcType="VARCHAR" property="sortName"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <!--使用mybatis  collection 进行集合查询-->
        <collection property="children" ofType="Sort" select="selectTree" column="{id=id,grade=grade}" javaType="java.util.ArrayList"/>
    </resultMap>
    
    <!--父级查询-->
    <select id="getAllSort" resultMap="BaseResultTreeMap">
        select * from sort s where s.parent_id = 0 //从parent_id为0开始递归
    </select>
    
    <!--关联集合查询-->
    <select id="selectTree" parameterType="String" resultMap="BaseResultTreeMap">
        select * from sort s where s.parent_id=#{id} and grade = #{grade}
    </select>

如果没有在resultMap中配置需要直接使用,可以如下在参数中

把查到结果中对应的字段值 匹配 配置的 参数名,一一对应的设置为下一次查询的参数值。
grade = #{grade,jdbcType=VARCHAR}

    <resultMap id="BaseResultTreeMap" type="org.sang.bean.Sort">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="grade" jdbcType="VARCHAR" property="grade"/>
        <result column="parent_id" jdbcType="BIGINT" property="parentId"/>
        <result column="parent_Name" jdbcType="VARCHAR" property="parentName"/>
        <result column="sort_name" jdbcType="VARCHAR" property="sortName"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <!--使用mybatis  collection 进行集合查询-->
        <collection property="children" ofType="Sort" select="selectTree" column="{id=id,grade=grade}" javaType="java.util.ArrayList"/>
    </resultMap>
    
    <!--父级查询-->
    <select id="getAllSort" resultMap="BaseResultTreeMap">
        select * from sort s where s.id parent_id = 0 //从parent_id为0开始递归
    </select>
    
    <!--关联集合查询-->
    <select id="selectTree" parameterType="String" resultMap="BaseResultTreeMap">
        select * from sort s where s.parent_id=#{id} 
and grade =  #{grade,jdbcType=VARCHAR}
    </select>

递归完成,剩下的即为service和controller的业务代码

  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MyBatis中查询树形结构,可以通过使用递归的方式来实现。具体步骤如下: 1. 定义一个实体类,包含树形结构节点的id节点id和节点名称等属性。 2. 编写Mapper.xml文件,使用递归查询方式来获取树形结构数据。 3. 在Mapper.xml文件中定义一个递归查询语句,通过连接查询子节点和节点来获取树形结构数据。 4. 在Mapper.xml文件中定义一个查询所有节点的语句,用于查询整个树形结构的数据。 5. 在Java代码中调用Mapper接口中定义的方法来查询树形结构数据。 以下是一个简单的例子,展示如何在MyBatis中查询树形结构: 1. 定义实体类TreeNode: ```java public class TreeNode { private Long id; private Long parentId; private String name; // getter和setter方法 } ``` 2. 编写Mapper.xml文件: ```xml <!-- 递归查询语句 --> <select id="selectTreeNodes" resultMap="TreeNodeMap"> with recursive cte(id, parent_id, name) as ( select id, parent_id, name from tree_node where parent_id is null union all select tn.id, tn.parent_id, tn.name from tree_node tn join cte on tn.parent_id = cte.id ) select * from cte; </select> <!-- 查询所有节点语句 --> <select id="selectAllTreeNodes" resultMap="TreeNodeMap"> select * from tree_node; </select> <resultMap id="TreeNodeMap" type="TreeNode"> <id property="id" column="id"/> <result property="parentId" column="parent_id"/> <result property="name" column="name"/> </resultMap> ``` 3. 在Java代码中调用Mapper接口中定义的方法来查询树形结构数据: ```java public interface TreeNodeMapper { List<TreeNode> selectTreeNodes(); List<TreeNode> selectAllTreeNodes(); } // 调用方式 SqlSession sqlSession = sqlSessionFactory.openSession(); TreeNodeMapper mapper = sqlSession.getMapper(TreeNodeMapper.class); List<TreeNode> treeNodes = mapper.selectTreeNodes(); List<TreeNode> allTreeNodes = mapper.selectAllTreeNodes(); sqlSession.close(); ``` 通过以上步骤,我们可以在MyBatis中轻松地查询树形结构数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值