数据库存取树形结构.docx
- 文档编号:25470851
- 上传时间:2023-06-09
- 格式:DOCX
- 页数:11
- 大小:16.94KB
数据库存取树形结构.docx
《数据库存取树形结构.docx》由会员分享,可在线阅读,更多相关《数据库存取树形结构.docx(11页珍藏版)》请在冰豆网上搜索。
数据库存取树形结构
数据库存取树形结构
(转自互联网)我们通常会在应用中碰到树形结构的内容,比如文件夹/文件模型,部门组织结构,目录树等等,通常在设计模式中叫做compose模式。
在数据库中常常这样表示:
我们以Catalog(分级目录)为例子
Catalog(分级目录)
字段名称
字段
类型
备注
目录IDcatalog_idvarchar(36)
pk,notnull
目录名称
catalog_namevarchar(50)
notnull
父目录IDparent_idvarchar(36)
fk
创建时间
create_datetimedatetimenotnull
目录描述
descriptionvarchar(200)
我们考虑在数据库中一次将所有数据读入内存,然后在内存中生成一个Tree,这样可以减少数据库的访问,增加性能,并且只有的数据方式改变的时候,全部重新从数据库中生成Tree,否则一直保持在内存中。
我们使用标准的DAO模式先生成POJO类(Catalog)和DAO类(CatalogDAO)。
然后我们建立相对通用的Tree和TreeNode类。
Tree.java001packagecom.humpic.helper.tree;
002importjava.util.*;
003importmons.lang.StringUtils;
004importmons.logging.Log;
005importmons.logging.LogFactory;
006publicabstractclassTree{
007protectedstaticLoglog=LogFactory.getLog(Tree.class);
008privateMaptreeNodeMaps=newHashtable();
009privateTreeNoderoot;
010013protectedvoidreload(Listnodes){
014log.info("treewillstartreloadalldata");
015016synchronized(this){
017//initialize018treeNodeMaps.clear();
019root=null;
020ListtreeNodes=newVector(nodes.size());
021for(inti=0;iTreeNodenode=this.transform(nodes.get(i));//transform022treeNodes.add(node);
023node.setTree(this);
024treeNodeMaps.put(node.getNodeId(),node);
025}
026027for(inti=0;i028TreeNodenode=(TreeNode)treeNodes.get(i);
029StringparentId=node.getParentId();
030if(this.isRootNode(node)){
031if(root==null){
032root=node;
033}else{
034log.error("findmorethenonerootnode.ignore.");
035}
036}else{
037TreeNodeparent=(TreeNode)treeNodeMaps.get(parentId);
038if(parent!
=null){
039parent.addChild(node);
040node.setParent(parent);
041}else{
042log.warn("node[id="+node.getNodeId()+"]:
missingparentnode.");
043}
044}
045}
046}
047if(root==null){
048log.error("therootnodeisnotbedefined");
049}
050}
051protectedbooleanisRootNode(TreeNodenode){
052returnStringUtils.isBlank(node.getParentId());
053}
054publicTreeNodegetRootNode(){
055returnroot;
056}
057publicTreeNodegetTreeNode(StringnodeId){
058return(TreeNode)treeNodeMaps.get(nodeId);
059}
060publicvoidaddTreeNode(TreeNodenode){
061synchronized(this){
062treeNodeMaps.put(node.getNodeId(),node);
063StringparentId=node.getParentId();
064if(StringUtils.isNotBlank(parentId)){
065TreeNodeparent=getTreeNode(parentId);
066if(parent!
=null){
067parent.addChild(node);
068node.setParent(parent);
069}else{
070log.error("parentcannotbefound:
"+node.getParentId());
071}
072}else{
073if(root==null){
074root=node;
075}else{
076log.error("findmorethenonerootnode.ignore.");
077}
078}
079}
080}
081publicvoiddeleteTreeNode(StringnodeId){
082synchronized(this){
083TreeNodenode=getTreeNode(nodeId);
084if(node==null)
085thrownewIllegalArgumentException(nodeId+"cannotbefound.");
086if(node.getParent()==null){
087root=null;
088treeNodeMaps.clear();
089log.warn("therootnodehasbeenremoved.");
090}else{
091node.getParent().getChildren().remove(node);
092treeNodeMaps.remove(nodeId);
093Listchildren=node.getAllChildren();
094for(inti=0;i095TreeNoden=(TreeNode)children.get(i);
096treeNodeMaps.remove(n.getNodeId());
097}
098}
099}
100}
101127protectedabstractTreeNodetransform(Objectinfo);
128}
129TreeNode.java130packagecom.humpic.helper.tree;
131importjava.util.List;
132importjava.util.Vector;
133importmons.collections.Predicate;
134importmons.lang.ObjectUtils;
135publicclassTreeNode{
136privateTreetree;
137privateTreeNodeparent;
138privateListchildren=newVector();
139privateListchildrenGroup=newVector();
140privateStringnodeId;
141privateStringparentId;
142privateObjectbindData;
143publicStringgetNodeId(){
144returnnodeId;
145}
146publicvoidsetNodeId(StringnodeId){
147this.nodeId=nodeId;
148}
149publicStringgetParentId(){
150returnparentId;
151}
152publicvoidsetParentId(StringparentId){
153this.parentId=parentId;
154}
155publicObjectgetBindData(){
156returnbindData;
157}
158publicvoidsetBindData(ObjectbindData){
159this.bindData=bindData;
160}
161publicTreegetTree(){
162returntree;
163}
164publicvoidsetTree(Treetree){
165this.tree=tree;
166}
167publicvoidsetParent(TreeNodeparent){
168this.parent=parent;
169}
170publicTreeNodegetParent(){
171returnthis.parent;
172}
173publicListgetChildren(){
174returnthis.children;
175}
176publicvoidaddChild(TreeNodenode){
177children.add(node);
178}
179182publicListgetAllChildren(){
183if(this.childrenGroup.isEmpty()){
184synchronized(this.tree){
185for(inti=0;iTreeNodenode=(TreeNode)this.children.get(i);
186this.childrenGroup.add(node);
187this.childrenGroup.addAll(node.getAllChildren());
188}
189}
190}
191returnthis.childrenGroup;
192}
193196publicListgetAllChildren(Predicatepredicate){
197Listgroups=newVector();
198fillAllChildren(groups,predicate);
199returngroups;
200}
201privatevoidfillAllChildren(Listgroups,Predicatepredicate){
202for(inti=0;i203TreeNodenode=(TreeNode)this.children.get(i);
204if(predicate.evaluate(node)){
205groups.add(node);
206node.fillAllChildren(groups,predicate);
207}
208}
209}
210213publicListgetParents(){
214Listresults=newVector();
215TreeNodeparent=this.getParent();
216while(parent!
=null){
217results.add(parent);
218parent=parent.getParent();
219}
220returnresults;
221}
222231publicbooleanisMyParent(StringnodeId){
232TreeNodetarget=tree.getTreeNode(nodeId);
233TreeNodeparent=this.getParent();
234if(parent==null){
235returntarget==null;
236}else{
237returnparent.equals(target);
238}
239}
240245publicbooleanisMyAncestor(StringnodeId){
246TreeNodetarget=tree.getTreeNode(nodeId);
247if(target==null)
248returntrue;
249returntarget.getAllChildren().contains(this);
250}
251256publicbooleanisMyBrother(StringnodeId){
257TreeNodetarget=tree.getTreeNode(nodeId);
258if(target==null)
259returnfalse;
260TreeNodep1=this.getParent();
261TreeNodep2=target.getParent();
262returnObjectUtils.equals(p1,p2);
263}
264}
然后建立业务TreeCatalogTree.javaviewsourceprint?
01packagecom.humpic.helper.tree;
02importjava.util.List;
03importmons.collections.Predicate;
04publicclassCatalogTreeextendsTree{
05privatestaticCatalogTreeinstance=null;
06privateCatalogTree(){}
0708publicstaticsynchronizedCatalogTreegetInstance(){
09if(instance==null){
10instance=newCatalogTree();
11instance.reloadCatalogs();
12}
13returninstance;
14}
15protectedTreeNodetransform(Objectinfo){
16Catalogcatalog=(Catalog)info;
17TreeNodenode=newTreeNode();
18node.setNodeId(catalog.getCatalogId());
19node.setParentId(catalog.getParentId());
20node.setBindData(catalog);
21returnnode;
22}
23publicvoidreloadCatalogs(){
24Listnodes=CatalogDAO.getInstance().findAll();
25super.reload(nodes);
26}
27publicCataloggetCatalogNode(StringcatalogId){
28TreeNodenode=super.getTreeNode(catalogId);
29returnnode==null?
null:
(Catalog)node.getBindData();
30}
31}
最后,我们只要使用以下的语句就可以了:
1.CatalogTree.getInstance().getTreeNode(.)
2.CatalogTree.getInstance().getCatalogNode(.)
3.CatalogTree.getInstance().getRootNode()
然后通过TreeNode,就可以得到parent,parents和children,allChildrenMSN空间完美搬家到新浪博客!
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 存取 树形 结构