最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Spring JdbcTemplate事務(wù)注解

配置類方式配置

在之前的操作中,相關(guān)的配置還是寫在了 xml 配置文件中?,F(xiàn)在,使用配置類的方式進(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>
    <!--開啟事務(wù)注釋-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

完全注解方式

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

把 xml 里的配置在配置類里用注解方式實(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  // 聲明配置類
@ComponentScan(basePackages = "com.pingguo.spring5")  // 開啟注解掃描
@EnableTransactionManagement  // 開啟事務(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 來讀取配置類。

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)文章

  • 原來Java接口多實(shí)現(xiàn)還可以這樣玩

    原來Java接口多實(shí)現(xiàn)還可以這樣玩

    JAVA中類不直接支持多繼承,因?yàn)闀?huì)出現(xiàn)調(diào)用的不確定性,所以JAVA將多繼承機(jī)制進(jìn)行改良,在JAVA中變成了多實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于Java接口多實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • java實(shí)現(xiàn)文件重命名的方法

    java實(shí)現(xiàn)文件重命名的方法

    這篇文章主要介紹了java實(shí)現(xiàn)文件重命名的方法,涉及java針對(duì)文件的重命名操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • Mybatis中的@Param及動(dòng)態(tài)SQL詳解

    Mybatis中的@Param及動(dòng)態(tài)SQL詳解

    這篇文章主要介紹了Mybatis中的@Param及動(dòng)態(tài)SQL詳解,@Param是MyBatis所提供的作為Dao層的注解,作用是用于傳遞參數(shù),從而可以與SQL中的的字段名相對(duì)應(yīng),需要的朋友可以參考下
    2023-10-10
  • Mybatis?Plus?QueryWrapper復(fù)合用法詳解

    Mybatis?Plus?QueryWrapper復(fù)合用法詳解

    這篇文章主要介紹了Mybatis?Plus?QueryWrapper復(fù)合用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • jvm信息jmap使用的基本方法教程

    jvm信息jmap使用的基本方法教程

    JDK本身提供了很多方便的JVM性能調(diào)優(yōu)監(jiān)控工具,除了集成式的VisualVM和jConsole外,還有jps、jstack、jmap、jhat、jstat等小巧的工具,下面這篇文章主要給大家介紹了關(guān)于jvm信息jmap使用的基本方法教程,需要的朋友可以參考下
    2018-08-08
  • SpringBoot模擬實(shí)現(xiàn)流式輸出效果

    SpringBoot模擬實(shí)現(xiàn)流式輸出效果

    這篇文章主要為大家詳細(xì)介紹了如何使用SpringBoot模擬實(shí)現(xiàn)流式輸出效果,并在前端使用流式接收數(shù)據(jù)并打印,感興趣的小伙伴可以參考一下
    2025-03-03
  • 深入了解SparkSQL中數(shù)據(jù)的加載與保存

    深入了解SparkSQL中數(shù)據(jù)的加載與保存

    這篇文章主要為大家詳細(xì)介紹了SparkSQL中數(shù)據(jù)的加載與保存的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下
    2023-11-11
  • 詳談Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑

    詳談Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑

    下面小編就為大家分享一篇Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2017-12-12
  • Java GZIPOutputStream流壓縮文件的操作

    Java GZIPOutputStream流壓縮文件的操作

    這篇文章主要介紹了Java GZIPOutputStream流壓縮文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • SpringBoot實(shí)現(xiàn)動(dòng)態(tài)多線程并發(fā)定時(shí)任務(wù)

    SpringBoot實(shí)現(xiàn)動(dòng)態(tài)多線程并發(fā)定時(shí)任務(wù)

    這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)動(dòng)態(tài)多線程并發(fā)定時(shí)任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05

最新評(píng)論

尼木县| 横峰县| 宁强县| 鄄城县| 奉节县| 锦州市| 平昌县| 额敏县| 香格里拉县| 马山县| 浮梁县| 芜湖县| 林西县| 曲松县| 会东县| 霍邱县| 兴和县| 衡阳市| 九台市| 景泰县| 枝江市| 陇西县| 湘潭县| 双柏县| 洞头县| 会东县| 罗甸县| 河曲县| 东台市| 英吉沙县| 泸西县| 湟源县| 全南县| 达孜县| 博乐市| 时尚| 集安市| 临高县| 犍为县| 利川市| 惠安县|