package cn.itcast.spring.hibernate;
import java.util.List;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import cn.itcast.spring.domain.Customer;
public class CustomerServiceImpl implements CustomerService {
//dao
private CustomerDao dao ;
//事务模板,封装事务管理的样板代码
private TransactionTemplate tt ;
//注入事务模板
public void setTt(TransactionTemplate tt) {
this.tt = tt;
}
//注入dao
public void setDao(CustomerDao dao) {
this.dao = dao;
}
public List<Customer> findCustomerByName(String name) {
return dao.findCustomerByName(name);
}
public void insertCustomer(Customer c) {
dao.insertCustomer(c);
}
/**
* 批量保存, 编程式事务管理
*/
public void saveCustomers(final List<Customer> list) {
// for(Customer c : list){
// dao.insertCustomer(c);
// }
tt.execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
try {
for(Customer c : list){
dao.insertCustomer(c);
}
} catch (RuntimeException e) {
e.printStackTrace();
//设置回滚
status.setRollbackOnly();
}
return null;
}});
}
public void updateCustomer(Customer c) {
dao.updateCustomer(c);
}
}
声明式:
package cn.itcast.spring.hibernate;
import java.util.List;
import org.springframework.aop.framework.AopContext;
import org.springframework.aop.support.AopUtils;
import cn.itcast.spring.domain.Customer;
/**
* 声明式事务管理,通过aop框架实现
*/
public class CustomerServiceDeclareImpl implements CustomerService {
//dao
private CustomerDao dao ;
//注入dao
public void setDao(CustomerDao dao) {
this.dao = dao;
}
public List<Customer> findCustomerByName(String name) {
return dao.findCustomerByName(name);
}
public void insertCustomer(Customer c) {
dao.insertCustomer(c);
}
/**
* 批量保存, 编程式事务管理
*/
public void saveCustomers(final List<Customer> list) {
CustomerService proxy = (CustomerService) AopContext.currentProxy();
System.out.println(proxy);
for(Customer c : list){
proxy.insertCustomer(c);
}
}
public void updateCustomer(Customer c) {
dao.updateCustomer(c);
}
}
配置文件:
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!-- 指定分散配置的文件的位置 -->
<context:property-placeholder location="classpath:cn/itcast/spring/hibernate/jdbc.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverclass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean>
<!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 指定hibernate自身的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<!-- 映射资源文件的位置
<property name="mappingResources">
<list>
<value>classpath:cn/itcast/spring/domain/Customer.hbm.xml</value>
</list>
</property>
-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/itcast/spring/domain</value>
</list>
</property>
<!-- 使用hibernate自身的配置文件配置
<property name="configLocations">
<list>
<value>classpath:hibernate.cfg.xml</value>
</list>
</property>
-->
</bean>
<!-- hibernate模板,封装样板代码 -->
<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- customerDao -->
<bean id="customerDao" class="cn.itcast.spring.hibernate.CustomerDaoImpl">
<property name="ht" ref="ht" />
</bean>
<!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- customerService,目标对象 -->
<bean id="customerServiceTarget" class="cn.itcast.spring.hibernate.CustomerServiceDeclareImpl">
<property name="dao" ref="customerDao" />
</bean>
<!-- 抽象的事务代理对象,专门用于继承 -->
<bean id="abstracTx"
abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 注入事务管理器 -->
<property name="transactionManager" ref="htm" />
<!-- 事务属性集(设置事务策略的) -->
<property name="transactionAttributes">
<props>
<prop key="insertCustomer">PROPAGATION_MANDATORY,ISOLATION_DEFAULT</prop>
<prop key="saveCustomers">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
<prop key="update*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
<prop key="delete*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
<prop key="load*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop>
</props>
</property>
</bean>
<!-- 具体的事务代理对象 -->
<bean id="customerService" parent="abstracTx">
<property name="proxyInterfaces">
<value>cn.itcast.spring.hibernate.CustomerService</value>
</property>
<!-- 暴露代理 -->
<property name="exposeProxy" value="true" />
<property name="target" ref="customerServiceTarget" />
</bean>
</beans>