1. 程式人生 > 其它 >【Spring 從0開始】JdbcTemplate 資料庫事務管理 - 完全註解方式

【Spring 從0開始】JdbcTemplate 資料庫事務管理 - 完全註解方式

在之前的操作中,相關的配置還是寫在了 xml 配置檔案中。現在,使用配置類的方式進行配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.pingguo.spring5"></context:component-scan>

    <!--引入外部屬性檔案-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置連線池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.username}"></property>
        <property name="password" value="${prop.password}"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入datasource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入資料來源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--開啟事務註釋-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

完全註解方式

一、建立配置類

把 xml 裡的配置在配置類裡用註解方式實現。

package com.pingguo.spring5.config;

import com.alibaba.druid.pool.DruidDataSource;
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  // 宣告配置類
@ComponentScan(basePackages = "com.pingguo.spring5")  // 開啟註解掃描
@EnableTransactionManagement  // 開啟事務
public class TxConfig {

    // 建立資料庫連線池
    @Bean
    public DruidDataSource getDruidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://223.31.222.111:3306/shop");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        return druidDataSource;
    }

    // 建立 JdbcTemplate 物件
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        // 注入 dataSource
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;

    }

    // 建立事務管理器的物件
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
}

二、測試註解方式的事務管理

修改下測試方法,使用 AnnotationConfigApplicationContext 來讀取配置類。

public class TestTrans {
    @Test
    public void testJdbc() {

        ApplicationContext context =
                new AnnotationConfigApplicationContext(TxConfig.class);

        UserService userService = context.getBean("userService", UserService.class);

        userService.accountMoney();
    }
}

執行一下:

八月 08, 2021 8:49:35 上午 com.alibaba.druid.pool.DruidDataSource info
資訊: {dataSource-1} inited

Process finished with exit code 0

檢視資料表資料的修改情況。

成功。

--不要用肉體的勤奮,去掩蓋思考的懶惰--