1. 新建一个项目,导入hibernate相关的jar包。
2. 开发持久化类Department:
package com.huey.entity;
/**
* Department实体,普通的JavaBean
* @version 2013-08-23
* @author huey2672
*
*/
public class Department {
private int deptId;
private String deptName;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Department() {
}
public Department(String deptName) {
setDeptName(deptName);
}
public Department(int deptId, String deptName) {
this(deptName);
setDeptId(deptId);
}
}
3. 创建数据库表tab_dept和序列dept_id_sequence:
create table tab_dept(
dept_id number(8) primary key,
dept_name varchar2(20) not null.
);
create sequence dept_id_sequence
start with 1001
increment by 1;
4. 配置映射文件Department.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
映射文件的根元素
package属性指定一个前缀包 ,映射文件中没有指定全限定的类名,则默认使用该包前缀
-->
<hibernate-mapping package="com.huey.entity">
<!--
每个class元素对应一个持久化类,
name属性指定持久化类映射的持久化类的类名
table属性指定持久化类映射的表名
-->
<class name="Department" table="tab_dept">
<!-- 映射主键 -->
<id name="deptId" column="dept_id">
<!-- 主键生成方式,序列 -->
<generator class="sequence">
<param name="sequence">DEPT_ID_SEQUENCE</param>
</generator>
</id>
<!-- 映射普通属性 -->
<property name="deptName" column="dept_name" />
</class>
</hibernate-mapping>
5. 配置配置文件hibernate.cfg.xml:
<?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>
<!-- 设置所需连接数据库服务的URL -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 设置连接数据库的用户名 -->
<property name="connection.username">oa</property>
<!-- 设置连接数据库的密码 -->
<property name="connection.password">oa</property>
<!-- 设置数据库连接池的最大并发连接数 -->
<property name="connection.pool_size">1</property>
<!-- 设置数据库方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 指定显示SQL语句 -->
<property name="show_sql">true</property>
<!-- 指定根据需要自动创建数据表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 罗列所有的映射文件 -->
<mapping resource="com/huey/entity/mapping/Department.hbm.xml" />
</session-factory>
</hibernate-configuration>
6. 测试用例:
package com.huey.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.huey.entity.Department;
/**
* @version 2013-08-23
* @author huey2672
*
*/
public class DepartmentTest {
/**
* 测试用例
* @throws Exception
*/
@Test
public void testAddDepartment() throws Exception {
// 实例化Configuration对象,并默认加载hibernate.cfg.xml配置文件
Configuration configuration = new Configuration().configure();
// 创建SessionFactory对象
SessionFactory sf = configuration.buildSessionFactory();
// 创建Session
Session session = sf.openSession();
// 开始事务
Transaction transaction = session.beginTransaction();
// 实例化Department对象
Department department = new Department("HumanResource");
// 持久化保存department实例
session.save(department);
// 事务提交
transaction.commit();
// 关闭Session
session.close();
// 关闭SessionFactory
sf.close();
}
}