mybatis 递归查询

java 实体

@Data
public class CodeTree{
    private Long id;
    private String pid;
    private String code;
    private String name;
    private List<CodeTree> children;
}

mapper 接口

// 根据 id 递归查询其子节点(不包含当前节点)
List<CodeTree> listByrecursion(Long id);

映射文件

<resultMap id="RecursionMap" type="com.ws.project.transfer.project.xin.wbs.entity.CodeTree">
    <id column="id" jdbcType="LONG" property="id" />
    <result column="parent_id" jdbcType="VARCHAR" property="pid" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <collection property="children" 
              ofType="com.ws.project.transfer.project.xin.wbs.entity.CodeTree" 
              column="id" 
              select="listByrecursion" />
</resultMap>

<select id="listByrecursion" resultMap="RecursionMap">
    SELECT <include refid="Base_Column_List" /> FROM T_CODE_TREE WHERE PID = #{id}
</select>
posted @ 2023-07-31 09:59  CyrusHuang  阅读(191)  评论(0编辑  收藏  举报