java树形结构 算法
最近看到一个有意思的树形结构,为每个节点添加了lft
和
rgt
两个属性。这样查找该节点的子节点、
查找该节点所有父节点,就不用去递归查询,只需要用
between
、
and
语句就可以实现。下面以创建一个栏目树为例,以下是我的理解。
一般来讲,我们创建栏目树的时候,大多只需要一个外键parentid
来区分该节点属于哪个父节点。数据库的设计如下图:
这样一来,
1.查找该节点的所有子节点,则需要采用
sql
的递归语句:
Sql代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=select%20*%20from%20tableName%20connect%20by%20prior%20id%3Dsj_parent_id%20start%20with%20%20id%3D1" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
select
*
from
tableName
connect
by
prior
id=sj_parent_id start
with
id=1
select * from tableName connect by prior id=sj_parent_id start with id=1
(
oracle 写法,
mysql
目前不支持,如果
mysql
想查找树形,可以利用存储过程
).
2.查找该节点的父节点
的
sql
递归语
句:
Sql代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=select%20*%20from%20tableName%20connect%20by%20prior%20sj_parent_id%20%3Did%20start%20with%20%20id%3D1" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15">
select
*
from
tableName
connect
by
prior
sj_parent_id =id start
with
id=1
select * from tableName c
相关文档:
Impl
public class BaseDAOImpl extends HibernateDaoSupport implements IBaseDAO
//添加数据
this.getHibernateTemplate().save(achi);
//删除
this.getHibernateTemplate().delete(this.getById(achi));
//查询所有
return this.getHibernateTemplate().find("from Achievement a ......
this
对象本身。public class ThisTest {
ThisTest tTest;
public ThisTest(){
tTest = this;
}
public void test(){
System.out.println(this);
}
public static void main(String arg[]){
new ThisTest().test();
}
}
成员方法引用。
成员变量引用。public class ThisTest {
String name ......
1.列举出 10个JAVA语言的优势
a:免费,开源,跨平台(平台独立性),简单易用,功能完善,面向对象,健壮性,多线程,结构中立,企业应用的成熟平台, 无线应用
2.列举出JAVA中10个面向对象编程的术语
a:包,类,接口,对象,属性,方法,构造器,继承,封装,多态,抽象,范型
3.列举出JAVA中6个比较常用的包
Java. ......
问题1.
public static void append(String str){
str += " Append!";
}
public static void append(StringBuffer sBuffer){
sBuffer.append(" Append!");
}
public void test(){
String str = "Nothing";
append(str);
System.out.println(str);
StringBuffer sBuffer = new StringBuffer("Nothing" ......
1. 如何得到Java应用程序的可用内存?
答:如下代码实现取得总的内存大小和可用内存大小,并打印到控制台上
public class MemoryExp {
public static void main(String[] args) {
System.out.println("Total Memory"+Runtime.getRuntime().totalMemory());
System.out.println("Free Memory ......