欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring声明式事务

程序员文章站 2022-03-05 10:07:32
...

分析:
通过编程方法实现事务控制
1、编写增强类,实现环绕增强(前置增强+后置增强)、抛出异常增强
使用spring提供三个事务控制对象,进行实现。

2、配置aop切面

声明式事务控制:
1、增强类由spring提供
2、aop的切面还需要程序员配置

  • dao:
package cn.itcast.crm.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cn.itcast.crm.dao.CustomerDao;
import cn.itcast.crm.domain.CstCustomer;

//标识这个dao然后名字为:customerDao,如果不写value默认为类名首字母小写
//@Repository("customerDao")
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	@Override
	public void insert(CstCustomer cstCustomer) { // 增加客户基本信息
		this.getHibernateTemplate().save(cstCustomer);
	}

	@Override
	public void delete(Long custId) { // 删除客户基本信息
		CstCustomer customer = this.getHibernateTemplate().get(CstCustomer.class, custId);
		this.getHibernateTemplate().delete(customer);

	}

	@Override
	public void update(CstCustomer cstCustomer) { // 修改客户基本信息
		this.getHibernateTemplate().update(cstCustomer);
	}

	@Override
	public CstCustomer findCustomerById(Long custId) { // 查询单个客户基本信息
		return this.getHibernateTemplate().get(CstCustomer.class, custId);
	}

	public Long findCustomerCount(DetachedCriteria detachedCriteria) {
		// 查询总数
		DetachedCriteria criteria = detachedCriteria.setProjection(Projections.rowCount());
		List list = this.getHibernateTemplate().findByCriteria(detachedCriteria);
		Long total = (Long) list.get(0);// 因为查到的是一个对象所以直接得到第一个就行了
		return total;
	}

	public List<CstCustomer> findCustomerList(DetachedCriteria detachedCriteria, int firstResult, int maxResults) {
		return (List<CstCustomer>) this.getHibernateTemplate().findByCriteria(detachedCriteria, firstResult,
				maxResults);
	}

}

  • service编写
  • `package cn.itcast.crm.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import cn.itcast.crm.dao.CustomerDao;
import cn.itcast.crm.dao.CustomerDetailDao;
import cn.itcast.crm.domain.CstCustomer;
import cn.itcast.crm.domain.CstCustomerDetail;
import cn.itcast.crm.service.CustomerService;

//标识为service
//@Service(“customerService”)
public class CustomerServiceImpl implements CustomerService {
// @Autowired//按类型注入
private CustomerDao customerDao;
// @Autowired//按类型注入
private CustomerDetailDao customerDetailDao;
// 添加客户同时添加联系人、客户详细信息
public void insertCustomer(CstCustomer cstCustomer, CstCustomerDetail cstCustomerDetail) {
// 定义事务
// 保存客户信息
customerDao.insert(cstCustomer);
// 客户详细信息和客户信息保持一致
cstCustomerDetail.setCustId(cstCustomer.getCustId());
// 保存客户详细信息
customerDetailDao.insert(cstCustomerDetail);
}

// 查询客户列表总记录数
@Override
public long findCustomerCount(CstCustomer cstCustomer) {

// ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“classpath:spring/applicationContext.xml”);
// CustomerDao customerDao = (CustomerDao) applicationContext.getBean(“customerDao”);
//
// 拼接查询条件
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CstCustomer.class);
if (cstCustomer != null) {
if (cstCustomer.getCustName() != null && !cstCustomer.getCustName().equals("")) {
// 拼接查询条件客户名称
detachedCriteria.add(Restrictions.eq(“custName”, cstCustomer.getCustName()));
}
}
return customerDao.findCustomerCount(detachedCriteria);
}

@Override
public List<CstCustomer> findCustomerList(CstCustomer cstCustomer, int firstResult, int maxResults) {
	// 拼接查询条件
	DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CstCustomer.class);
	if (cstCustomer != null) {
		if (cstCustomer.getCustName() != null && !cstCustomer.getCustName().equals("")) {
			// 拼接查询条件客户名称
			detachedCriteria.add(Restrictions.eq("custName", cstCustomer.getCustName()));
		}
	}
	return customerDao.findCustomerList(detachedCriteria, firstResult, maxResults);

}

// @Override
// public void updateCustomer(Long custId,CstCustomer cstCustomer,CstCustomerDetail cstCustomerDetail) {
// Session session = HibernateUtil.getCurrentSession();
// try {
// //开启事务
// session.beginTransaction();
// CustomerDao customerDao = new CustomerDaoImpl();
// CustomerDetailDao customerDetailDao = new CustomerDetailDaoImpl();
//
// //根据id查询客户
// CstCustomer cstCustomer_update = customerDao.findCustomerById(custId);
// cstCustomer_update.setCustName(cstCustomer.getCustName());//更新客户名称
//
// customerDao.update(cstCustomer_update);
// //根据id查询客户详细信息
// CstCustomerDetail cstCustomerDetail_update = customerDetailDao.findCustomerDetailById(custId);
// cstCustomerDetail_update.setCustAddress(cstCustomerDetail.getCustAddress());//联系地址
// customerDetailDao.update(cstCustomerDetail);
// //提交事务
// session.getTransaction().commit();
// } catch (Exception e) {
// e.printStackTrace();
// //回滚事务
// session.getTransaction().rollback();
// } finally {
// //关闭session
// HibernateUtil.closeSession();
// }
// }

// @Override
// public void deleteCustomer(Long custId) {
// Session session = HibernateUtil.getCurrentSession();
// try {
// //开启事务
// session.beginTransaction();
//// ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“classpath:spring/applicationContext.xml”);
//// CustomerDao customerDao = (CustomerDao) applicationContext.getBean(“customerDao”);
//// CustomerDetailDao customerDetailDao = (CustomerDetailDao) applicationContext.getBean(“customerDetailDao”);
// customerDao.delete(custId);
// customerDetailDao.delete(custId);
// //提交事务
// session.getTransaction().commit();
// } catch (Exception e) {
// e.printStackTrace();
// //回滚事务
// session.getTransaction().rollback();
// } finally {
// //关闭session
// HibernateUtil.closeSession();
// }
//
// }

@Override
public CstCustomer findCustomerById(Long custId) {
	return customerDao.findCustomerById(custId);
}

// 根据主键查询客户详细信息
public CstCustomerDetail findCstCustomerDetailById(Long custId) {
	return customerDetailDao.findCustomerDetailById(custId);
}

public CustomerDao getCustomerDao() {
	return customerDao;
}

public void setCustomerDao(CustomerDao customerDao) {
	this.customerDao = customerDao;
}

public CustomerDetailDao getCustomerDetailDao() {
	return customerDetailDao;
}

public void setCustomerDetailDao(CustomerDetailDao customerDetailDao) {
	this.customerDetailDao = customerDetailDao;
}

}
`

  • service配置

  • `

     <property name="customerDao" ref="customerDao" />
     <property name="customerDetailDao" ref="customerDetailDao" />
    
`
  • 事务管理器(重点在这里)

  • `

     	<!-- 配置事务 -->
     	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
     	<!-- 配置依赖 -->
     	<property name="sessionFactory" ref="sessionFactory"/>
     	</bean>
     	<!-- 事务增强配置 -->
     	<tx:advice id="txAdvice" transaction-manager="transactionManager">
     		<tx:attributes>
     			<tx:method name="insert*" propagation="REQUIRED" /><!-- 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中-->
     			<tx:method name="delete*" propagation="REQUIRED"/>
     			<tx:method name="update*" propagation="REQUIRED" />
     			<tx:method name="get*" propagation="SUPPORTS" read-only="true"/><!-- 支持当前事务,如果当前没有事务,就以非事务方式执行 -->
     			<tx:method name="find*" propagation="SUPPORTS" read-only="true"/><!-- read-only="true" 只读 -->
     			<tx:method name="save*" propagation="REQUIRED"/>
     		</tx:attributes>
     	</tx:advice>
     	<!-- 配置aop代理
     		aop切面
     	 -->
     	<aop:config>
     		<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.crm.service.impl.*.*(..))"/><!-- 切入点 -->
     	</aop:config>
    
`

了解配置jdk或cglib

在aop代理配置处,可以通过proxy-target-class控制使用的代理方式:
proxy-target-class=false 默认值,使用jdk代理

<aop:config proxy-target-class=“false”>

proxy-target-class=true 使用cglib代理

<aop:config proxy-target-class=“true”>

如果此类没有实现接口,只能使用cglib代理!!!

相关标签: 事务