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

境: Windows2000 + JBuilderX + Hibernate2.1.6 + Oracle9i
1.安装JBuilderX和Oracle9i,并使用以下信息配置Oracle
用户名:system
密码:admin
服务:cpdb

2.先去
http://prodownloads.sourceforge.net/hibernate/ 下载Hibernate API的包hibernate-2.1.6.zip,如下图所示


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



4.然后将截压缩后的hibernate-2.1/etc/hibernate.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的class/person目录下
(注意:数据库定义文件hibernate.properties是放在class目录下的,而表的配置文件要放在相对应的包下,我们这里的包名是person,所以就放在class/person下)

PersonModel.hbm.xml的内容如下:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "
http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd
" >
<hibernate-mapping>
  <class name="person.PersonModel"
         table="zl">
    <id name="id" type="long">
      <generator class="sequence">
        <param name="sequence">ZL_ID_SEQ</param>
      </generator>
    </id>

    <property name="name"/>
    <property name="address"/>
   
  </class>
</hibernate-mapping>

表名为zl,里面有两个字段(name和address,对应person.PersonModel),id是一个sequence.

总的目录结构如下图所示


JBuilder内部结构如下:


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




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


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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值