ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 手把手配置Hibernate环境(JBuilderX版)(转)

手把手配置Hibernate环境(JBuilderX版)(转)

原创 Linux操作系统 作者:jcszjswkzhou 时间:2019-07-08 21:42:05 0 删除 编辑
环境: Windows2000 + JBuilderX + Hibernate2.1.6 + Oracle9i
1.安装JBuilderX和Oracle9i,并使用以下信息配置Oracle
用户名:system
密码:admin
服务:cpdb

2.先去
下载Hibernate API的包hibernate-2.1.6.zip,如下图所示


3.在JBuilder中选择Project菜单中的Project Properties,在Require Library中加入Hibernate所有的API包,还要加入Oracle目录ORANTjdbclibclasses12.zip



4.然后将截压缩后的hibernate-2.1etchibernate.properties拷贝到本地project的classes目录下然后做以下变化:
①注释默认的数据库连接,将
## HypersonicSQL

hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:hsql://localhost
hibernate.connection.url jdbc:hsqldb:test
hibernate.connection.url jdbc:hsqldb:.

改为
## HypersonicSQL

#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class org.hsqldb.jdbcDriver
#hibernate.connection.username sa
#hibernate.connection.password
#hibernate.connection.url jdbc:hsqldb:hsql://localhost
#hibernate.connection.url jdbc:hsqldb:test
#hibernate.connection.url jdbc:hsqldb:.

然后修改Oracle的连接,将
## Oracle

#hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
#hibernate.dialect net.sf.hibernate.dialect.OracleDialect
#hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
#hibernate.connection.username ora
#hibernate.connection.password ora
#hibernate.connection.url jdbc:oracle:thin:@localhost:1521:test

改为
## Oracle

hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
hibernate.dialect net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username system
hibernate.connection.password admin
hibernate.connection.url jdbc:oracle:thin:@172.28.122.49:1521:cpdb

注意:@172.28.122.49为Oracle服务器的ip地址,1521为端口号

5.在project下建立package person,在package下建立四个class,分别为:
①PersonModel.java

package person;

import java.io.Serializable;

public class PersonModel implements Serializable {
private Long id;
private String name;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}

}
②TestPersonModel1.java
package person;

import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

public class TestPersonModel1 {
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception{
System.out.println("start");
Configuration conf= new Configuration().addClass(PersonModel.class);

SchemaExport dbExport=new SchemaExport(conf);
dbExport.setOutputFile("sql_out_lib\sql.txt");
dbExport.create(true, true);
}
}

③TestPersonModel2.java
package person;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class TestPersonModel2 {
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception{
Configuration conf= new Configuration().addClass(PersonModel.class);

//在表中插入第一条数据

sessionFactory = conf.buildSessionFactory();
Session s = sessionFactory.openSession();

Transaction t = s.beginTransaction();

PersonModel p1=new PersonModel();
p1.setName("zhaol");
p1.setAddress("shanghai");

//持久化
s.save(p1);
//数据库中已有记录
t.commit();
s.close();
}
}

④TestPersonModel3.java
package person;

import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.Query;

public class TestPersonModel3 {
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception{
Configuration conf= new Configuration().addClass(PersonModel.class);

sessionFactory = conf.buildSessionFactory();
Session s = sessionFactory.openSession();
PersonModel p = new PersonModel();
Query q = s.createQuery("from PersonModel as p where p.id=1");
p = (PersonModel) q.list().get(0);
System.out.println(p.getName());
s.close();
}
}

其中PersonModel.java是对应数据库中字段的存储结构,TestPersonModel1.java是测试自动创建表和字段,TestPersonModel2.java是测试在表中插入记录,TestPersonModel3.java是测试从表中取得记录.

6.编译所有的java文件

7.好了,到最后一步了,也是最关键的一步,我们已经定义了数据库连接(hibernate.properties)
创建了字段的存储结构(PersonModel.java),也写好了操作数据库的代码(TestPersonModel1.java~TestPersonModel3.java),接下来要做的是定义数据库中表的配置文件,因为我们的字段的存储结构文件是PersonModel.java,所以创建PersonModel.hbm.xml(注意:表的配置文件名是由[字段的存储结构文件名+.hbm.xml]构成,创建位置为project的classperson目录下
(注意:数据库定义文件hibernate.properties是放在class目录下的,而表的配置文件要放在相对应的包下,我们这里的包名是person,所以就放在classperson下)
PersonModel.hbm.xml的内容如下:

" >

table="zl">


ZL_ID_SEQ





表名为zl,里面有两个字段(name和address,对应person.PersonModel),id是一个sequence.
总的目录结构如下图所示


JBuilder内部结构如下:


9.好了,配置完成,接下来运行一下吧,先运行TestPersonModel1.class,让hibernate为我们自动建立一张名为zl(内有字段name和address)的表,运行完成后的JB显示以及数据库的变化如下:




怎么样?表是不是已经建好了,呵呵.
接下来运行TestPersonModel2.class,让hibernate再为我们插入一条记录,运行结果如下:


hibernate环境的配置就介绍到这里


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-122422/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论