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

网友投稿 253 2022-07-27


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

Spring JdbcTemplate事务注解

配置类方式配置

在之前的操作中,相关的配置还是写在了 xml 配置文件中。现在,使用配置类的方式进行配置。

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:cohttp://ntext="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:cohttp://ntext="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">

完全注解方式

一、创建配置类

把 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 jdbcThttp://emplate;

}

// 创建事务管理器的对象

@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事务注解的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java全面细致讲解final的使用(java中final是什么)
下一篇:Spring5新功能日志框架Log4j2整合示例(spring 日志框架)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~