1.导入相关jar坐标
<dependencies>
<!--spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<!--spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<!--c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!--aspectjweaver -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<!--spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.9</version>
</dependency>
<!--spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
2.编写domain,Dao,Imp和service
public class Account {
private String name;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Account(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
public interface AccountDao {
void in(Account account);
void out(Account account);
}
@Repository("AccountImp")
public class AccountImp implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void in(Account account) {
jdbcTemplate.update("update account set price=price+? where name=?",account.getPrice(),
account.getName());
}
@Override
public void out(Account account) {
jdbcTemplate.update("update account set price=price-? where name=?",account.getPrice(),
account.getName());
}
}
@Service
public class AccountService {
private AccountImp accountImp;
public AccountService(AccountImp accountImp) {
this.accountImp = accountImp;
}
@Transactional(isolation = Isolation.DEFAULT ,timeout = -1,readOnly = false)
public void transfer(String name, String in, double money){
Account account=new Account(name,money);
accountImp.out(account);
account=new Account(in,money);
int i=1/0;
accountImp.in(account);
}
}
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:context="http://www.springframework.org/schema/context" 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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--扫描组件-->
<context:component-scan base-package="com"/>
<!--引入外部文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean name="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driver}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置jdbcTemplate-->
<bean name="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"/>
</bean>
<!--配置事务平台管理器-->
<bean name="plate" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="plate"/>
</beans>
4.外部文件
jdbc.url=jdbc:mysql://localhost:3306/test-spring
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=
5.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Test {
@Autowired
private AccountService accountService;
@org.junit.Test
public void test(){
accountService.transfer("xu","tom",100);
}
}