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>