hibernate集成

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

}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值