Mybatis实现级联查询

本文介绍了如何使用Mybatis进行级联查询,通过在实体类中添加@JsonIgnoreProperties注解忽略特定属性,并在xml映射文件中配置collection元素实现递归查询。同时,解释了在使用懒加载时为何需要该注解以及如何设置fetchType为lazy。
摘要由CSDN通过智能技术生成

Mybatis实现级联查询

可以通过collection集合实现递归查询,没必要断开连断开连的这种去查询

数据库表结构

在这里插入图片描述

实体类

这里添加了@JsonIgnoreProperties(value = {“handler”})注解,作用是忽略了handler这个属性不需要转化成json。(这是用了懒加载的情况下,用了懒加载不加注解会报错,数据量不大不用懒加载不需要加这个注解)还要加一个List属性
至于为什么可以看下这个博主的文章
https://www.cnblogs.com/orangeCitrus/p/10203554.html

@Data
@JsonIgnoreProperties(value = {"handler"})
public class BDivisioninfo implements Serializable {
    private String id;
    private String pid;
    private String leavel;
    private String areaName;
    private List<BDivisioninfo> children;
    private Integer parent;
    private static final long serialVersionUID = 1L;
}

xml

类似于一对多关联查询

<mapper namespace="com.srq.dao.BDivisioninfoDao">
    <resultMap id="BaseResultMap" type="com.srq.pojo.BDivisioninfo">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="pid" jdbcType="VARCHAR" property="pid"/>
        <result column="leavel" jdbcType="VARCHAR" property="leavel"/>
        <result column="area_name" jdbcType="VARCHAR" property="areaName"/>
        <result column="parent" jdbcType="VARCHAR" property="parent"/>
        <collection
                property="children" javaType="java.util.List" ofType="com.srq.pojo.BDivisioninfo"
                select="com.srq.dao.BDivisioninfoDao.selectByPid" column="id" fetchType="lazy">
        </collection>
    </resultMap>
    <sql id="ColumnList">
    id,pid,leavel,area_name,parent
    </sql>
    <select id="selectByPid" resultMap="BaseResultMap">
        select
        <include refid="ColumnList"/>
        from b_divisioninfo
        where pid = #{id}
    </select>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙嵓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值