首页 > 其他分享 >20_Spring_零XML事务控制

20_Spring_零XML事务控制

时间:2023-03-06 13:32:01浏览次数:32  
标签:XML jdbc 20 Spring springframework dataSource context org import


 创建配置类
 

package com.msb.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
@Configuration // 配置类标志注解
@ComponentScan(basePackages = "com.msb") // spring包扫描
@PropertySource("classpath:jdbc.properties") // 读取属性配置文件
@EnableTransactionManagement // 开启事务注解
public class SpringConfig {
@Value("${jdbc_driver}")
private String driver;
@Value("${jdbc_url}")
private String url;
@Value("${jdbc_username}")
private String username;
@Value("${jdbc_password}")
private String password;
/*创建数据库连接池*/
@Bean
public DruidDataSource getDruidDataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
/*创建JdbcTemplate对象*/
@Bean
public JdbcTemplate getJdbcTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate=new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
/*创建事务管理器*/
@Bean
public PlatformTransactionManager getPlatformTransactionManager(DataSource dataSource){
DataSourceTransactionManager transactionManager =new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}

20_Spring_零XML事务控制_spring

20_Spring_零XML事务控制_Source_02

 

测试代码

@Test()
public void testTransaction3(){
ApplicationContext context =new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService accountService = context.getBean(AccountService.class);
int rows = accountService.transMoney(1, 2, 100);
System.out.println(rows);
}

20_Spring_零XML事务控制_bc_03

20_Spring_零XML事务控制_bc_04

 


标签:XML,jdbc,20,Spring,springframework,dataSource,context,org,import
From: https://blog.51cto.com/u_15975228/6103068

相关文章

  • [SWPUCTF 2021 新生赛]sql
     前言思路:这个CTF题是一个SQL注入相关的题目,题中所含相关知识点:题目中过滤了空格、等于号,使用/**/替换空格符,使用like替换等于号,后面步骤均为常规sql注入;到最后显示......
  • 20_Spring_零XML事务控制
    ​ 创建配置类 packagecom.msb.config;importcom.alibaba.druid.pool.DruidDataSource;importorg.springframework.beans.factory.annotation.Value;importor......
  • Spring Boot + Vue3 前后端分离 实战 wiki 知识库系统<一>--Vue3 + Vue CLI 项Vue3 +
    接着上一次https://www.cnblogs.com/webor2006/p/17139526.html的继续往下,这次则开始涉及到前端Vue框架的学习了,为之后的后台界面搭建打好地基。了解Vue和VueCLI及其关系......
  • 英语 2023-3-6
    Television,sinceitsfirstprevalenceoverradiointhe1950s,hasplayedamoreandmoreimportantroleinpeople'slives,Asamatteroffact,ithasbecomes......
  • 19_Spring_事务管理XML配置方式
    ​ applicationContext中,通过AOP实现事务的控制 <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xm......
  • 19_Spring_事务管理XML配置方式
    ​ applicationContext中,通过AOP实现事务的控制 <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xm......
  • C++学生成绩管理系统[2023-03-06]
    C++学生成绩管理系统[2023-03-06]C++课程设计说明参与专业信息和数学专业所有学生时间安排完成需求分析、类设计以及代码的实现答辩注意:答辩未过的需要参加下届C++......
  • C/C++飞机订票系统[2023-03-06]
    C/C++飞机订票系统[2023-03-06]三、飞机订票系统1.某公司每天有10航班(航班号、价格),每个航班的飞机,共有80个坐位,分20排,每排4个位子。编号为A,B,C,D·如座位号:10D......
  • 春季测试2023全题解
    T1涂色游戏非常困难的题目,我们需要记录每一行/每一列最后一次被修改的时间以及被修改成什么颜色。输出的时候每一个格子是受行影响还是列影响即可。复杂度\(O(nm)\)。......
  • APIO 2013 T1 ROBOTS
    每个机器人只能和相邻的机器人合并,转成人话就是任何的合并机器人只能是一段区间。而且机器人之间的行动互不干扰。那么就是区间\(dp\)了。设\(dp_{l,r,x,y}\)表示令由......