Spring事务案例
案例准备:
1.导入jar包
注意版本一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version> 4.1 . 3 .Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version> 4.1 . 3 .Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version> 4.1 . 3 .Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version> 4.1 . 3 .Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <!-- spring jdbc 相关的包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version> 5.1 . 37 </version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version> 0.9 . 5.2 </version> </dependency> <!-- 配置的 spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> |
- 创建数据库
在springjdbc库中直接创建表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /*Table structure for table `cardinfo` ( ` */ CREATE TABLE `cardinfo` ( `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR( 20 ) NOT NULL, `money` DECIMAL( 10 , 2 ) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT= 3 DEFAULT CHARSET=utf8; /*Data for the table `cardinfo` */ INSERT INTO `cardinfo`(`id`,`username`,`money`) VALUES ( 1 , 'Helen' , '1000.00' ); INSERT INTO `cardinfo`(`id`,`username`,`money`) VALUES ( 2 , 'Tom' , '1000.00' ); |
- 创建dao接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public interface ICardInfoDao { /** * 加钱方法 * @param id * @param money */ void increaseMoney( int id , float money); /** * 减钱方法 * @param id * @param money */ void decreaseMoney( int id , float money); } |
- 创建dao实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @Component public class CardInfoDao implements ICardInfoDao { @Autowired private JdbcTemplate jdbcTemplate; public void increaseMoney( int id , float money) { jdbcTemplate.update( "update cardinfo set money = money + ? where id = ? ;" ,money,id); } public void decreaseMoney( int id , float money) { jdbcTemplate.update( "update cardinfo set money = money - ? where id = ? ;" ,money,id); } } |
- 创建service接口
1 2 3 4 5 6 | public interface ICardInfoService { //转账业务 void transfer( int from , int to , float money); } |
- 创建service实现类
1 2 3 4 5 6 7 8 9 10 11 12 | @Service public class CardInfoService implements ICardInfoService { @Autowired private ICardInfoDao cardInfoDao; public void transfer(Integer from, Integer to, Double money) { cardInfoDao.decreaseMoney(from,money); cardInfoDao.increaseMoney(to,money); } } |
- 添加ioc配置文件
先不处理事务问题
注意xmlns的导入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "http://www.springframework.org/schema/context" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-4.0.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-4.0.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-4.0.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base- package = "com.test" /> <context:property-placeholder location= "db.properties" /> <bean id= "comboPooledDataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" p:driverClass= "${jdbc.driverClass}" p:jdbcUrl= "${jdbc.jdbcUrl}" p:user= "${jdbc.user}" p:password= "${jdbc.password}" /> <bean id= "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > <property name= "dataSource" ref= "comboPooledDataSource" /> </bean> </beans> |
测试代码:
1 2 3 4 5 | ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); ICardInfoService service = (ICardInfoService) context.getBean( "cardInfoService" ); service.transfer( 1 , 2 , 100.0 ); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)