Hibernate的简单示例

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();
	}	
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值