Spring 学习笔记 Part11
1. 开启spring对注解事务的支持
xml方法开启方式
<tx:annotation-driven transaction-manager="transactionManager"/>
纯注解方式开启方式 @EnableTransactionManagement
@Configuration
@EnableTransactionManagement
public class SpringConfiguration {
}
2. 注解事务的新标签
@EnableTransactionManagement:开启注解开发方式管理事务
@Transactional:声明式事务管理,可以贴在类上或贴在单独的方法上,被贴的类方法或者单独方法将能会被spring事务管理器管理事务
属性:(跟xml配置一样,在上节part10详细介绍过)
3. 完整回顾注解开发
3.1 config包
SpringConfiguration.java
@Configuration
@ComponentScan("com.swz")
@Import({JdbcConfig.class,TransactionConfig.class})
@PropertySource("classpath:jdbcConfig.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}
JdbcConfig.java
public class JdbcConfig {
@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("template")
public JdbcTemplate createJdbcTemplate(DataSource ds){
return new JdbcTemplate(ds);
}
@Bean("ds")
public DataSource getDataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
TransactionConfig.java
public class TransactionConfig {
// PlatformTransactionManager是接口,DataSourceTransactionManager是实现类
@Bean("txManager")
public PlatformTransactionManager getDataSourceTransactionManager(DataSource ds){
return new DataSourceTransactionManager(ds);
}
}
3.2 Service包
accountServiceImpl.java
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao dao;
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
@Override
public Account findAccountById(Integer accountId) {
return dao.findAccountById(accountId);
}
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@Override
public void transfer(String sourceName, String targetName, Float money) {
Account source = dao.findAccountByName(sourceName);
Account target = dao.findAccountByName(targetName);
source.setMoney(source.getMoney()-money);
target.setMoney(target.getMoney()+money);
int i=1/0;
dao.updateAccount(source);
dao.updateAccount(target);
}
}
3.3 Dao包
AccountDaoImpl.java
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate template;
@Override
public Account findAccountById(Integer accountId) {
return template.queryForObject("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
}
@Override
public Account findAccountByName(String accountName) {
List<Account> accounts = template.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), accountName);
if(accounts.isEmpty()){
return null;
}
if(accounts.size()>1){
throw new RuntimeException();
}
return accounts.get(0);
}
@Override
public void updateAccount(Account account) {
template.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}
}
3.4 其他
jdbcConfig.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
Test.java
public class AccountServiceTest {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
AccountService service = ac.getBean("accountService", AccountService.class);
service.transfer("aaa","bbb",100f);
}
}
到此,spring基础知识的学习先告一段落。