目录
1.Spring开启事务的操作
1.1需求编辑
1.2 操作步骤
1.3 实现
2. Spring事务角色编辑
3.事务属性
3.1事务配置
3.2事务传播行为 编辑
3.3 案例:转账业务追加日志
1.Spring开启事务的操作
1.1需求
1.2 操作步骤
1.3 实现
public class JdbcConfig {
//1.定义一个方法获得要管理的对象
//2.添加@Bean,表示当前方法的返回值是一个bean
@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 DataSource dataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(userName);
druidDataSource.setPassword(passWord);
return druidDataSource;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.pojo");
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
//Mapper代理的配置
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.dao");
return msc;
}
}
@Configuration
@ComponentScan("com")
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
@EnableTransactionManagement //注解式的事务驱动
public class SpringConfig {
}
public interface AccountDao {
@Update("update account set money = money + #{money} where name = #{name}")
public void inMoney(@Param("name") String name, @Param("money") double money);
@Update("update account set money = money - #{money} where name = #{name}")
public void outMoney(@Param("name") String name, @Param("money") double money);
@Select("select * from account")
public List selectAll();
}
public interface AccountService {
@Transactional
public void tansfer(String in, String out, Double money);
List selectAll();
}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void tansfer(String in, String out, Double money) {
accountDao.inMoney(in,money);
int i = 1/0;//异常
accountDao.outMoney(out,money);
}
public List selectAll() {
return accountDao.selectAll();
}
}
public class JdbcConfig {
//1.定义一个方法获得要管理的对象
//2.添加@Bean,表示当前方法的返回值是一个bean
@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 DataSource dataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(userName);
druidDataSource.setPassword(passWord);
return druidDataSource;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.pojo");
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
//Mapper代理的配置
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.dao");
return msc;
}
}
@Configuration
@ComponentScan("com")
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
@EnableTransactionManagement //注解式的事务驱动
public class SpringConfig {
}
public interface AccountDao {
@Update("update account set money = money + #{money} where name = #{name}")
public void inMoney(@Param("name") String name, @Param("money") double money);
@Update("update account set money = money - #{money} where name = #{name}")
public void outMoney(@Param("name") String name, @Param("money") double money);
@Select("select * from account")
public List selectAll();
}
public interface AccountService {
@Transactional
public void tansfer(String in, String out, Double money);
List selectAll();
}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void tansfer(String in, String out, Double money) {
accountDao.inMoney(in,money);
int i = 1/0;//异常
accountDao.outMoney(out,money);
}
public List selectAll() {
return accountDao.selectAll();
}
}
2. Spring事务角色
3.事务属性
3.1事务配置
public interface AccountService {
@Transactional(rollbackFor = {IOException.class})
//IOException默认不参与回滚,需要手动加上
public void tansfer(String in, String out, Double money);
List selectAll();
}
3.2事务传播行为
3.1事务配置
public interface AccountService {
@Transactional(rollbackFor = {IOException.class})
//IOException默认不参与回滚,需要手动加上
public void tansfer(String in, String out, Double money);
List selectAll();
}
3.2事务传播行为
public interface AccountService {
@Transactional(rollbackFor = {IOException.class})
//IOException默认不参与回滚,需要手动加上
public void tansfer(String in, String out, Double money);
List selectAll();
}
3.2事务传播行为
3.3 案例:转账业务追加日志
public interface AccountDao {
@Update("update account set money = money + #{money} where name = #{name}")
public void inMoney(@Param("name") String name, @Param("money") double money);
@Update("update account set money = money - #{money} where name = #{name}")
public void outMoney(@Param("name") String name, @Param("money") double money);
}
public interface LogDao {
@Insert("insert into account_log(info,createData) values(#{info},now())")
public void addLog(String info);
}
public interface AccountService {
@Transactional(rollbackFor = {IOException.class})
//IOException默认不参与回滚,需要手动加上
public void tansfer(String in, String out, Double money);
}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Autowired
private LogService logService;
public void tansfer(String in, String out, Double money) {
try{
accountDao.inMoney(in, money);
int i = 1/0;//异常
accountDao.outMoney(out, money);
}finally {
logService.addLog(in, out, money);
}
}
}
public interface LogService {
@Transactional(propagation = Propagation.REQUIRES_NEW) //事务的传播行为:开启单独的事物
public void addLog(String in, String out, double money);
}
@Service
public class LogServiceImpl implements LogService {
@Autowired
private LogDao logDao;
public void addLog(String in, String out, double money) {
logDao.addLog(out+"给"+in+"转账"+money+"元");
}
}
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService accountService = ctx.getBean(AccountService.class);
accountService.tansfer("zhangsan", "lisi", 50.0);
}
}



