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)的操作被保留或被回滚(也就是事务生效)

相关推荐
也许明天y3 分钟前
Spring AI 核心原理解析:基于 1.1.4 版本拆解底层架构
java·后端·spring
小红的布丁13 分钟前
BIO、NIO、AIO 与 IO 多路复用:select、poll、epoll 详解
java·数据库·nio
lifallen15 分钟前
Flink Checkpoint 流程、Barrier 流动与 RocksDB 排障
java·大数据·flink
疯狂打码的少年19 分钟前
【Day12 Java转Python】Python工程的“骨架”——模块、包与__name__
java·开发语言·python
希望永不加班24 分钟前
SpringBoot 自定义 Starter:从零开发一个私有 Starter
java·spring boot·后端·spring·mybatis
悟空码字42 分钟前
别再System.out了!这份SpringBoot日志优雅指南,让你告别日志混乱
java·spring boot·后端
一 乐42 分钟前
工会管理|基于springboot + vue工会管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·工会管理系统
callJJ1 小时前
Spring AI ETL 数据处理管道实战指南:从原始文档到向量索引
java·人工智能·spring·ai·etl·spring ai
暗暗别做白日梦1 小时前
Maven 内部 Jar 包私服部署 + 多模块父工程核心配置
java·maven·jar
2501_915921431 小时前
苹果iOS应用开发上架与推广完整教程
android·ios·小程序·https·uni-app·iphone·webview