Spring的事务控制-基于注解的方式
2024-09-08 00:18:58
模拟转账操作,即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);
}
}
结果:
最新文章
- C#语法知识
- IOS自定义表格UITableViewCell
- Toolkit.getImage获取图片
- jsp中的四种对象作用域
- zencart安装第三步出现空白
- mysql颠覆实战笔记(八)--mysql的自定义异常处理怎么破
- Winform 数据验证
- PHP数组排列
- 随着时间的推移:构造SDK路径错误(An error occurred while automatically activating bundle com.android.ide.eclipse.adt)
- mysql和VS2010 C++链接过程中出现的问题
- POJ 3624 01背包
- 在程序加载过程中显示ProgressDialog 对话框
- eclipse中tomcat 中server location灰色,如何修改?
- 实验演示Oracle“多版本一致读”和“Cross DDL”
- Cngigure和BUS实现远端配置
- 快速排序quick_sort(python的两种实现方式)
- Spring Boot 面试,一个问题就干趴下了!
- [面试]future模式
- logrotate异常排查
- SecureCRT8.1+SecureCRT_keygen完成注册
热门文章
- CF1327F题解
- CentOS 通过shell脚本过滤得到服务器IP地址
- Blazor 002 : 一种开历史倒车的UI描述语言 -- Razor
- python中判断为None
- 【论文阅读】CVPR2021: MP3: A Unified Model to Map, Perceive, Predict and Plan
- docker容器登录,退出等操作命令
- XMLBeanFactory ?
- java中的方法覆盖(Overriding)和方法重载(Overloading)是什么意思?重写跟重载的区别?
- 还能这样?把 Python 自动翻译成 C++
- react开发教程(六)React与DOM