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

Spring框架JdbcTemplate數(shù)據(jù)庫(kù)事務(wù)管理完全注解方式

 更新時(shí)間:2022年05月30日 09:40:25   作者:把蘋(píng)果咬哭的測(cè)試筆記  
這篇文章主要介紹了Spring框架JdbcTemplate數(shù)據(jù)庫(kù)事務(wù)管理及完全注解方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Spring JdbcTemplate事務(wù)注解

配置類(lèi)方式配置

在之前的操作中,相關(guān)的配置還是寫(xiě)在了 xml 配置文件中?,F(xiàn)在,使用配置類(lèi)的方式進(jìn)行配置。

<?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">
        <!--注入數(shù)據(jù)源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--開(kāi)啟事務(wù)注釋-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

完全注解方式

一、創(chuàng)建配置類(lèi)

把 xml 里的配置在配置類(lèi)里用注解方式實(shí)現(xiàn)。

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  // 聲明配置類(lèi)
@ComponentScan(basePackages = "com.pingguo.spring5")  // 開(kāi)啟注解掃描
@EnableTransactionManagement  // 開(kāi)啟事務(wù)
public class TxConfig {
    // 創(chuàng)建數(shù)據(jù)庫(kù)連接池
    @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;
    }
    // 創(chuàng)建 JdbcTemplate 對(duì)象
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        // 注入 dataSource
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }
    // 創(chuàng)建事務(wù)管理器的對(duì)象
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
}

二、測(cè)試注解方式的事務(wù)管理

修改下測(cè)試方法,使用 AnnotationConfigApplicationContext 來(lái)讀取配置類(lèi)。

public class TestTrans {
    @Test
    public void testJdbc() {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(TxConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.accountMoney();
    }
}

執(zhí)行一下:

八月 08, 2021 8:49:35 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Process finished with exit code 0

查看數(shù)據(jù)表數(shù)據(jù)的修改情況。

成功。

以上就是Spring框架JdbcTemplate數(shù)據(jù)庫(kù)事務(wù)管理完全注解方式的詳細(xì)內(nèi)容,更多關(guān)于Spring JdbcTemplate事務(wù)注解的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Jenkins任務(wù)批量修改的技巧分享

    Jenkins任務(wù)批量修改的技巧分享

    這篇文章主要給大家介紹了關(guān)于Jenkins任務(wù)批量修改的一些技巧,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 解決kafka消息堆積及分區(qū)不均勻的問(wèn)題

    解決kafka消息堆積及分區(qū)不均勻的問(wèn)題

    這篇文章主要介紹了解決kafka消息堆積及分區(qū)不均勻的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java使用遞歸回溯完美解決八皇后的問(wèn)題

    Java使用遞歸回溯完美解決八皇后的問(wèn)題

    這篇文章主要介紹了Java基于循環(huán)遞歸回溯實(shí)現(xiàn)八皇后問(wèn)題算法,結(jié)合具體實(shí)例形式分析了java的遍歷、遞歸、回溯等算法實(shí)現(xiàn)八皇后問(wèn)題的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2021-11-11
  • Spring Cloud @RefreshScope 原理及使用

    Spring Cloud @RefreshScope 原理及使用

    這篇文章主要介紹了Spring Cloud @RefreshScope 原理及使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Spring Boot整合elasticsearch的詳細(xì)步驟

    Spring Boot整合elasticsearch的詳細(xì)步驟

    這篇文章主要介紹了Spring Boot整合elasticsearch的詳細(xì)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • SpringBoot Logback日志記錄到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法

    SpringBoot Logback日志記錄到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了SpringBoot Logback日志記錄到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Java?Guava異步編程實(shí)踐

    Java?Guava異步編程實(shí)踐

    今天咱們要聊的是Guava在異步編程中的應(yīng)用,讓我們搞清楚為什么要用Guava來(lái)處理異步任務(wù),在Java的世界里,異步編程是個(gè)老話題了,但它依舊非常關(guān)鍵,它能讓咱們的應(yīng)用更高效,尤其是在處理那些耗時(shí)的I/O操作
    2023-12-12
  • Springboot集成swagger實(shí)現(xiàn)方式

    Springboot集成swagger實(shí)現(xiàn)方式

    這篇文章主要介紹了Springboot集成swagger實(shí)現(xiàn)方式,通過(guò)簡(jiǎn)單的示例代碼詳細(xì)描述了實(shí)現(xiàn)過(guò)程步驟,有需要的朋友可以借鑒參考下,希望可以有所幫助
    2021-08-08
  • elasticsearch源碼分析index?action實(shí)現(xiàn)方式

    elasticsearch源碼分析index?action實(shí)現(xiàn)方式

    這篇文章主要為大家介紹了elasticsearch源碼分析index?action實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • java中BeanUtils.copyProperties的用法(超詳細(xì))

    java中BeanUtils.copyProperties的用法(超詳細(xì))

    本文介紹了BeanUtils.copyProperties()方法的使用,包括其功能、用法、注意事項(xiàng)和示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08

最新評(píng)論