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层中制造异常,看是否回滚了

posted @   Arbitrary233  阅读(161)  评论(0编辑  收藏  举报
编辑推荐:
· 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)
点击右上角即可分享
微信分享提示