Spring XML事务配置
事先准备:
配置数据源对象
用<bean>实例化各个业务对象。
1.配置事务管理器。
<bean id="transactionManager" class="org.springframework.jdbc.datasourceManager"> <property name="datasource" ref="datasource"></property> </bean>
2.配置事务属性
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="方法名" propagation="REQUIRES_NEW"/> <!--新开事务--> <tx:method name="*"/> <!--使用原有事务--> </tx:attributes> </tx:advice>
3.配置事务切入点,注入事务属性
<aop:config> <aop:pointcut expression="execution(.......)" id="txPointCut"/> <aop:advisor advice-ref="txtAdvice" pointcut-ref="txtPointCut"/> </aop:config>
下面看一个实例:
准备工作:导入c3p0、Spring框架、Mysql、AOP的jar包,并配置好
db.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mydb
user=root
password=
minPoolSize=5
maxPoolSize=20
initialPoolSize=5
1.做两个接口IInfoDao和IWorkDao
package com.itnba.maya.dao; public interface IInfoDao { public void delete(String code); }
package com.itnba.maya.dao; public interface IWorkDao { public void deleteByInfoCode(String code); }
2.做两个类继承以上两个接口:InfoDao和WorkDao
package com.itnba.maya.daoImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.itnba.maya.dao.IInfoDao; public class InfoDao implements IInfoDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public void delete(String code) { String sql = "delete from info where code=?"; jdbcTemplate.update(sql, code); int n = 5/0; } }
package com.itnba.maya.daoImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.itnba.maya.dao.IWorkDao; public class WorkDao implements IWorkDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public void deleteByInfoCode(String code) { String sql = "delete from work where infoCode=?"; jdbcTemplate.update(sql,code); } }
3.向xml文件 添加配置信息
<?xml version="1.0" encoding="UTF-8"?> <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" default-autowire="byName" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:property-placeholder location="classpath:db.properties"/> <!-- 生成连接池 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="minPoolSize" value="${minPoolSize}"></property> <property name="maxPoolSize" value="${maxPoolSize}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property> </bean> <!-- 生成JdbcTemplate --> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean class="com.itnba.maya.daoImpl.InfoDao" id="infoDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean class="com.itnba.maya.daoImpl.WorkDao" id="workDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean class="com.itnba.maya.serviceImpl.InfoService" id="infoService"> <property name="workDao" ref="workDao"></property> <property name="infoDao" ref="infoDao"></property> </bean> <!-- 配置事务管理器 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.itnba.maya..*.*(..))" id="pointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config> </beans>
4.建立业务接口并用类继承
IInfoService接口
package com.itnba.maya.service; public interface IInfoService { public void delete(String code); }
InfoService类
package com.itnba.maya.serviceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.itnba.maya.dao.IInfoDao; import com.itnba.maya.dao.IWorkDao; import com.itnba.maya.service.IInfoService; public class InfoService implements IInfoService { private IWorkDao workDao; private IInfoDao infoDao; public void setWorkDao(IWorkDao workDao) { this.workDao = workDao; } public void setInfoDao(IInfoDao infoDao) { this.infoDao = infoDao; } @Override public void delete(String code) { workDao.deleteByInfoCode(code); infoDao.delete(code); } }
5.创建测试类并运行
package com.itnba.maya.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.itnba.maya.dao.IInfoDao; import com.itnba.maya.dao.IWorkDao; import com.itnba.maya.service.IInfoService; import com.itnba.maya.serviceImpl.TestAll; public class Test { private static ApplicationContext context = null; private static IInfoDao infoDao = null; private static IWorkDao workDao = null; private static IInfoService infoService=null; static{ context = new ClassPathXmlApplicationContext("beans.xml"); infoDao = (IInfoDao)context.getBean("infoDao"); workDao = (IWorkDao)context.getBean("workDao"); infoService = (IInfoService)context.getBean("infoService"); } public static void main(String[] args) { infoService.delete("p009"); } }
运行结果:”p009“未删除成功
取消掉事务控制后再运行,”p009“已删除