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("张三","李四");
}