spring事物管理以配置文件方式完成转账
1.导入jar
2.创建OrderDao和OrderService
package cn.itcast.dao;
import org.springframework.jdbc.core.JdbcTemplate;
public class OrderDao {
//注入jdbcTemplate
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//钱减少
public void reduceMoney() {
String sql = "update account set money=money-? where username=?";
jdbcTemplate.update(sql,1000,"小王");
}
//钱增加
public void addMoney() {
String sql = "update account set money=money+? where username=?";
jdbcTemplate.update(sql,1000,"小马");
}
}
package cn.itcast.service;
import cn.itcast.dao.OrderDao;
public class OrderService {
private OrderDao orderDao;
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
//调用dao的方法
//业务层逻辑,写转账业务
public void accountMoney(){
//小王减少1000
orderDao.reduceMoney();
// int i = 10/0;
//小马增加1000
orderDao.addMoney();
}
}
3.配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入属性至 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_day03?characterEncoding=utf8"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
</bean>
<!-- 创建对象 -->
<bean id="orderService" class="cn.itcast.service.OrderService">
<!-- 属性注入 -->
<property name="orderDao" ref="orderDao"/>
</bean>
<bean id="orderDao" class="cn.itcast.dao.OrderDao">
<!-- jdbcTemplate注入 -->
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- dataSource注入 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第一步配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第二部配置事物增强 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 做事物操作 -->
<tx:attributes>
<!-- 设置进行事物操作的方法的匹配规则 -->
<tx:method name="account*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 第三步配置切面 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="pointcut1" expression="execution(* cn.itcast.service.OrderService.*(..))"/>
<!-- 切面 -->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
4.测试类
package cn.itcast.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
public class TestService {
// DataSourceTransactionManager transactionManager;
@Test
public void testDemo(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
OrderService orderService = (OrderService) context.getBean("orderService");
orderService.accountMoney();
}
}
spring事物管理以配置文件方式完成转账
1.导入jar同上
2.创建OrderDao和OrderService,OrdersDao同上,OrderService类多了一个开启事物管理的注解
package cn.itcast.service;
import cn.itcast.dao.OrderDao;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class OrderService {
private OrderDao orderDao;
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
//调用dao的方法
//业务层逻辑,写转账业务
public void accountMoney(){
//小王减少1000
orderDao.reduceMoney();
// int i = 10/0;
//小马增加1000
orderDao.addMoney();
}
}
3.配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入属性至 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_day03?characterEncoding=utf8"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
</bean>
<!-- 创建对象 -->
<bean id="orderService" class="cn.itcast.service.OrderService">
<!-- 属性注入 -->
<property name="orderDao" ref="orderDao"/>
</bean>
<bean id="orderDao" class="cn.itcast.dao.OrderDao">
<!-- jdbcTemplate注入 -->
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- dataSource注入 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第一步配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第二步开启事物的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.测试类同上
两种事物管理的注解与纯配置文件比较
<!-- 第一步配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第二部配置事物增强 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 做事物操作 -->
<tx:attributes>
<!-- 设置进行事物操作的方法的匹配规则 -->
<tx:method name="account*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 第三步配置切面 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="pointcut1" expression="execution(* cn.itcast.service.OrderService.*(..))"/>
<!-- 切面 -->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
<!-- 第一步配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第二步开启事物的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>