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

文章展示了如何在Spring框架中配置数据源、事务管理器以及JdbcTemplate,并在Service层使用@Transactional注解实现事务控制。通过一个主类调用Service方法插入数据,并通过注解观察事务回滚的效果。
摘要由CSDN通过智能技术生成

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值