1.将hibernate3.jar放到lib中
2.在src目录下创建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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- 数据库URL -->
<property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.1.1:1521:test</property>
<!-- 数据库用户名 -->
<property name="hibernate.connection.username">test</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password">test</property>
<!-- 指定连接池大小 -->
<property name="connection.pool_size">5</property>
<!-- 数据库类型 -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- 是否显示SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 事务配置 -->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- 罗列HBM映射文件 -->
<mapping resource="com/test/ticket/vo/Testvo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.引入相关jar包
dom4j-1.6.1.jar 解析xml
commons-collections.jar
classes12.jar oracle数据库驱动
cglib-nodep-2.2.jar
jta.jar
4.创建VO和hbm映射文件
<?xml version="1.0"?>
<!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.test.ticket.vo.TestVO" table="test_stu">
<id name="Id" type="int">
<column name="id" precision="10" scale="0" />
<generator class="sequence" >
<param name="sequence">test_stu_id_seq</param>
</generator>
</id>
<property name="name" type="string">
<column name="name"/>
</property>
<property name="dept" type="string">
<column name="dept"/>
</property>
</class>
</hibernate-mapping>
5.编写dao类
public class TestDao {
Configuration config;
SessionFactory sessionFactory;
Session session;
public void save(TestVO testvo){
//实例化config
config = new Configuration().configure();
//实例化SessionFactory
sessionFactory = config.buildSessionFactory();
//实例化Session
session = sessionFactory.openSession();
//开始事务
Transaction tx = session.beginTransaction();
//插入操作
session.save(testvo);
//提交事务
tx.commit();
//关闭session
session.close();
}
}