获取树形目录结构

一、通过collection实现

1、 TreeMapper.xml

 <resultMap id="cataAndTemplateMap" type="java.util.Map">
	<id column="code" property="code"></id>
	<result column="name" property="name"></result>
	<result column="dtype" property="dtype"></result>
	<collection property="children" column="code" select="selectChildCataOrTemplateByCode" />
 </resultMap>
 
 
 <select id="getReportTemplateList" resultMap="cataAndTemplateMap">
        select Rpt_Cata_Code as "code",Rpt_Cata_Name as "name",0 as "dtype"  from RPT_CATALOG where MODCODE=#{modCode} and (Rpt_Cata_Parent_Code is null or Rpt_Cata_Parent_Code='')
  </select>
 
 <select id="selectChildCataOrTemplateByCode" parameterType="string" resultMap="cataAndTemplateMap">
        select Rpt_Cata_Code as "code",Rpt_Cata_Name as "name",0 as "dtype" from RPT_CATALOG where Rpt_Cata_Parent_Code=#{code}
        Union ALL
        select Rpt_Rt_ID as "code",RPT_RT_TABNAME as "name",1 as "dtype" from RPT_TEMPLATE where RPT_CATA_CODE=#{code}
 </select>

2、 TreeMapper.java

 List<Map> getReportTemplateList(modCode);

缺点:每个目录都要查询是否有下级,数据量大时,查询速度缓慢

二、自己遍历封装

1、TreeMapper.xml

<resultMap id="cataAndTemplateMap" type="java.util.Map">
        <id column="code" property="code"></id>
        <result column="name" property="name"></result>
        <result column="dtype" property="dtype"></result>
        <result column="rptCataParentCode" property="rptCataParentCode"></result>
</resultMap>

<select id="getCatalogList" resultMap="cataAndTemplateMap">
        select Rpt_Cata_Code as "code",Rpt_Cata_Name as "name",0 as "dtype",RPT_CATA_PARENT_CODE as "rptCataParentCode" from RPT_CATALOG where MODCODE=#{modCode} and (Rpt_Cata_Parent_Code is null or Rpt_Cata_Parent_Code='')
</select>
<select id="getChildCataList" resultMap="cataAndTemplateMap">
        select Rpt_Cata_Code as "code",Rpt_Cata_Name as "name",0 as "dtype",RPT_CATA_PARENT_CODE as "rptCataParentCode" from RPT_CATALOG where MODCODE=#{modCode} and (Rpt_Cata_Parent_Code is not null or Rpt_Cata_Parent_Code!='')
</select>

 2、TreeMapper.java

List<Map> getCatalogList(String modeCode);
List<Map> getChildCataList(String modeCode);

3、封装成树的代码

 //一级目录
List<Map> catalogList = reportTemplateMapper.getCatalogList(modCode);
//子级目录
List<Map> childCataList = reportTemplateMapper.getChildCataList(modCode);
//将一级目录、子级目录封装成树
//这里有一个问题,如果存在一个没有父级的但是有rptCataParentCode的目录,将会陷入死循环
while (rptCatalogs.size()>0) {
	for (int i = 0; i < rptCatalogs.size(); i++) {
		Map rptCatalog = rptCatalogs.get(i);
		if (rptCatalog.get("rptCataParentCode") == null) {
			result.add(rptCatalog);
			rptCatalogs.remove(rptCatalog);
			i--;
		} else {
			if(makeCatalogTree(rptCatalog, result, rptCatalogs,rptCataCode)){
				i--;
			};
		}
	}
}



    /**
     * 转换树结构
     * @param rptCatalog
     * @param result
     * @param rptCatalogs
     * @return
     */
   private boolean  makeCatalogTree(Map rptCatalog,List<Map> result,List<Map> rptCatalogs,String rptCataCode){
       for (Map map : result) {
           if(map.get(rptCataCode).equals(rptCatalog.get("rptCataParentCode"))){
               if(map.get("children")==null) {
                   List<Map> childrenList = new ArrayList<>();
                   childrenList.add(rptCatalog);
                   map.put("children", childrenList);
               }else{
                   List<Map> childrenList = (List<Map>) map.get("children");
                   childrenList.add(rptCatalog);
                   map.put("children", childrenList);
               }
               rptCatalogs.remove(rptCatalog);
               return true;
           }else{
               if(map.get("children")!=null){
                   makeCatalogTree(rptCatalog, (List<Map>) map.get("children"),rptCatalogs,rptCataCode);
               }
           }
       }
       return false;
   }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值