先贴代码
public class NewsCatalogVO {
private String text;
private String id;
private List<NewsCatalogVO> children;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<NewsCatalogVO> getChildren() {
return children;
}
public void setChildren(List<NewsCatalogVO> children) {
this.children = children;
}
}
/**
* 数据访问层
*/
public class NewsCatalogDaoImpl implements NewsCatalogDao {
private PersistTemplate template;
@Override
public List<NewsCatalogVO> getAllDataById(String id) {
List<NewsCatalogVO> returnDatas = new ArrayList<NewsCatalogVO>();
List<NewsCatalog> datas = template.find("from NewsCatalog t where t.catalogId<>'"+id+"'");
List<NewsCatalog> nc = template.find("from NewsCatalog t where t.catalogId='"+id+"'");
if(datas.size()>0&&nc.size()>0){
returnDatas.add(initNewsCatalogVO(datas,nc.get(0)));
}
return returnDatas;
}
/**
* 递归数据 转成 符合 easyui tree 格式数据对象
* @param datasParam
* @param nc
* @return
*/
public NewsCatalogVO initNewsCatalogVO(List<NewsCatalog> datasParam,NewsCatalog nc){
List<NewsCatalogVO> chileren = new ArrayList<NewsCatalogVO>();
for(NewsCatalog model :datasParam){
if(model.getCatalogParentId()!=null){
if(model.getCatalogParentId().equals(nc.getCatalogId())){
chileren.add(initNewsCatalogVO(datasParam,model));
}
}
}
NewsCatalogVO ncvo = new NewsCatalogVO();
ncvo.setId(nc.getCatalogId());
ncvo.setText(nc.getCatalogName());
ncvo.setChildren(chileren);
return ncvo;
}
public PersistTemplate getTemplate() {
return template;
}
public void setTemplate(PersistTemplate template) {
this.template = template;
}
}
写下感受:
1.写递归的时候 先完成 第一步(不要去想其他步骤 ),第一步的意思就是先完成一个对象基本属性(不包含数组集合的属性)【想这种树状的数据肯定是包含集合 而 对象本身又是组成集合。其实也可以这样去想:找到最后一个没有子节点的对象(即:没有集合的对象),这个对象便是最根上的,那么他的上一个对象的集合中肯定要包含它,那么就要把他给返回 即 return ,返回之后 要把它放到 集合中.....】这样呢 代码就跟着感觉一点一点出来了。。。。。。。。。