自己开发遇到的后台组装的树结构方法,记录下来
1、首先数据库为oracle,使用 start with 语句和union 组装要查询数据:
SELECT * from (
SELECT equip.EQUIP_CATEGORY_ID as ID,equip.EQUIP_CATEGORY_NAME as name,'' as PID,'1' as LEV FROM SYS_DIS_DEFECT_LIBRARY equip
UNION ALL
SELECT PARTS."ID" as id,PARTS.PARTS_NAME as name,PARTS.EQUIP_CATEGORY_ID as PID,'2' as LEV FROM SYS_DIS_DEFECT_PARTS parts
UNION ALL
SELECT PLACE.id as id,PLACE.PLACE_NAME as name,PLACE.PARTS_ID as pid,'3' as LEV from SYS_DIS_DEFECT_PLACE place
UNION ALL
SELECT DESCR."ID" as id,DESCR.DESCRIPTION as name,DESCR.PLACE_ID as pid,'4' as LEV from SYS_DIS_DEFECT_DESCRIPTION descr
UNION ALL
SELECT CLASSIF."ID" as id,CLASSIF.CLASSIFICATION as name,CLASSIF.DESCRIPTION_ID as pid,'5' as LEV from SYS_DIS_DEFECT_CLASSIFICATION classif
)t START WITH t.pid is null CONNECT by PRIOR t.id = t.pid ;
dao层实现方法
public List<Map<String, Object>> getDefectStandLibraryTree(String rootId) {2、将list数据处理,组装成Tree数据结构
List<Map<String, Object>> result = null;
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("rootId", rootId);
StringBuilder sql = new StringBuilder();
sql.append("SELECT * from ( ");
sql.append("SELECT equip.EQUIP_CATEGORY_ID as ID,equip.EQUIP_CATEGORY_NAME as name,'' as PID FROM SYS_DIS_DEFECT_LIBRARY equip ");
sql.append("UNION ALL ");
sql.append("SELECT PARTS.ID as id,PARTS.PARTS_NAME as name,PARTS.EQUIP_CATEGORY_ID as PID FROM SYS_DIS_DEFECT_PARTS parts ");
sql.append("UNION ALL ");
sql.append("SELECT PLACE.id as id,PLACE.PLACE_NAME as name,PLACE.PARTS_ID as pid from SYS_DIS_DEFECT_PLACE place ");
sql.append("UNION ALL ");
sql.append("SELECT DESCR.ID as id,DESCR.DESCRIPTION as name,DESCR.PLACE_ID as pid from SYS_DIS_DEFECT_DESCRIPTION descr ");
sql.append("UNION ALL ");
sql.append("SELECT CLASSIF.ID as id,CLASSIF.CLASSIFICATION as name,CLASSIF.DESCRIPTION_ID as pid from SYS_DIS_DEFECT_CLASSIFICATION classif ");
if(SystemUtils.isEmpty(rootId)){
sql.append(")t START WITH t.pid is null CONNECT by PRIOR t.id = t.pid ");
result = sqlSupportDao.findBySQL(sql.toString());
}else {
paramMap.put("rootId", rootId);
sql.append(")t START WITH t.pid =:rootId CONNECT by PRIOR t.id = t.pid ");
result = sqlSupportDao.findBySQL(sql.toString(), paramMap);
}
return result;
}
这里使用的迭代的算法
service层
public DslTreeBO getDefectStandLibraryTree(String rootId) {迭代方法
List<Map<String, Object>> result = sysDisDefectLibraryDao
.getDefectStandLibraryTree(rootId);
DslTreeBO treeRoot = new DslTreeBO("0", "缺陷标准库", "0");
if(!SystemUtils.isEmpty(rootId)){
treeRoot.setChildren(getChildren(result,rootId));
}else{
treeRoot.setChildren(getChildren(result,"null"));
}
return treeRoot;
}
// 取节点的所有children
private List<DslTreeBO> getChildren(List<Map<String, Object>> results, String rootId) {
List<DslTreeBO> list = new ArrayList<DslTreeBO>();
for (int i = 0; i < results.size(); i++) {
Map<String, Object> objs = results.get(i);
if (rootId.equals(objs.get("PID")+"")) {
DslTreeBO root = new DslTreeBO(objs.get("ID")+"", objs.get("NAME")+"", objs.get("LEV")+"");
List<DslTreeBO> children = getChildren(results.subList(i, results.size()),
String.valueOf(objs.get("ID")));
if (!children.isEmpty()) {
root.setChildren(children);
}
list.add(root);
}
}
return list;
}
最后是TreeBo实体
public class DslTreeBO {
private String id;
private String name;
private String level;
List<DslTreeBO> children;
//缺陷等级
private String defectLevel;
public String getDefectLevel() {
return defectLevel;
}
public void setDefectLevel(String defectLevel) {
this.defectLevel = defectLevel;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public List<DslTreeBO> getChildren() {
/*if(this.children==null){
return this.children= new LinkedList<DslTreeBO>();
}else{
return children;
}*/
if(this.children==null){
return null;
}else{
return children;
}
}
public void setChildren(List<DslTreeBO> children) {
if(this.children==null){
this.children= new LinkedList<DslTreeBO>();
}
this.children.addAll(children);
}
public DslTreeBO(String id, String name, String level) {
super();
this.id = id;
this.name = name;
this.level = level;
}
public DslTreeBO(String id, String name, String level,String defectLevel) {
super();
this.defectLevel = defectLevel;
this.id = id;
this.name = name;
this.level = level;
}
public DslTreeBO() {
super();
// TODO Auto-generated constructor stub
}
}