Java获取部门组织树

本文章记录两种方式获取部门树型结构
一、使用mybatis,因循环查库,比较耗时,
二、递归查询

一、使用mybatis实现

1.编写实体类,TreeVO.java

@Data
public class TreeVO {

    private String value;
    private String label;

    private List<TreeVO> children = new ArrayList<>();

}

2.编写sql 语句,CommonMapper.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="CommonMapper">
    <resultMap id="BaseResultMap" type="TreeVO">
        <result property="value"  column="id"/>
        <result property="label"  column="name"/>
        
        <collection property="children"  javaType="java.util.ArrayList" ofType="TreeVO"
                    select="getChildLists" column="id">
        collection>
    resultMap>

    <select id="getAllParentOrgs" resultMap="BaseResultMap">
        SELECT org.id ,org.`name`  FROM
        organization org
    WHERE
        ISNULL( org.parent_id )
    select>

    <select id="getChildLists" parameterType="String" resultMap="BaseResultMap">
        select org.id ,org.`name` from organization org where parent_id=#{id}
    select>
mapper>

3.编写mapper类,CommonMapper.java

@Repository
public interface CommonMapper {

    /**
     * 获取组织树
     *
     * @return
     */
    List<TreeVO> getAllParentOrgs();

    /**
     * 根据id的子组织
     * @return
     */
    List<TreeVO> getChildLists(String id);
}

4.编写controller,CommonController.java

@Autowired
private CommonMapper commonMapper;
//list即为我们需要的数据
List<TreeVO> list =  commonMapper.getAllParentOrgs();

到此第一种方式已经完成。

第二种,递归方式

1.实体类同上面TreeVO
2.编写组织实体类,OrganizationVO.java

@Data
public class OrganizationVO {

    private String orgId;

    private String orgName;

    private String parentId;
}

3.编写sql语句,CommonMapper.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="CommonMapper">
    
    <select id="getParentOrgs" resultType="TreeVO">
    SELECT
	org.id `value` ,
	org.`name` label
    FROM
	organization org
    WHERE
	ISNULL( org.parent_id )
    select>

    
    <select id="getChildOrgs" resultType="OrganizationVO">
    SELECT
	org.id orgId,
	org.`name` orgName,
	org.parent_id parentId
    FROM
	organization org
    WHERE
	parent_id IS NOT NULL
    select>

mapper>

4.编写CommonMapper.java

@Repository
public interface CommonMapper {
    /**
     * 子组织
     *
     * @return
     */
    List<OrganizationVO> getChildOrgs();

    /**
     * 父组织
     *
     * @return
     */
    List<TreeVO> getParentOrgs();
}

5.编写CommonController.java

public List<TreeVO> getOrgList() {
        List<TreeVO> allParentOrgs = commonMapper.getParentOrgs();
        List<OrganizationVO> childOrgs = commonMapper.getChildOrgs();
        allParentOrgs.forEach(treeVO -> {
            List<TreeVO> children = treeVO.getChildren();
            getOrgTree(treeVO.getValue(),childOrgs,children);
        });
        return allParentOrgs;
    }
    
private void getOrgTree(String parentId,List<OrganizationVO> childOrgs,List<TreeVO> children) {
       Iterator<OrganizationVO> iterator = childOrgs.iterator();
       while (iterator.hasNext()) {
           OrganizationVO organizationVO = iterator.next();
           if (parentId.equals(organizationVO.getParentId())) {
               String orgId = organizationVO.getOrgId();
               String orgName = organizationVO.getOrgName();
               TreeVO treeVO = new TreeVO();
               treeVO.setLabel(orgName);
               treeVO.setValue(orgId);
               List<TreeVO> nextChildren = new ArrayList<>();
               treeVO.setChildren(nextChildren);
               getOrgTree(orgId, childOrgs, nextChildren);
               children.add(treeVO);
           }
       }
    }

至此两种方式都已完成。

你可能感兴趣的:(Java,java,开发语言,后端)