通过使用Hibernate的使用,可以很方便的使用数据库,不用自己再编写繁琐的SQL语言,可以非常方便的将类映射到数据库中。使用的时候只需在程序中定义类,建立映射文件,然后在ORACLE中建立数据库,再运行程序,程序就会自动在程序中建立各个数据库表。
1、假设已经知道数据库配置参数:
用户名:PERSONNEL_MANAGE
密码:MWQ
数据库实例:XE
侦听端口:1521
数据库驱动名:oracle.jdbc.driver.OracleDriver
connection.url:jdbc:oracle:thin:@127.0.0.1:1521:XE
2‘、(1)建立eclipse工程,
(2)然后从www.hibernate.org网站下载Hibernate发布包(如hibernate.jar),把相关jar文件添加到工程中。
(3)从ORACLE网站下载ORACLE的JDBC驱动程序jar包,把相关jar文件也添加到工程中。
3、建立持久化类
package com.mwq.hibernate.mapping;
import java.util.HashSet;
import java.util.Set;
public class TB_STAFFM implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private String tel;
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getTel(){
return this.tel;
}
public void setTel(String tel){
this.tel=tel;
}
}
4、编写反映持久化类与数据库表映射关系的Hibernate映射文件TB_STAFFM.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mwq.hibernate.mapping.TB_STAFFM"
table="tb_staffmm">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true" />
</property>
<property name="tel" type="java.lang.String">
<column name="tel" length="100" not-null="false" />
</property>
</class>
</hibernate-mapping>
5、编写Hibernate配置文件hibernate.cfg.xml,并在配置文件中通过mapping元素加入持久化类与数据库表映射的信息,即通过resource指定上一步建立的映射文件的位置。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class"><!-- 配置数据库的驱动类 -->
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url"><!-- 配置数据库的连接路径 -->jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
<property name="connection.username">PERSONNEL_MANAGE</property><!-- 配置数据库的连接用户名 -->
<property name="connection.password">MWQ</property><!-- 配置数据库的连接密码,这里密码为空,在这种情况下也可以省略该行配置代码 -->
<property name="dialect"><!-- 配置数据库使用的方言 -->
org.hibernate.dialect.OracleDialect
</property>
<property name="hbm2ddl.auto">update</property>
<property name="use_sql_comments">true</property><!-- 配置在输出的SQL语句前面添加提示信息 -->
<mapping
resource="com/mwq/hibernate/mapping/TB_STAFFM.hbm.xml" />
</session-factory>
</hibernate-configuration>
6、编写HibernateSessionFactory类,使用ThreadLocal控制Session
package com.mwq.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static SessionFactory sessionFactory;
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
System.out.println("------在加载Hibernate配置文件时抛出异常,内容如下:");
e.printStackTrace();
}
}
public static Session getSession() {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null || session.isOpen()) {
session.close();
}
}
}
7、经过上述过程,就可完成Hibernate的配置了,接下来可以编写程序操作数据库
package com.db;
import java.util.Calendar;
import java.util.Date;
import oracle.sql.DATE;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mwq.hibernate.HibernateSessionFactory;
import com.mwq.hibernate.mapping.TB_STAFFM;
public class Main {
public static void main(String [] args)
{
// Get session
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try
{
// Begin transaction
tx = session.beginTransaction();
// Create a Product object and set its property
TB_STAFFM product = new TB_STAFFM();
product.setId(100);
product.setName("xxxx");
product.setTel("159900000000");
System.out.println(product.getName());
// Save the object
session.save(product);
// Commin
tx.commit();
}
catch (Exception e)
{
if (tx != null)
{
tx.rollback();
}
try
{
// Spread the exception
throw e;
}
catch (Exception e2)
{
e2.printStackTrace();
}
}
finally
{
// Close the session
session.close();
}
}
}