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

20_Spring_零XML事务控制

时间:2023-03-06 13:22:10浏览次数:35  
标签: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;
    }
}

 

测试代码

@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);
    }

 

标签:XML,jdbc,20,Spring,springframework,dataSource,context,org,import
From: https://www.cnblogs.com/89564f/p/17183506.html

相关文章

  • 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}\)表示令由......
  • APIO 2013 T2 Toll
    想要解决这道题的最大难点是:如果我们前面插入了边的话,可能会改变树的形态,从而对后面的点的安排产生影响。每次都需要重构树形态。考虑消除顺序的影响,枚举最终加入生成树的......
  • [SDOI2019] 移动金币 题解
    首先考虑一个状态什么时候是合法的。不难把游戏过程抽象为阶梯Nim博弈。根据阶梯Nim博弈的结论,先手必胜当且仅当奇数位上的异或和不为0。考虑将本题中每个棋子后面......