分类: Java
2009-09-04 16:08:04
|
1:编写以下文件
--------------------------------------------------------------------
hibernate.cfg.xml 配置文件,指定oracle实例 驱动 用户名 密码等
hello
|--Dept.java DEPT表对应的类文件
|--HelloWorld.java 主程序
|--HelloWorld.hbm.xml 映射文件
Dept.java
--------------------------------------------------------------------
package hello;
public class Dept {
private Long deptno;
private String dname;
private String loc;
public Dept(){}
public Long getDeptno() {
return deptno;
}
private void setDeptno(Long deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
HelloWorld.java
--------------------------------------------------------------------
package hello;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HelloWorld
{
public static void main(String args[])
{
Configuration config = new Configuration().configure();
SessionFactory sessions = config.buildSessionFactory();
Session session = sessions.openSession();
Transaction transaction = session.beginTransaction();
// 这里的Dept是映射文件中的name属性,而不是table属性dept(会报错)
List depts = session.createQuery("from Dept").list();
System.out.println(depts.size() + "dept(s) found");
System.out.println("deptno dname loc");
for(Iterator iter = depts.iterator(); iter.hasNext();) {
Dept dept = (Dept)iter.next();
System.out.println(dept.getDeptno() + dept.getDname() + dept.getLoc());
}
transaction.commit();
session.close();
sessions.close();
}
}
HelloWorld.hbm.xml
--------------------------------------------------------------------
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"">
hibernate.cfg.xml
--------------------------------------------------------------------
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"">
2:设置CLASSPATH环境变量
-------------------------------------------------------------------
set CLASSPATH=%CLASSPATH%;C:\software\oracle\jdbc\lib\ojdbc14.jar;F:\开源\代码\hibernate3.3.2\hibernate3.jar;F:\开源\代码\hibernate3.3.2\lib\required\antlr-2.7.6.jar;F:\开源\代码\hibernate3.3.2\lib\required\commons-collections-3.1.jar;F:\开源\代码\hibernate3.3.2\lib\required\dom4j-1.6.1.jar;F:\开源\代码\hibernate3.3.2\lib\required\javassist-3.9.0.GA.jar;F:\开源\代码\hibernate3.3.2\lib\required\jta-1.1.jar;F:\开源\代码\hibernate3.3.2\lib\required\slf4j-api-1.5.8.jar;F:\开源\代码\hibernate3.3.2\lib\required\slf4j-nop-1.5.2.jar
增加了以下三种jar文件
a) hibernate安装目录下lib\required下的所有jar文件
b) oracle的jdbc jar包
c) slf4j-nop-1.5.2.jar 这个安装包中没有,但是必须否则报错
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
3:编译
-------------------------------------------------------------------
javac hello/Dept.java
javac hello/HelloWorld.java
4:运行
-------------------------------------------------------------------
java hello.HelloWorld
输出
4dept(s) found
deptno dname loc
10ACCOUNTINGNEW YORK
20RESEARCHDALLAS
30SALESCHICAGO
40OPERATIONSBOSTON