spring5 事务 纯注解开发
作者:互联网
1.TxConfig
package com.config; 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("com.demo") @EnableTransactionManagement public class TxConfig { @Bean public DruidDataSource getDruidDataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://203.195.254.63:3306/mytest"); dataSource.setUsername("myTest"); dataSource.setPassword("jx22hmyDtzfHHtLy"); return dataSource; } @Bean public JdbcTemplate getJdbcTemplate(DruidDataSource dataSource){ JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; } @Bean public DataSourceTransactionManager getDataSourceTransactionManager(DruidDataSource dataSource){ DataSourceTransactionManager manager = new DataSourceTransactionManager(); manager.setDataSource(dataSource); return manager; } }
2.UserService
package com.demo.service; import com.demo.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ) public class UserService { @Autowired private UserDao userDao; public void account(){ userDao.addMoney(); int a = 10/0; userDao.reduceMoney(); } }
标签:事务,import,springframework,public,dataSource,org,注解,annotation,spring5 来源: https://www.cnblogs.com/cciscc/p/16610373.html