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

相关推荐
TeleostNaCl5 分钟前
Android TV | 一种不跳出应用指定页面的类 Monkey 的 Android TV 压测脚本
android·经验分享·压力测试
4***W4298 分钟前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
k***08299 分钟前
mysql中general_log日志详解
android·数据库·mysql
程序员-周李斌10 分钟前
ArrayList 源码深度分析(基于 JDK 8)
java·开发语言·数据结构·算法·list
safestar201211 分钟前
Spring Boot的魔法与陷阱:从自动配置原理到生产环境避坑实战
java·spring boot·后端
v***553413 分钟前
Spring Boot环境配置
java·spring boot·后端
J***516813 分钟前
Spring Cloud GateWay搭建
java
IT·小灰灰15 分钟前
深度解析重排序AI模型:基于硅基流动API调用多语言重排序AI实战指南
java·大数据·javascript·人工智能·python·数据挖掘·php
一辉ComeOn16 分钟前
【大数据高并发核心场景实战】 数据持久化层 - 分表分库
java·大数据·分布式·mysql·系统架构
2501_9371549321 分钟前
酷秒神马 9.0 版源码系统实测
android·源码·源代码管理·机顶盒