EasyUI_tree根据数据库数据生成树形结构JSON格式

@Entity
public class PubComp {
 @Id
 private String aguid; // 菜单ID
 private String pguid; // 父菜单
 private String aname;// 名称

}

 

public class TreeNode {
  private String id;          //要显示的子节点的ID 
     private String text;        //要显示的子节点的 Text 
     private String iconCls;     //节点的图标 
     private String parentId;    //父节点的ID 
     private List<TreeNode>  children;   //孩子节点的List 
    
     public TreeNode(){}
  public TreeNode(String id, String text, String iconCls,
    String parentId, List<TreeNode> children) {
   super();
   this.id = id;
   this.text = text;
   this.iconCls = iconCls;
   this.parentId = parentId;
   this.children = children;
  }
  public String getId() {
   return id;
  }
  public void setId(String id) {
   this.id = id;
  }
  public String getText() {
   return text;
  }
  public void setText(String text) {
   this.text = text;
  }
  public String getIconCls() {
   return iconCls;
  }
  public void setIconCls(String iconCls) {
   this.iconCls = iconCls;
  }
  public String getParentId() {
   return parentId;
  }
  public void setParentId(String parentId) {
   this.parentId = parentId;
  }
  public List<TreeNode> getChildren() {
   return children;
  }
  public void setChildren(List<TreeNode> children) {
   this.children = children;
  }
  
  //添加子菜单的方法 
     public void addChild(TreeNode node){ 
        if(this.children == null){ 
            children= new ArrayList<TreeNode>(); 
            children.add(node); 
        }else{ 
            children.add(node); 
        } 
            
     } 

 

 public String load(ModelMap modelMap){
    List<PubComp> pcList=testCaseDaoImpl.queryPubComp("select p from PubComp p ");   
         modelMap.put("list", pcList); //需要返回的数据(pcList)
        
         List<TreeNode> list = new ArrayList<TreeNode>(); 
         Map map = new HashMap<String, TreeNode>(); 
   
         try { 
   
             //拉出数据库的数据,放入pcList中 
             //将list2中的数据,转换成TreeNode类型,放入Map中备用 
             for (PubComp tDict : pcList) { 
                TreeNode node = new TreeNode(); 
                node.setId(tDict.getAguid()); 
                node.setText(tDict.getAname()); 
                node.setParentId(tDict.getPguid()); 
                map.put(tDict.getAguid(), node); 
   
             } 
             //遍历pcList的数据,把每个节点加入他的父节点的孩子List 
             for (PubComp tDict : pcList) { 
                    if(tDict.getPguid() == null) 
                    { 
                       list.add((TreeNode)map.get(tDict.getAguid())); 
                    }else{ 
   
                       String pidString = tDict.getPguid(); 
                       TreeNode pnode = (TreeNode)map.get(pidString); 
                       TreeNode cnode=(TreeNode)map.get(tDict.getAguid()); 
                       pnode.addChild(cnode); 
                    } 
             }        
            
             //将对象转换成json(注意需要添加gson-2.2.2.jar)
             Gson g = new GsonBuilder().create();
       String json = g.toJson(list,list.getClass());
             System.out.println(json+"-----");
            
            
            
            
         }catch (Exception e) { 
             e.printStackTrace(); 
         }
    

转载于:https://www.cnblogs.com/qgc88/p/3286628.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值