广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring框架JdbcTemplate数据库事务管理完全注解方式
  • 871
分享到

Spring框架JdbcTemplate数据库事务管理完全注解方式

2024-04-02 19:04:59 871人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录spring JdbcTemplate事务注解配置类方式配置完全注解方式一、创建配置类二、测试注解方式的事务管理Spring JdbcTemplate事务注解 配置类方式配置 在

Spring 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

查看数据表数据的修改情况。

成功。

以上就是Spring框架JdbcTemplate数据库事务管理完全注解方式的详细内容,更多关于Spring JdbcTemplate事务注解的资料请关注编程网其它相关文章!

--结束END--

本文标题: Spring框架JdbcTemplate数据库事务管理完全注解方式

本文链接: https://www.lsjlt.com/news/150105.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring框架JdbcTemplate数据库事务管理完全注解方式
    目录Spring JdbcTemplate事务注解配置类方式配置完全注解方式一、创建配置类二、测试注解方式的事务管理Spring JdbcTemplate事务注解 配置类方式配置 在...
    99+
    2022-11-13
  • Spring框架JdbcTemplate数据库事务管理完全注解方式是什么
    这篇“Spring框架JdbcTemplate数据库事务管理完全注解方式是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“...
    99+
    2023-06-30
  • 使用spring框架实现数据库事务处理方式
    目录使用spring框架实现数据库事务处理JDBC对数据库事务处理的支持JDBC定义了五种事务隔离级别来解决这些并发导致的问题在spring框架中调用一个数据库事务处理分三步走:sp...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作