欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring 注解如何開啟聲明式事務(wù)

 更新時(shí)間:2022年12月28日 10:49:29   作者:很懶的十六  
這篇文章主要介紹了spring 注解開啟聲明式事務(wù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

spring開啟聲明式事務(wù)

導(dǎo)入依賴

pom.xml

<dependencies>
? ? ? ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
? ? ? ? <!--spring依賴-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework</groupId>
? ? ? ? ? ? <artifactId>spring-context</artifactId>
? ? ? ? ? ? <version>4.3.12.RELEASE</version>
? ? ? ? </dependency>
? ? ? ? <!--單元測試依賴-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>junit</groupId>
? ? ? ? ? ? <artifactId>junit</artifactId>
? ? ? ? ? ? <version>4.13</version>
? ? ? ? ? ? <scope>test</scope>
? ? ? ? </dependency>

? ? ? ? <!--mysql驅(qū)動(dòng)-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>mysql</groupId>
? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId>
? ? ? ? ? ? <version>8.0.21</version>
? ? ? ? </dependency>
? ? ? ? <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
? ? ? ? <!--c3p0數(shù)據(jù)源-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>c3p0</groupId>
? ? ? ? ? ? <artifactId>c3p0</artifactId>
? ? ? ? ? ? <version>0.9.1.2</version>
? ? ? ? </dependency>
? ? ? ? <!--spring jdbc spring操作數(shù)據(jù)庫-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework</groupId>
? ? ? ? ? ? <artifactId>spring-jdbc</artifactId>
? ? ? ? ? ? <version>4.3.12.RELEASE</version>
? ? ? ? </dependency>
? ? </dependencies>

操作數(shù)據(jù)庫需要配置數(shù)據(jù)源,spring提供的操作數(shù)據(jù)庫的JdbcTemplate創(chuàng)建時(shí)需要傳入數(shù)據(jù)源,事務(wù)管理器創(chuàng)建時(shí)也需要傳入數(shù)據(jù)源,然后將數(shù)據(jù)源,JdbcTemplate,事務(wù)管理器注冊到容器中

具體代碼如下:

配置類文件

SpringTxConfig.java

package com.sixteen.tx;

import com.mchange.v2.c3p0.ComboPooledDataSource;
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.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@ComponentScan(basePackages = {"com.sixteen.tx"})//包掃描
@Configuration
@EnableTransactionManagement//開啟基于注解的聲明式事務(wù)
public class SpringTxConfig {

?? ?//注冊數(shù)據(jù)源
? ? @Bean
? ? public DataSource dataSource() throws PropertyVetoException {
? ? ? ? ComboPooledDataSource dataSource = new ComboPooledDataSource();
? ? ? ? dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
? ? ? ? dataSource.setPassword("123456");
? ? ? ? dataSource.setUser("root");
? ? ? ? dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=UTF-8");
? ? ? ? return dataSource;
? ? }

?? ?//注冊JdbcTemplate
? ? @Bean
? ? public JdbcTemplate jdbcTemplate() throws PropertyVetoException {
? ? ? ? //兩種方法獲取DataSource
? ? ? ? //1. 直接在方法放置參數(shù) public JdbcTemplate jdbcTemplate(DataSource dataSource)
? ? ? ? // ? ? ?默認(rèn)會去容器中獲取
? ? ? ? //2. 如下: 調(diào)用上面的方法


? ? ? ? //spring對@Configuration類有特殊處理,注冊組件的方法多次調(diào)用只是在IOC容器中找組件
? ? ? ? return new JdbcTemplate(dataSource());
? ? }

? ? //注冊事務(wù)管理器
? ? @Bean
? ? public PlatformTransactionManager transactionManager() throws PropertyVetoException {
? ? ? ? return new DataSourceTransactionManager(dataSource());//需要傳入dataSource
? ? }
}

ps: 我用的mysql是8.0+版本,編寫jdbcUrl時(shí)至少需要附帶時(shí)區(qū)參數(shù):serverTimezone=Asia/Shanghai

在此之前需要在mysql數(shù)據(jù)庫中建一張普通的User表,如下所示:(id自增)

idname
1zhangsan

業(yè)務(wù)邏輯編寫

UserDao.java

package com.sixteen.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(){
        String sql = "INSERT INTO `user`(name) VALUES(?)";
        String name = UUID.randomUUID().toString().substring(0, 5);
        jdbcTemplate.update(sql,name);
    }
}

UserService.java

package com.sixteen.tx;

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

@Service
public class UserService {

? ? @Autowired
? ? private UserDao userDao;

? ? @Transactional
? ? public void insertUser(){
? ? ? ? userDao.insert();
? ? ? ? System.out.println("插入完成");
? ? ? ? //故意制造錯(cuò)誤,使事務(wù)生效,進(jìn)行回滾
? ? ? ? int i = 10/0;
? ? }
}


測試代碼

SpringTxTest.java

package com.sixteen.test;

import com.sixteen.tx.SpringTxConfig;
import com.sixteen.tx.UserService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringTxTest {
? ? @Test
? ? public void testInsert(){
? ? ? ? AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringTxConfig.class);
? ? ? ? UserService service = context.getBean(UserService.class);
? ? ? ? service.insertUser();
? ? }
}

總結(jié)

以上就是spring如何用注解方式開啟聲明式事務(wù),細(xì)節(jié)之處可能沒有講到,但基本的實(shí)現(xiàn)還是有的,若有不足之處望見諒并指出。

這些僅為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論