作为开源框架,Spring很好的支持了hibernate,通过Spring的声明式事务很好解决了,hiberante事务问题、、、
一、用eclipse开发引入spring+hibernate jar包,利用spring配置管理hibernate.
二、编写 log4j.properties文件
#错误的级别,存放器分别控制台与文件
log4j.rootLogger =error,A,R
#A可随便写,输出控制台,自己指定布局格式
log4j.appender.A = org.apache.log4j.ConsoleAppender
log4j.appender.A.layout = org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern = [%t] %-5p %c %x - %m%n
#R可随便写,输出在文件,文件位置/立即刷新/最大改寸/备份个数/布局格式
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=c:\logger.log
log4j.appender.R.ImmediateFlush=true
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} [%t] %-5p %-30c{3} %x - %m %n
三、配置 ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<property name="username">
<value>terry</value>
</property>
<property name="password">
<value>terry</value>
</property>
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- 处理HQL语句中文参数乱码问题 -->
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/terry/hibernate/pojo/Stu.hbm.xml</value></list>
</property>
</bean>
<!--
配置事务管理
-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"></property>
</bean>
<!-- 配置事务拦截器 -->
<bean id="transactionIterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器需要注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="find*,get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionIterceptor</value>
</list>
</property>
</bean>
<!-- 后台实现类 后台管理Bean -->
<bean class="org.terry.hibernate.base.imp.DaoBeanImp" name="beanImp">
<property name="sessionFactory" ref="SessionFactory"></property>
</bean>
<!-- 自定义模板,让所有实现类都继承模板-->
<bean id="daoTemplate" abstract="true" lazy-init="true">
<property name="sessionFactory" ref="SessionFactory"></property>
</bean>
<bean class="org.terry.hibernate.dao.StuDao" name="stu" parent="daoTemplate">
</bean>
</beans>
四、编写具体实现类
1.继承 HibernateDaoSupport
2.使用 HibernateTemplate 模板
五、测试
通过ClassPathXmlApplicationContext 得到 ApplicationContext 对象,通过其 getBeans("")得到具体实现类