一、 数据库表结构
二、从后台读取数据库生成easyui tree结构的树
1、TreeNode树结点类(每个结点都包含easyui tree 的基本属性信息)
import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class TreeNode implements Serializable { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } private String text; public String getText() { return text; } public void setText(String text) { this.text = text; } private String parentid; public String getParentid() { return parentid; } public void setParentid(String parentid) { this.parentid = parentid; } private List<TreeNode> children = new ArrayList<TreeNode>(); public List<TreeNode> getChildren() { return children; } public void setChildren(List<TreeNode> children) { this.children = children; } private String iconCls; public String getIconCls() { return iconCls; } public void setIconCls(String iconCls) { this.iconCls = iconCls; } private String state;//节点状态,'open' 或 'closed',默认:'open'。 public String getState() { return state; } public void setState(String state) { this.state = state; } private boolean checked; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } private Map<String, Object> attributes = new HashMap<String, Object>(); public Map<String, Object> getAttributes() { return attributes; } public void setAttributes(Map<String, Object> attributes) { this.attributes = attributes; } }
2、用递归方法生成easyui tree 格式的json字符串
public static TreeNode recursiveTree(String id) { // 根据id获取节点对象(SELECT * FROM ttree t WHERE t.id=?) TreeNode node = getTreeNode(id); // 查询id下的所有子节点(SELECT * FROM ttree t WHERE t.parentid=?) List<TreeNode> childTreeNodes = queryTreeNode(id); // 遍历子节点 for (TreeNode child :childTreeNodes) { TreeNode n = recursiveTree(child.getId()); // 递归 node.getChildren().add(n); } return node; }
private static TreeNode getTreeNode(String id){ TreeNode treeNode = null; Connection dbConn = getDbConnection(); if(dbConn != null){ try { String strSQL = "select * from ttree where id="+id; PreparedStatement pstmt = null; ResultSet rs = null; pstmt = dbConn.prepareStatement(strSQL); rs = pstmt.executeQuery(); while (rs.next()) { treeNode = new TreeNode(); treeNode.setId(rs.getString("id")); treeNode.setText(rs.getString("text")); treeNode.setParentid(rs.getString("parentid")); Map<String, Object> attributes = new HashMap<String, Object>(); attributes.put("url",rs.getString("url")); treeNode.setAttributes(attributes); } rs.close(); pstmt.close(); dbConn.close(); } catch (Exception e) { e.printStackTrace(); } } return treeNode; }
private static List<TreeNode> queryTreeNode(String id){ List<TreeNode> arrList = new ArrayList<TreeNode>(); Connection dbConn = getDbConnection(); TreeNode treeNode = null; if(dbConn != null){ try { String strSQL = "select * from ttree where parentid="+id; PreparedStatement pstmt = null; ResultSet rs = null; pstmt = dbConn.prepareStatement(strSQL); rs = pstmt.executeQuery(); while (rs.next()) { treeNode = new TreeNode(); treeNode.setId(rs.getString("id")); treeNode.setText(rs.getString("text")); treeNode.setParentid(rs.getString("parentid")); Map<String, Object> attributes = new HashMap<String, Object>(); attributes.put("url",rs.getString("url")); treeNode.setAttributes(attributes); arrList.add(treeNode); } rs.close(); pstmt.close(); dbConn.close(); } catch (Exception e) { e.printStackTrace(); } } return arrList; }
3、测试调用(JSON.toJSONString方法是使用的fastjson.jar包中的方法功能很强大强烈推荐,可以在网上下载)
public static void main(String[] args) { TreeNode node = recursiveTree("1"); String jsonText = JSON.toJSONString(node, true); System.out.println(jsonText); }
4、输出结果
{ "attributes":{ "url":"1" }, "checked":false, "children":[{ "attributes":{ "url":"2" }, "checked":false, "children":[ { "attributes":{ "url":"3" }, "checked":false, "children":[], "id":"3", "parentid":"2", "text":"长沙" }, { "attributes":{ "url":"4" }, "checked":false, "children":[], "id":"4", "parentid":"2", "text":"株洲" }, { "attributes":{ "url":"5" }, "checked":false, "children":[], "id":"5", "parentid":"2", "text":"湘潭" }, { "attributes":{ "url":"6" }, "checked":false, "children":[ { "attributes":{ "url":"7" }, "checked":false, "children":[], "id":"7", "parentid":"6", "text":"岳阳县" }, { "attributes":{ "url":"http://192.168.1.1" }, "checked":false, "children":[], "id":"8", "parentid":"6", "text":"华容县" }, { "attributes":{ "url":"9" }, "checked":false, "children":[], "id":"9", "parentid":"6", "text":"湘阴县" } ], "id":"6", "parentid":"2", "text":"岳阳" } ], "id":"2", "parentid":"1", "text":"湖南" }], "id":"1", "parentid":"0", "text":"中国" }
写得很简单代码不是很规范,只是个测试demo。但功能很实用,希望对做web开发同学有些帮助。