Hibernate 的配置与简单应用

Hibernate 的配置与简单应用

 

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

 

一、配置

1.  下载网址:www.hibernate.org (我下载的是hibernate-3.2)

2.  解压,把在hibernate-3.2目录下的hibernate3.jarhibernate-3.2/lib目录下的所有文件拷贝到自己创建的一个文件夹下(我的是创建在E:/JBuilder2/hibernate lib

3.  启动JBuilder 2006,打开菜单Tools/Configure/Libraries,按左下角的new按钮,设置NameHibernate 3.0,且Add2步创建的hibernate lib文件夹。

4.  配置完成。

 

二、简单应用

1.  新建一个项目testHibernate

2.  打开菜单Project/Project Properties,在PathRequired LibrariesAdd3步创建的Hibernate 3.0

3.  打开菜单Project/Project Properties,在BuildResource下的xml设置为Copy。(这步很重要啊,如果不设置这一项的话,项目运行时会找不到**.hbm.xml)。

4.  hibernate源文件夹下的etc目录(即目录E:/hibernate-3.2/etc)下的两个文件hibernate.propertieslog4j.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

 

              这一步主要是配置hibernateMySQL数据库的连接。

5.  new 一个javabean,类名为PersonBean ,添加两个属性usernamepassword;

6.  New 一个空xml,名字为PersonBean-hbm.xml (注意:这个名字不是随便命名的,它必须是这种规范,表示类PersonBeanHibernate的对象-关系映射)。它的内容为:

<?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对应J2EEjavabean,这就是有名的对象-关系映射,通过这种方式,我们就对数据库的操作和系统实现隔离,在系统实现里没有一句SQL语句。这就是伟大的Hibernate!!!

 

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

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值