一、通过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;
}