栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring——事务操作(完全注解模式)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring——事务操作(完全注解模式)

Spring基于注解的事务操作:

创建配置类
BookConfig.java

package com.example.demo1;

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;

@Configuration//配置类
@ComponentScan(basePackages = "com.example.demo1")//组件扫描
@EnableTransactionManagement//事务管理
public class BookConfig {
    @Bean(value = "druidDataSource")
    public DruidDataSource getDruidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/springdemo?serverTimezone=Asia/Shanghai");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        return druidDataSource;
    }
    @Bean(value = "jdbcTemplate")
    public JdbcTemplate getJdbcTemplate(DruidDataSource druidDataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(druidDataSource);
        return jdbcTemplate;

    }
    @Bean(value = "dataSourceTransactionManager")
    public DataSourceTransactionManager getDaSourceTransactionManager(DruidDataSource druidDataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(druidDataSource);
        return dataSourceTransactionManager;
    }
}

BookDao.java

package com.example.demo1.dao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository(value = "bookDao")//创建bean
public class BookDao {
    @Autowired//属性注入,自动装配
    private JdbcTemplate jdbcTemplate;
    public void add(String b){
        String sql = "update person set money = money + 100 where name = ?";
        int update = jdbcTemplate.update(sql, b);
        System.out.println(update+"add");

    }
    public void minus(String a){
        String sql = "update person set money = money - 100 where name = ?";
        int update = jdbcTemplate.update(sql, a);
        System.out.println(update+"minus");
    }
}

BookService.java

package com.example.demo1.service;

import com.example.demo1.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "bookService")
@Transactional//开启事务
public class BookService {
    @Autowired
    private BookDao bookDao;
    public void money(String a,String b){
        bookDao.minus(a);
        int c = 1/0;//模拟错误
        bookDao.add(b);
        System.out.println("转账完成");
    }
}

测试:
Test.java

    @org.junit.Test
    public void money(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BookConfig.class);
        applicationContext.getBean("bookService", com.example.demo1.service.BookService.class).money("张三","李四");
    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/865801.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号