首先创建一个spring的配置类相当于bean.xml
package com.lwh.config;
import org.springframework.context.annotation.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@ComponentScan("com.lwh")
@Import({JdbcConfig.class,TransactionConfig.class})
@PropertySource("jdbcConfig.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}
然后创建一个和连接数据库有关的配置类
//和连接数据库有关的配置类
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;
/*创建JdbcTemplate
* */
@Bean(name = "jdbcTemplate")
public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/*创建数据源对象*/
@Bean(name = "dataSource")
public DataSource createDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
接着写一个事务相关的配置类
/*和事务相关的配置类*/
public class TransactionConfig {
/*用于创建事务管理器对象*/
@Bean(name="transactionManager")
public PlatformTransactionManager createTransactionManager(DataSource dataSource)
{
return new DataSourceTransactionManager(dataSource);
}
}
然后最关键的一步别忘了在数据访问层加上事务注解@Transactional(eesy老师忘了 卡了我一会)
@Repository("accountDao")
@Transactional(propagation = Propagation.REQUIRED)//这是重点,propagation = Propagation.REQUIRED增删改事务
public class AccountDaoImpl implements AccountDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public Account findByid(int id) {
List < Account> accounts = jdbcTemplate.query("select * from account1 where id=?", new BeanPropertyRowMapper<Account>(Account.class) ,id);
return accounts.get(0);
}
@Override
public void transfer(int sourceId, int targetId, double money) {
Account source = (Account) findByid(sourceId);
Account target = (Account) findByid(targetId);
jdbcTemplate.update("update account1 set balance=? where id=? ",source.getBalance()-money,source.getId());
//int i =1/0;
jdbcTemplate.update("update account1 set balance=? where id=? ",target.getBalance()+money,target.getId());
System.out.println("转账成功"+money+"元");
}
@Override
public void updateAccount(Account account) {
jdbcTemplate.update("update account1 set username=?,balance=? where id=?",account.getUsername(),account.getBalance(),account.getId());
}
@Override
public void deleteAccount(int id) {
jdbcTemplate.update("delete from account1 where id=?",id);
}
@Override
public List<Account> findAll() {
List<Account> accounts=jdbcTemplate.query("select *from account1",new BeanPropertyRowMapper<Account>(Account.class));
return accounts ;
}
}
最后测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class test {
@Autowired
private AccountService as;
@Test
public void transferTest(){
as.transfer(1,2,100);
}
}
tips:有一个有趣的现象 假如给dao层的accountDaoImpl的transfer方法加上try catch 那么事务管理将不起作用 (可能是spring事务管理默认catch不是一个错误指令)