咱們常常要根據數據庫表的結構,生成樹形結構在頁面顯示;下面就是一個例子:javascript
頁面的tree組件採用的是EasyUI 的 Tree 組件。html
數據庫結構:java
表名稱: tDictnode
Id name parentid sortid valid數據庫
主鍵 名稱 父ID 排序ID 是否可用session
tDict 實體類中,父ID以 tDict 實體類表述,以下:app
public class TDict{ // Fields private String id; private TDict tDict; private Integer sortid; private String valid; private String name; ……省略<Get & Set> }
要用到Hibernate的支持,如下是模型層的映射文件 hbmthis
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping default-lazy="false"> <class name="com.bzinfo.crm.model.TDict" table="T_DICT"> <id name="id" type="java.lang.String"> <column name="ID" length="50" /> <generator class="assigned" /> </id> <many-to-one name="tDict" class="com.bzinfo.crm.model.TDict" lazy="false"> <column name="PARENTID" length="50" /> </many-to-one> <property name="sortid" type="java.lang.Integer"> <column name="SORTID" precision="6" scale="0" /> </property> <property name="valid" type="java.lang.String"> <column name="VALID" length="1" /> </property> <property name="name" type="java.lang.String"> <column name="NAME" length="50" /> </property> </hibernate-mapping>
首先,創建數節點類:spa
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class TreeNode { privateString id; //要顯示的子節點的ID privateString text; //要顯示的子節點的 Text privateString iconCls; //節點的圖標 privateString parentId; //父節點的ID privateList<TreeNode> children; //孩子節點的List publicTreeNode(){} publicTreeNode(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; } publicString getId() { returnid; } publicvoid setId(String id) { this.id= id; } publicString getText() { returntext; } publicvoid setText(String text) { this.text= text; } publicString getIconCls() { returniconCls; } publicvoid setIconCls(String iconCls) { this.iconCls= iconCls; } publicString getParentId() returnparentId; } publicvoid setParentId(String parentId) { this.parentId= parentId; } publicList<TreeNode> getChildren() { returnchildren; } publicvoid setChildren(List<TreeNode> children) { this.children= children; } //添加孩子的方法 publicvoid addChild(TreeNode node){ if(this.children == null){ children= new ArrayList<TreeNode>(); children.add(node); }else{ children.add(node); } } }
下面是生成樹的方法:.net
@SuppressWarnings("unchecked") public List fillTree(String tableName){ String hql = " from TDictt where valid='1' order by t.sortid "; List<TreeNode> list = new ArrayList<TreeNode>(); Map map = new HashMap<String, TreeNode>(); try { //拉出數據庫的數據,放入list2中 ArrayList<TDict> list2 = (ArrayList<TDict>)this.find(hql); //將list2中的數據,轉換成TreeNode類型,放入Map中備用 for (TDict tDict : list2) { TreeNode node = new TreeNode(); node.setId(tDict.getId()); node.setText(tDict.getName()); if(tDict.gettDict()!=null){ node.setParentId(tDict.gettDict().getId()); } map.put(tDict.getId(), node); } //遍歷list2的數據,把每一個節點加入他的父節點的孩子List for (TDict tDict : list2) { if(tDict.gettDict()!= null){ if(tDict.gettDict().getId() == null) { list.add((TreeNode)map.get(tDict.getId())); }else{ String pidString = tDict.gettDict().getId(); TreeNode pnode = (TreeNode)map.get(pidString); TreeNode cnode=(TreeNode)map.get(tDict.getId()); pnode.addChild(cnode); } }else{ list.add((TreeNode)map.get(tDict.getId())); } } }catch (Exception e) { // TODO: handleexception e.printStackTrace(); } return list; }
在頁面顯示的時候,把list轉換成Json格式:
List treeSet = treeManagerImpl.fillTree(null); String treeString = gson.toJson(treeSet); session.setAttribute("tree", treeString);
在頁面取Json數據,並顯示
<script type="text/javascript"> window.onload =function(){ tree = ${tree}; $('#tt').tree({ data:tree }); }; </script>