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

相关推荐
没有晚不了安7 分钟前
1.11作业
android
zhangphil10 分钟前
Android Coil3缩略图、默认占位图placeholder、error加载错误显示,Kotlin(1)
android·kotlin
布谷歌11 分钟前
Oops! 更改field的数据类型,影响到rabbitmq消费了...(有关于Java序列化)
java·开发语言·分布式·rabbitmq·java-rabbitmq
PXM的算法星球13 分钟前
java(spring boot)实现向deepseek/GPT等模型的api发送请求/多轮对话(附源码)
java·gpt·microsoft
被程序耽误的胡先生16 分钟前
java中 kafka简单应用
java·开发语言·kafka
F202269748629 分钟前
Spring MVC 对象转换器:初级开发者入门指南
java·spring·mvc
楠枬1 小时前
网页五子棋——对战后端
java·开发语言·spring boot·websocket·spring
YXWik61 小时前
23种设计模式
java·设计模式
貂蝉空大1 小时前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app