您的位置:首页 > 数据库 > SQL

Hibernate 的配置与简单应用

2007-06-12 00:25 337 查看
Hibernate 的配置与简单应用

 

Hibernate is a lightweight object-relations mapping service for Java.

 

一、配置
1.  下载网址:www.hibernate.org (我下载的是hibernate-3.2)
2.  解压,把在hibernate-3.2目录下的hibernate3.jar和hibernate-3.2/lib目录下的所有文件拷贝到自己创建的一个文件夹下(我的是创建在E:/JBuilder2/hibernate lib)
3.  启动JBuilder 2006,打开菜单Tools/Configure/Libraries,按左下角的new按钮,设置Name为Hibernate 3.0,且Add第2步创建的hibernate lib文件夹。
4.  配置完成。
 

二、简单应用
1.  新建一个项目testHibernate;
2.  打开菜单Project/Project Properties,在Path的Required Libraries下Add第3步创建的Hibernate 3.0。
3.  打开菜单Project/Project Properties,在Build的Resource下的xml设置为Copy。(这步很重要啊,如果不设置这一项的话,项目运行时会找不到**.hbm.xml)。<
4000
/span>
4.  在hibernate源文件夹下的etc目录(即目录E:/hibernate-3.2/etc)下的两个文件hibernate.properties和log4j.properties添加到项目的<Project Source>下。打开hibernate.properties,找到:
## HypersonicSQL
 

hibernate.dialect org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:./build/db/hsqldb/hibernate
hibernate.connection.url jdbc:hsqldb:hsql://localhost
hibernate.connection.url jdbc:hsqldb:test
              把它修改成:
## HypersonicSQL
 

#hibernate.dialect org.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class org.hsqldb.jdbcDriver
#hibernate.connection.username sa
#hibernate.connection.password
#hibernate.connection.url jdbc:hsqldb:./build/db/hsqldb/hibernate
#hibernate.connection.url jdbc:hsqldb:hsql://localhost
#hibernate.connection.url jdbc:hsqldb:test
             
              然后找到MySQL的配置信息,把它修改成:
## MySQL
 

hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost/test
hibernate.connection.username root
hibernate.connection.password 123456
 

              这一步主要是配置hibernate和MySQL数据库的连接。
5.  new 一个javabean,类名为PersonBean ,添加两个属性username和password;
6.  New 一个空xml,名字为PersonBean-hbm.xml (注意:这个名字不是随便命名的,它必须是这种规范,表示类PersonBean和Hibernate的对象-关系映射)。它的内容为:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
  <class name="com.study.PersonBean">
 

    <id name = "username" unsaved-value = "null">
      <generator class="uuid.hex"/>
    </id>
    <property name="password"/>
  </class>
</hibernate-mapping>
 

这个文件放在和PersonBean类相同的目录下;
7.  New 一个java类,名字为test,其功能为create一个表PersionBean(username,password)到MySQL数据库里面,其内容为:
package com.study;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
 *本类只是用来创建表的,并不往表内部插入任何数据,并且只能使用一次,否则会删除已有的表的
*/
public class Test
{
  private static SessionFactory sessionFactory;
 

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration().addClass(PersonBean.class);
    //在mysql中创建PersonBean(uesrname,password)的表
    //第一次运行时用来在数据库中创建表
    //并且把sql语句输出到txt文件用的
    //以后的运行不能使用该段代码,否则每次都会先删除原表,再新建该表
    SchemaExport dbExport = new SchemaExport(conf);
    dbExport.setOutputFile("sql.txt");
    dbExport.create(true, true);
 

  }
}
8.  Run Test.java,打开MySQL,可以看到新创建了PersonBean的一个表。
9.  心得:Hibernate的功能:把数据库的table对应J2EE的javabean,这就是有名的对象-关系映射,通过这种方式,我们就对数据库的操作和系统实现隔离,在系统实现里没有一句SQL语句。这就是伟大的Hibernate!!!
 

图1. 这是项目testHibenate的所有文件

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息