SprintBoot Mapper 树形查询
DeptMapper.xml
<?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.zhen.mp.mapper.DeptMapper">
<resultMap id="treeMap" type="com.zhen.mp.dto.dept.DeptTreeDto">
<id column="id" property="id"/>
<result column="p_id" property="ppId" />
<result column="name" property="name" />
<result column="sort" property="sort" />
<collection column="id" property="sonNode" javaType="java.util.ArrayList" ofType="com.zhen.mp.dto.dept.DeptTreeDto" select="getNextTree"/>
</resultMap>
<select id="getTree" resultMap="treeMap">
select * from dept where p_id is null;
</select>
<select id="getNextTree" resultMap="treeMap">
select * from dept where p_id = #{id}
</select>
</mapper>
DeptMapper.java
package com.zhen.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhen.mp.bean.Dept;
import com.zhen.mp.dto.dept.DeptTreeDto;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface DeptMapper extends BaseMapper<Dept> {
List<DeptTreeDto> getTree();
List<DeptTreeDto> getNextTree();
}
Dept.java
package com.zhen.mp.bean;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
@Data
@TableName("dept")
public class Dept extends Model {
@TableId(type = IdType.AUTO)
private Long id;
private Long pId;
private String name;
private Integer sort;
}
DeptTreeDto.java
package com.zhen.mp.dto.dept;
import lombok.Data;
import java.util.List;
@Data
public class DeptTreeDto {
private Long id;
private Long ppId;
private String name;
private Integer sort;
private List<DeptTreeDto> sonNode;
}