spring复习:(41)全注解方式的事务

一、创建事务、数据源相关的配置类:

复制代码
package cn.edu.tju.study.service.transaction;


import com.zaxxer.hikari.HikariDataSource;
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.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
public class TransactionConfig {

    @Bean
    public DataSource getDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://xxx.xxx.xxx.xxx/test");
        dataSource.setUsername("root");
        dataSource.setPassword("MyPa");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return dataSource;
    }


    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }

    @Bean(name = "jdbcTemplate")
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

}

二、创建主配置类

复制代码
package cn.edu.tju.study.service.transaction;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan("cn.edu.tju.study.service.transaction")
public class SpringConfig {
}

三、定义service

复制代码
package cn.edu.tju.study.service.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TransactionService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void insertData(){
        jdbcTemplate.execute("insert into xx values(4,5)");
        int t = 1/0;
        jdbcTemplate.execute("insert into xx values(24,35)");

    }
}

四、定义主类,并调用service

复制代码
package cn.edu.tju.study.service.transaction;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TransactionTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        TransactionService transactionService = applicationContext.getBean("transactionService", TransactionService.class);
        transactionService.insertData();

    }
}

五、可以分别去掉和加上service类中的@Transactional注解,看运行效果。

分别是insert into xx values(4,5)的操作被保留或被回滚(也就是事务生效)

相关推荐
Thomas.Sir1 分钟前
SpringMVC 工作原理深入解析
spring·设计模式·mvc·spring mvc
StackNoOverflow3 分钟前
Maven 核心知识整理
java·maven
ekkcole3 分钟前
easyexcel2.2.10版本对本地文件指定行或多行样式处理
java·easyexcel
小七mod5 分钟前
【Nacos】Nacos1.4.x服务注册源码分析
java·spring cloud·微服务·nacos·源码·集群·注册中心
于先生吖9 分钟前
Java 打车小程序 APP 源码 顺风车滴滴跑腿系统完整实现
java·开发语言·打车系统
凌冰_11 分钟前
IDEA2025 基于 Jakarta EE 开发 Servlet + Thymeleaf
java·servlet
sanggou17 分钟前
Spring Cloud负载均衡组件到底是哪一个?
spring·spring cloud·负载均衡
会员源码网21 分钟前
可变参数与数组混用导致的方法调用异常
java
xiaoye370822 分钟前
Spring Bean 生命周期自定义扩展示例
java·spring boot·spring
sanyii31313122 分钟前
k8s工作负载-ReplicaSet控制器
java·git·kubernetes