定义一个树的接口,继承了一个可被用持久层的接口 public interface ITree : IPersistenable { string Id { get;set;} string Name { get;set;} string ParentId { get;set;} ITree CloneTree(); } 属性结构菜单的实体类如下: public class CustomMenu : PersistenceProxy, ITree { public CustomMenu() { this.PersistenceProperty.TableName = "t_ui_custom_menu"; } private string _id, _name, _parentId; private string _url, _target; private string _img; private int _sequence; private string _roleList, _userList; private string _isAddToTab, _isHaveNavigation, _isLevelThree; private string _isShowNavigation; [Column(true, false, true)] public string Id { get { return _id; } set { _id = value; } } [Column(false, true, true)] public string Name { get { return _name; } set { _name = value; } } [Column(false, true, true)] public string ParentId { get { return _parentId; } set { _parentId = value; } } [Column(false, true, true)] public string Url { get { return _url; } set { _url = value; } } [Column(false, true, true)] public string Target { get { return _target; } set { _target = value; } } [Column(false, true, true)] public string Img { get { return _img; } set { _img = value; } } [Column(false, true, true)] public int Sequence { get { return _sequence; } set { _sequence = value; } } /// <summary> /// “1”为true null为false /// </summary> [Column(false, true, true)] public string IsAddToTab { get { return _isAddToTab; } set { _isAddToTab = value; } } /// <summary> /// 是否有导航栏 "1"为true "0"为false /// 目前有导航栏的只有流程 /// </summary> [Column(false, true, true)] public string IsHaveNavigation { get { return _isHaveNavigation; } set { _isHaveNavigation = value; } } /// <summary> /// 是否是三级菜单 "1"为true "0"为false /// 目前只有待处理的流程,我发起的流程等等 /// </summary> [Column(false, true, true)] public string IsLevelThree { get { return _isLevelThree; } set { _isLevelThree = value; } } /// <summary> /// 是否显示导航栏位置 "1"为true "0"为false /// 目前只有待处理的流程,我的项目 /// </summary> [Column(false, true, true)] public string IsShowNavigation { get { return _isShowNavigation; } set { _isShowNavigation = value; } } [Column(false, true, true)] public string RoleList { get { return _roleList; } set { _roleList = value; } } [Column(false, true, true)] public string UserList { get { return _userList; } set { _userList = value; } } public string[] Role { get { if (String.IsNullOrEmpty(RoleList)) return new string[0]; return RoleList.Split(','); } } public string[] User { get { if (String.IsNullOrEmpty(UserList)) return new string[0]; return UserList.Split(','); } } public ITree CloneTree() { CustomMenu menu = new CustomMenu(); menu.Id = this.Id; menu.Name = this.Name; menu.ParentId = this.ParentId; menu.Url = this.Url; menu.Target = this.Target; menu.Img = this.Img; menu.Sequence = this.Sequence; menu.UserList = this.UserList; menu.RoleList = this.RoleList; menu.IsAddToTab = this.IsAddToTab; menu.IsHaveNavigation = this.IsHaveNavigation; menu.IsShowNavigation = this.IsShowNavigation; menu.IsLevelThree = this.IsLevelThree; return menu; } public CustomMenu Clone() { return (CustomMenu)this.CloneTree(); } } 构建树和对数据库进行操作的两个类,分别如下: namespace MBO.WebHelper.Tools { public delegate TreeNode CreateNodeHandler(ITree tree); /// <summary> /// 构建树的类 /// </summary> public class BuildTree { private string _rootParentID; private TreeView _tree; private Type _type; public BuildTree(string rootParentID, TreeView tree, Type type) { this._rootParentID = rootParentID; this._tree = tree; this._type = type; } public CreateNodeHandler CreateNodes; //public string RootId //{ // get { return _rootId; } // set { _rootId = value; } //} /// <summary> /// 创建当前码表的目录树 /// </summary> /// <param name="tree"></param> /// <returns></returns> public void Build() { _tree.Nodes.Clear(); ITree tree = (ITree)Activator.CreateInstance(_type); TreeStorage storage = new TreeStorage(); PersistenceList pList = storage.LoadRootList(tree, _rootParentID); for (pList.First(); !pList.EndOf(); pList.Next()) { ITree curCodeBase = pList.GetCurrentObject() as ITree; string tableName = curCodeBase.PersistenceProperty.TableName; TreeNode node = CreateNode(curCodeBase); if (storage.IsHaveChild(curCodeBase)) PopulateSubNode(curCodeBase, node); _tree.Nodes.Add(node); //nodeList.Add(node); } _tree.ExpandAll(); // return treeView; //return nodeList; } /// <summary> /// 递归循环创建树的节点 /// </summary> /// <param name="tree"></param> /// <param name="node"></param> private void PopulateSubNode(ITree tree, TreeNode node) { TreeStorage handleTree = new TreeStorage(); PersistenceList pList = handleTree.LoadChild(tree); for (pList.First(); !pList.EndOf(); pList.Next()) { ITree curtree = pList.GetCurrentObject() as ITree; string tableName = curtree.PersistenceProperty.TableName; TreeNode newNode = CreateNode(curtree); newNode.CollapseAll(); if (handleTree.IsHaveChild(curtree)) PopulateSubNode(curtree, newNode); node.ChildNodes.Add(newNode); } } /// <summary> /// 创建节点 /// </summary> /// <param name="text"></param> /// <param name="id"></param> /// <returns></returns> private TreeNode CreateNode(ITree tree) { if (CreateNodes == null) { TreeNode node = new TreeNode(); node.Text = tree.Name; node.Value = tree.Id; return node; } else return CreateNodes(tree); } } /// <summary> /// 操作树的类 /// </summary> public class TreeStorage : Rule { public bool IsHaveData(ITree tree) { string select = " select * "; PersistenceList plist = QuerySingleObject(select, "", tree); if (plist.Count == 0) return false; else return true; } /// <summary> /// 加载根节点 /// </summary> /// <param name="tree"></param> /// <returns></returns> public PersistenceList LoadRootList(ITree tree, string rootParentID) { //tree.ParentId = StartNode; //tree.PersistenceProperty.IsOnlyQueryStringField = true; //return rule.Query(tree); string select = " select * "; string where = " ParentId ='" + rootParentID + "'"; return QuerySingleObject(select, where, tree); } /// <summary> /// 获取当前对象的子对象列表 /// </summary> /// <param name="tree"></param> /// <returns></returns> public PersistenceList LoadChild(ITree tree) { //ITree curTree = (ITree)Activator.CreateInstance(tree.GetType()); //curTree.ParentId = tree.Id; //return Query(curTree); string select = " select * "; string where = " ParentId ='" + tree.Id + "'"; return QuerySingleObject(select, where, tree); } /// <summary> /// 检查当前对象是否存在子对象 /// </summary> /// <param name="tree"></param> /// <returns></returns> public bool IsHaveChild(ITree tree) { PersistenceList pList = LoadChild(tree); if (pList.Count == 0) return false; else return true; } /// <summary> /// 添加树节点 /// </summary> /// <param name="tree"></param> public void InsertTree(ITree tree) { this.PrepareTransaction(); this.Insert(tree); this.Commit(); } /// <summary> /// 删除节点 /// </summary> /// <param name="tree"></param> public void DeleteTree(ITree tree) { this.PrepareTransaction(); this.Delete(tree); this.Commit(); } public ITree GetTreeNode(string id, ITree tree) { string select = " select * "; string where = " Id ='" + id + "'"; PersistenceList pList = QuerySingleObject(select, where, tree); if (pList.Count == 0) return null; else return (ITree)pList.GetCurrentObject(); } } }