浏览 7221 次
|
该帖已经被评为精华帖
|
|
|---|---|
| 作者 | 正文 |
|
时间:2004-09-25
花了两个小时做了个spring+hibernate的树形结构,以下代码随手写的,还没来得及调试,欢迎各位提建议!
DAO代码: [code:1] package infoweb.admin.dao; import java.util.List; import java.util.Iterator; import infoweb.pojo.Board; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Query; import net.sf.hibernate.Session; import org.springframework.orm.hibernate.HibernateCallback; import org.springframework.orm.hibernate.support.HibernateDaoSupport; /** * <p>Title: 版块分类DAOImpl</p> * <p>Description: 用树型结构实现</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author 段洪杰 * @version 1.0 */ public class BoardTreeDAOImpl extends HibernateDaoSupport implements IBoardTreeDAO { /** * 构造函数 */ public BoardTreeDAOImpl() { super(); } /** * 取根叶 * @return List */ public List getRoots() throws HibernateException { String queryString = "select board as Board where board.parent_id='root' order by board.id desc"; List roots = getHibernateTemplate().find(queryString); return roots; } /** * 存根叶 * @param board Board */ public void setRoot(Board board) { board.setParentId("root"); getHibernateTemplate().save(board); } /** * 取子叶 * @param parentid String * @return List */ public Iterator getChildren(String parentid) { /* String queryString = "select board as Board where board.parent_id='parentid' order by board.id desc"; List children = getHibernateTemplate().find(queryString); return children; */ Board parent = (Board) getHibernateTemplate().load(Board.class, parentid); return parent.getChildren().iterator(); } /** * 取子叶数 * @param parentid String * @return int */ public int getChildsCount(String parentid) { /* String queryString = "select count(*) Board where board.parent_id='parentid' order by board.id desc"; List children = getHibernateTemplate().find(queryString); int count = ((Integer) children.iterator().next()).intValue(); return count; */ Board parent = (Board) getHibernateTemplate().load(Board.class, parentid); return parent.getChildren().size(); } /** * 存子叶 * @param parentLeaf Leaf */ public void setChild(Board board, String parentid) { board.setParentId(parentid); getHibernateTemplate().save(board); } /** * * 删除该叶和它的子叶 * @param board Board */ public void deleteBranch(Board board) { } /** * 根据子叶得到父叶 * @param child Board * @return Board */ public Board getParentByChild(Board child) { } /** * 通过子ID得到父叶 * @param id String * @return Board */ public Board getParentByChildId(String id) { } } [/code:1] Board代码 [code:1] package infoweb.pojo; import java.io.Serializable; import java.util.Date; import java.util.Set; import org.apache.commons.lang.builder.ToStringBuilder; /** @author Hibernate CodeGenerator */ public class Board implements Serializable { /** identifier field */ private String id; /** nullable persistent field */ private String parentId; /** nullable persistent field */ private String name; /** nullable persistent field */ private Date createDate; /** persistent field */ private Set infos; /** persistent field */ private Set children; /** full constructor */ public Board(String parentId, String name, Date createDate, Set infos, Set children) { this.parentId = parentId; this.name = name; this.createDate = createDate; this.infos = infos; this.children = children; } /** default constructor */ public Board() { } /** minimal constructor */ public Board(Set infos, Set children) { this.infos = infos; this.children = children; } public String getId() { return this.id; } public void setId(String id) { this.id = id; } public String getParentId() { return this.parentId; } public void setParentId(String parentId) { this.parentId = parentId; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return this.createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Set getInfos() { return this.infos; } public void setInfos(Set infos) { this.infos = infos; } public Set getChildren() { return this.children; } public void setChildren(Set children) { this.children = children; } public String toString() { return new ToStringBuilder(this) .append("id", getId()) .toString(); } } [/code:1] Board.hbm.xml代码 [code:1] <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <!-- Created by the Middlegen Hibernate plugin 2.1 http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/ --> <class name="infoweb.pojo.Board" table="board" > <id name="id" type="java.lang.String" column="id" > <generator class="uuid.hex" /> </id> <property name="parentId" type="java.lang.String" column="parent_id" length="50" /> <property name="name" type="java.lang.String" column="name" length="255" /> <property name="createDate" type="java.sql.Timestamp" column="createDate" length="19" /> <!-- Associations --> <!-- bi-directional one-to-many association to Info --> <set name="infos" lazy="true" inverse="true" cascade="none" > <key> <column name="board_id" /> </key> <one-to-many class="infoweb.pojo.Info" /> </set> <set cascade="save-update" inverse="true" lazy="true" name="children" > <key column="parent_id" /> <one-to-many class="infoweb.pojo.Board" /> </set> </class> </hibernate-mapping> [/code:1] 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2004-09-25
这个板块最热心最无私的莫过于dhj1。
呵呵 从他的文章学到了不少东西 |
|
| 返回顶楼 | |
|
时间:2004-09-26
今早又花了两个小时,把代码全部调试通过了,现在把测试代码贴在下面:
[code:1] package infoweb.admin.dao; import junit.framework.*; import infoweb.pojo.*; import java.util.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import net.sf.hibernate.HibernateException; /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author 段洪杰 * @version 1.0 */ public class TestBoardTreeDAOImpl extends TestCase { private IBoardTreeDAO boardTreeDAO = null; private ApplicationContext ac; protected void setUp() throws Exception { super.setUp(); ac = new FileSystemXmlApplicationContext("F:/jbproject/info_web/infoweb/WEB-INF/applicationContext-hibernate.xml"); boardTreeDAO =(BoardTreeDAOImpl) ac.getBean("boardTreeDAO"); } protected void tearDown() throws Exception { boardTreeDAO = null; super.tearDown(); } public void testgetBoardById() { Board board=boardTreeDAO.getBoardById("40288548ff334c2300ff334c27700003"); String actualReturn=board.getName(); String expectedReturn= "中国新闻"; assertEquals("return value", expectedReturn, actualReturn); } public void testDeleteBranch() { Board board= boardTreeDAO.getBoardById("40288548ff38b15e00ff38b163c00003"); boardTreeDAO.deleteBranch(board); } public void testGetChildren() { String parentid = "40288548ff334c2300ff334c27700003"; List actualReturn=new ArrayList(); List expectedReturn = new ArrayList(); expectedReturn.add("港台新闻"); expectedReturn.add("云南新闻"); Iterator it = boardTreeDAO.getChildren(parentid); while(it.hasNext()){ Board board=(Board)it.next(); actualReturn.add(board.getName()); } assertEquals("return value", expectedReturn, actualReturn); } public void testGetChildrenCount() { String parentid = "40288548ff334c2300ff334c27700003"; int expectedReturn = 2; int actualReturn = boardTreeDAO.getChildrenCount(parentid); assertEquals("return value", expectedReturn, actualReturn); } public void testGetParentByChild() { Board board=boardTreeDAO.getBoardById("40288548ff38b26900ff38b26b8b0001"); String expectedReturn = "ROOT789"; String actualReturn = boardTreeDAO.getParentByChild(board).getName(); assertEquals("return value", expectedReturn, actualReturn); } public void testGetParentByChildId() { String id = "40288548ff38b26900ff38b26b8b0001"; String expectedReturn = "ROOT789"; Board actualReturn =(Board) boardTreeDAO.getParentByChildId(id); assertEquals("return value", expectedReturn, actualReturn.getName()); } public void testGetRoots() throws HibernateException{ List expectedReturn =new ArrayList(); List actualReturn=new ArrayList(); expectedReturn.add("中国新闻"); expectedReturn.add("国际新闻"); expectedReturn.add("我是一个人111"); Iterator roots = boardTreeDAO.getRoots(); while(roots.hasNext()){ Board board=(Board)roots.next(); actualReturn.add(board.getName()); } assertEquals("ROOTS版块值", expectedReturn, actualReturn); } public void testSetChild() { Board board = new Board(); board.setName("r00t123-1-111"); String parentid = "40288548ff38b15e00ff38b163c00003"; boardTreeDAO.setChild(board, parentid); } public void testSetRoot() { Board board = new Board(); board.setName("ROOT789"); boardTreeDAO.setRoot(board); } } [/code:1] |
|
| 返回顶楼 | |
|
时间:2004-10-12
我的做法:
1.加入level(层次),levelcode(层次码),orderno(层内顺序号),以提供快速的子父关系查找,这几个参数的计算在一个facade bean 提供. 2.提供取某一节点子树的方法 3.提供先根和后根遍历 |
|
| 返回顶楼 | |







