使用注解的方式 模拟转账 要么都成功 要么都失败 !保持一致性!
准备工作:
jar包:
需要的类:
UserDao:
package com.hxzy.spring.c3p0.Dao; import lombok.Data;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource;
@Transactional //开启事务
@Data //生成set get
@Controller("userDao") //配置 加入ioc容器
public class UserDao {
@Resource(name = "jdbcTemplate")
private JdbcTemplate template; public void test_c3(){
String sql = "UPDATE user_all SET u_balance = u_balance-5 WHERE uid = 1";
// int a = 100/0;
String sql1 = "UPDATE user_all SET u_balance = u_balance+5 WHERE uid = 2";
template.update(sql);
template.update(sql1);
}
}
UserService:
package com.hxzy.spring.c3p0.Service; import com.hxzy.spring.c3p0.Dao.UserDao;
import lombok.Data;
import org.springframework.stereotype.Service; import javax.annotation.Resource; @Data
@Service("userService")
public class UserService {
@Resource(name = "userDao")
private UserDao userDao ;
public void test_(){
userDao.test_c3();
}
}
Test测试类:
package test; import com.hxzy.spring.c3p0.Service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserService service = (UserService) context.getBean("userService");
service.test_();
}
}
spring-config.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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--============创建c3p0链接池=========-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/user_transfer"></property>
<property name="user" value="root"></property>
<property name="password" value="gubin"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--============创建事务==============-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启对事物的支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--============开启扫包==========-->
<context:component-scan base-package="com.hxzy.spring"></context:component-scan>
</beans>
数据库: