注解名 | 作用 |
---|
@EnableTransactionManagement | 开启注解事务管理 |
@Transactional | 表示当前方法是一个事务方法 |
TxConfig
package com.atguigu.tx;
import com.mchange.v2.c3p0.ComboPooledDataSource;
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;
import java.beans.PropertyVetoException;
@EnableTransactionManagement
@PropertySource("classpath:/dbconfig.properties")
@ComponentScan("com.atguigu.tx")
@Configuration
public class TxConfig {
@Value("${db.user}")
private String user;
@Value("${db.password}")
private String password;
@Value("${db.driverClass}")
private String driverClass;
@Bean
public DataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev");
dataSource.setDriverClass(driverClass);
return dataSource;
}
//JdbcTemplate 简化增删改查的操作
@Bean
public JdbcTemplate jdbcTemplate() throws PropertyVetoException {
//Spring对@Configuration类会特殊处理,给容器加组件的方法,多次调用都只是从从容器里找组件
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
return jdbcTemplate;
}
//注册事务管理器在容器中
@Bean
public PlatformTransactionManager transactionManager() throws PropertyVetoException {
return new DataSourceTransactionManager(dataSource());
}
}
UserDao
package com.atguigu.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.UUID;
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void insert(){
String sql = "insert into users(username,age) values(?,?)";
//自动生成随机name
String username = UUID.randomUUID().toString().substring(0, 5);
jdbcTemplate.update(sql,username,18);
}
}
UserService
package com.atguigu.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {
@Autowired
private UserDao userDao;
@Transactional
public void insertUser(){
userDao.insert();
System.out.println("插入完成");
int a = 10/0; //测试事务,正确则插入,错误则回滚不插入
}
}
IOCTx
package com.atguigu.test;
import com.atguigu.aop.MathCalculator;
import com.atguigu.config.MyConfigOfAOP;
import com.atguigu.tx.TxConfig;
import com.atguigu.tx.UserService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class IOCTx {
@Test
public void test01(){
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(TxConfig.class);
UserService service = context.getBean(UserService.class);
service.insertUser();
context.close();
}
}