19、spring注解学习(声明式事务)——spring注解版声明式事务
1、用jdbc操作数据库举例,在导入了aop和spring依赖以外,导入sping-jdbc依赖,先创建配置类TxConfig,在类中注册了三个bean
dataSource
jdbcTemplate
platformTransactionManager
还有@EnableTransactionManagement//开启基于注解的事务管理功能 千万不能忘记!
package crm.mytest.tc; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.beans.PropertyVetoException; @EnableTransactionManagement//开启基于注解的事务管理功能,重要!!! @Configuration @ComponentScan("crm.mytest.tc") public class TxConfig { /** * DataSource * @return * @throws PropertyVetoException */ @Bean public DataSource dataSource() throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser("root"); dataSource.setPassword("123"); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql:///crm"); return dataSource; } @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource){ JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; } //注册事务管理器在容器中,重要!!! @Bean public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException { return new DataSourceTransactionManager(dataSource());//放入数据源 } }
2、简略的创建一个UserDao和UserService,模拟一下开发的mvc模式,这里就先不用接口了
package crm.mytest.tc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class UserDao { @Autowired private JdbcTemplate jdbcTemplate; public void insert(){ String sql="insert into test values(?,?,?)"; jdbcTemplate.update(sql, null, "liweijun",1); } }
package crm.mytest.tc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class UserService { @Autowired private UserDao userDao; @Transactional public void insert(){ userDao.insert(); System.out.println("插入完成"); // int i=1/0; } }
3、最后是测试类
package crm.mytest.tc; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = crm.mytest.tc.TxConfig.class) public class Demo { @Autowired private UserService userService; @Test public void demo1(){ userService.insert(); } }
最后可以自行测试在service层中制造异常,看是否回滚了
分类:
Spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)