模拟转账操作,即Jone减少500,tom增加500
如果有疑问请访问spring事务控制-基于xml方式
1.创建数据表
2.创建Account实体类
public class Account {
private String name;
private String money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
}
3.创建AccountDao接口及其实现类(接口代码省略)
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate template;
@Override
public void out(String outMan, double money) {
template.update("update account set money=money-? where name=?",money,outMan);
}
@Override
public void in(String inMan, double money) {
template.update("update account set money=money+? where name=?",money,inMan);
}
}
4.创建AccountService接口及其实现类(接口代码省略)
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
@Override
public void transfer(String outMan, String inMan,double money) {
accountDao.out(outMan,money);
accountDao.in(inMan,money);
}
}
5.配置spring文件
<?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" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置组件扫描-->
<context:component-scan base-package="com.hao"/>
<!--开启事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置平台事务管理器,当前的DataSourceTransactionManager是jdbc、mybatis技术,如果以后使用了其他技术,则此处需要修改-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
<property name="user" value="root"/>
<property name="password" value="hao20001010"/>
</bean>
<!-- 将模板对象注入到spring容器-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
6.测试:
public class AccountController {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService service= context.getBean(AccountService.class);
service.transfer("Jone","tom",500);
}
}
结果: