1. tx 配置方法, 代码示例

javabean及其映射文件省略,和上篇的一样

CustomerDao.java, dao层接口

public interface CustomerDao {
public void insertCustomer(Customer c);
public void updateCustomer(Customer c);
public List<Customer> findCustomerByName(String name); //批量保存
//public void saveCustomers(List<Customer> list);
}

CustomerDaoImpl.java ,dao层实现

public class CustomerDaoImpl implements CustomerDao {

	private SessionFactory sf ;
public void setSf(SessionFactory sf) {
this.sf = sf;
} public List<Customer> findCustomerByName(String name) {
String hql = "from Customer c where c.name = ?";
Query q = sf.getCurrentSession().createQuery(hql);
q.setString(0, name);
return q.list();
} public void insertCustomer(Customer c) {
sf.getCurrentSession().save(c);
} public void updateCustomer(Customer c) {
sf.getCurrentSession().update(c);
}
}

CustomerService.java ,service层接口

public interface CustomerService {
public void insertCustomer(Customer c);
public void updateCustomer(Customer c);
public List<Customer> findCustomerByName(String name); //批量保存
public void saveCustomers(List<Customer> list);
}

CustomerServiceImpl.java,service层实现

public class CustomerServiceImpl 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) {
for(Customer c : list){
this.insertCustomer(c);
}
} public void updateCustomer(Customer c) {
dao.updateCustomer(c);
}
}

jdbc.properties

jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root c3p0.pool.size.max=10
c3p0.pool.size.min=2
c3p0.pool.size.ini=3
c3p0.pool.size.increment=2 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=none

sh.xml,spring配置文件

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<!-- 指定分散配置的文件的位置 -->
<context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/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="mappingDirectoryLocations">
<list>
<value>classpath:cn/itcast/spring/domain</value>
</list>
</property>
</bean> <!-- customerDao -->
<bean id="customerDao" class="cn.itcast.spring.hibernate.tx25.CustomerDaoImpl">
<property name="sf" ref="sessionFactory" />
</bean> <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- customerService -->
<bean id="customerService" class="cn.itcast.spring.hibernate.tx25.CustomerServiceImpl">
<property name="dao" ref="customerDao" />
</bean> <!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="htm">
<!-- 事务属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/> <tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- aop配置(配置切入点) -->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
</beans>

App.java 测试代码

public class App {

	public static void main(String[] args) throws SQLException {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"cn/itcast/spring/hibernate/tx25/sh.xml");
CustomerService cs = (CustomerService) ac.getBean("customerService");
List<Customer> list = new ArrayList<Customer>();
Customer c = null ;
for(int i = 0 ; i < 10 ; i++){
c = new Customer();
if(i == 9){
c.setName(null);
}
else{
c.setName("tom" + i);
}
c.setAge(20 + i);
list.add(c);
}
cs.saveCustomers(list);
} }

2. aspectj 配置方法 代码示例

和(1)tx方式相比只有两个文件有变动

CustomerServiceImpl.java, service实现 以加注解的方式

/**
* 通过注解方法实现事务管理
*/
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
public class CustomerServiceImpl implements CustomerService {
//dao
private CustomerDao dao ; //注入dao
public void setDao(CustomerDao dao) {
this.dao = dao;
} /**
* 只读
*/
@Transactional(readOnly=true)
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){
this.insertCustomer(c);
}
} public void updateCustomer(Customer c) {
dao.updateCustomer(c);
}
}

sh.xml 配置文件

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<!-- 指定分散配置的文件的位置 -->
<context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/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="mappingDirectoryLocations">
<list>
<value>classpath:cn/itcast/spring/domain</value>
</list>
</property>
</bean> <!-- customerDao -->
<bean id="customerDao" class="cn.itcast.spring.hibernate.aspectj.CustomerDaoImpl">
<property name="sf" ref="sessionFactory" />
</bean> <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- customerService -->
<bean id="customerService" class="cn.itcast.spring.hibernate.aspectj.CustomerServiceImpl">
<property name="dao" ref="customerDao" />
</bean> <!-- 使用注解驱动 -->
<tx:annotation-driven transaction-manager="htm"/>
</beans>

Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法的更多相关文章

  1. spring,mybatis事务管理配置与@Transactional注解使用[转]

    spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...

  2. Spring整合Hibernate 二 - 声明式的事务管理

    Spring大战Hibernate之声明式的事务管理 Spring配置文件: 添加事务管理类的bean: <bean id="txManager" class="o ...

  3. spring + ibatis 多数据源事务(分布式事务)管理配置方法(转)

    spring + ibatis 多数据源事务(分布式事务)管理配置方法(转) .我先要给大家讲一个概念:spring 的多数据源事务,这是民间的说法.官方的说法是:spring 的分布式事务.明白了这 ...

  4. spring,mybatis事务管理配置与@Transactional注解使用

    spring,mybatis事务管理配置与@Transactional注解使用[转]   spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...

  5. Spring事务管理配置以及异常处理

    Spring事务管理配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  6. spring 事物(三)—— 声明式事务管理详解

    spring的事务处理分为两种: 1.编程式事务:在程序中控制事务开始,执行和提交:详情请点此跳转: 2.声明式事务:在Spring配置文件中对事务进行配置,无须在程序中写代码:(建议使用) 我对&q ...

  7. Spring Boot 2.x基础教程:事务管理入门

    什么是事务? 我们在开发企业应用时,通常业务人员的一个操作实际上是对数据库读写的多步操作的结合.由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻 ...

  8. [Spring-AOP-XML] 利用Spirng中的AOP和XML进行事务管理

    Spring中的AOP进行事务管理有三种方式 A.自定义事务切面 利用AspectJ来编写事务,我们一般把这个切面作用在service层中.其他代码在下面 编写一个Transaction实现类,通过S ...

  9. spring3.0事务管理配置

    转载:http://war-martin.iteye.com/blog/1396335 第一种配置方法:基于XML的事务管理 这种方法不需要对原有的业务做任何修改,通过在XML文件中定义需要拦截方法的 ...

随机推荐

  1. 【Git和GitHub】学习笔记

    1. 书籍推荐: 先看一本比较简单并且好的入门书籍 Git - Book https://git-scm.com/book/zh/v2 2. 书籍理解: Git 有三种状态,你的文件可能处于其中之一: ...

  2. LeetCode 学习

    1.整数反转 题目:给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 思路:把最后的一位提取出来,放到新的容器前面,反复进行上面的操作,同时也要判断是否会导致溢出 class ...

  3. Downgrading an Exchange 2010 Server(Exchange降级)

    Downgrading an Exchange 2010 Server Microsoft Exchange Server 2010 comes in two versions: enterprise ...

  4. "零代码”开发B/S企业管理软件之二:怎么创建数据源

    声明:该软件为本人原创作品,多年来一直在使用该软件做项目,软件本身也一直在改善,在增加新的功能.但一个人总是会有很多考虑不周全的地方,希望能找到做同类软件的同行一起探讨. 本人文笔不行,能把意思表达清 ...

  5. 如何在 window 上面输入特殊字符?

    打开 字符映射表 程序 选中任意一个字符,它会在下方显示该字符的 16进制 转换16进制至10进制,并在输入法打开的状态下,按住 Alt 键输入 10 进制数值即可.

  6. 《COM本质论》COM是一个更好的C++心得分享

    昨天看了<COM本质论>的第一章"COM是一个更好的C++",认为非常有必要做一些笔记,于是整理成这篇文章.我相信你值得拥有. 这篇文章主要讲的内容是:一个实现了高速查 ...

  7. sql server性能调优

    转自:https://www.cnblogs.com/woodytu/tag/%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98%E5%9F%B9%E8%AE%AD/defaul ...

  8. eclipse导入项目,项目名出现红叉的情况(修改版)

    转至:http://blog.csdn.net/niu_hao/article/details/17440247 今天用eclipse导入同事发给我的一个项目之后,项目名称上面出现红叉,但是其他地方都 ...

  9. Django-MTV(Day66)

    阅读目录 Django基本命令 视图层路由配置系统 视图层之视图函数 MTV模型 Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象(ORM) Template(模板):负 ...

  10. 开启无线WLAN方式

    1.以管理员身份运行命令提示符 因为下面的步骤必须在管理员权限下运行,因此我们从开始菜单找到"命令提示符",或直接键入cmd快速搜索,右键单击它,选择"以管理员身份运行& ...