一。创建数据库
二。建立OrderDao OrderService 和测试类
package springcount;
import org.springframework.transaction.annotation.Transactional;
import c3p0.UserDao;
public class OrderService {
private OrderDao orderDao;
public OrderDao getOrderDao() {
return orderDao;
}
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
public void count(){
//小明账户减少
orderDao.lessmoney();
int i=10/0;
//小李账户增加
orderDao.moremoney();
}
}
package springcount;
import org.springframework.jdbc.core.JdbcTemplate;
public class OrderDao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void lessmoney(){
String sql="update test3 set sarlary=sarlary-? where username=?";
jdbcTemplate.update(sql,"1000","xiaowang");
}
public void moremoney(){
String sql="update test3 set sarlary=sarlary+? where username=?";
jdbcTemplate.update(sql,"1000","xiaoli");
}
}
package springcount;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCount {
@Test
public void testCount(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext2.xml");
OrderService orderService=(OrderService)context.getBean("orderService");
orderService.count();
}
}
三。写applicationContext2.xml文件(基于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: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-3.1.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">
<!-- 配置c3po连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://120.27.104.36:3306/test"></property>
<property name="user" value="hpn"></property>
<property name="password" value="hpn2017"></property>
</bean>
<!--第一步,配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--第二步,配置事务增强 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="count"/>
</tx:attributes>
</tx:advice>
<!--第三步,配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* springcount.OrderService.*(..))" id="pointcut1"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
<bean id="orderDao" class="springcount.OrderDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="orderService" class="springcount.OrderService">
<!-- 注入dao对象 -->
<property name="orderDao" ref="orderDao"></property>
</bean>
<!-- 创建jdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
基于注解方式:
<?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: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-3.1.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">
<!-- 配置c3po连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://120.27.104.36:3306/test"></property>
<property name="user" value="hpn"></property>
<property name="password" value="hpn2017"></property>
</bean>
<!--第一步,配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--第二步,配置事务增强 -->
<!-- <tx:advice id="txadvice" transaction-manager="transactionManager"> -->
<!-- <tx:attributes> -->
<!-- <tx:method name="count"/> -->
<!-- </tx:attributes> -->
<!-- </tx:advice> -->
<!--第三步,配置切面 -->
<!-- <aop:config> -->
<!-- <aop:pointcut expression="execution(* springcount.OrderService.*(..))" id="pointcut1"/> -->
<!-- <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/> -->
<!-- </aop:config> -->
<bean id="orderDao" class="springcount.OrderDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="orderService" class="springcount.OrderService">
<!-- 注入dao对象 -->
<property name="orderDao" ref="orderDao"></property>
</bean>
<!-- 创建jdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
另外还需要在OrderService类前加注释@
@Transactional
public class OrderService {
private OrderDao orderDao;
public OrderDao getOrderDao() {
return orderDao;
}
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
public void count(){
//小明账户减少
orderDao.lessmoney();
int i=10/0;
//小李账户增加
orderDao.moremoney();
}
}