Spring jdbctemplate和事务管理器 全注解配置 不使用xml

 

/**
* spring的配置类,相当于bean.xml
*/
@Configuration
//@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>
// 作用为:配置spring容器(应用上下文)
@ComponentScan("com.zxh")//需要扫描的包
@Import({JdbcConfig.class,TransactionConfig.class})//子配置类
@PropertySource("jdbcConfig.properties")//数据源properties文件
@EnableTransactionManagement//开启spring对事务注解的支持
public class SpringConfiguration {
}

//jdbc配置类
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
* @param dataSource
* @return
*/
@Bean(name="jdbcTemplate")
public JdbcTemplate createJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}

/**
* 创建数据源对象
* @return
*/
@Bean(name="dataSource")
public DataSource createDataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}

//事务配置类


/**
* 和事务相关的配置类
*/
public class TransactionConfig {

/**
* 用于创建事务管理器对象
* @param dataSource
* @return
*/
@Bean(name="transactionManager")
public PlatformTransactionManager createTransactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}


持久层和事务层实现类不变
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    @Autowired
private JdbcTemplate jdbcTemplate;

@Override
public Account findAccountById(Integer accountId) {
List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
return accounts.isEmpty()?null:accounts.get(0);
}

@Override
public Account findAccountByName(String accountName) {
List<Account> accounts = jdbcTemplate.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) {
jdbcTemplate.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}
}
 
 
@Service("accountService")
@Transactional(propagation= Propagation.SUPPORTS,readOnly=true)//只读型事务的配置
public class AccountServiceImpl implements IAccountService {

@Autowired
private IAccountDao accountDao;


@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);

}


//需要的是读写型事务配置
@Transactional(propagation= Propagation.REQUIRED,readOnly=false)
@Override
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("transfer....");
//2.1根据名称查询转出账户
Account source = accountDao.findAccountByName(sourceName);
//2.2根据名称查询转入账户
Account target = accountDao.findAccountByName(targetName);
//2.3转出账户减钱
source.setMoney(source.getMoney()-money);
//2.4转入账户加钱
target.setMoney(target.getMoney()+money);
//2.5更新转出账户
accountDao.updateAccount(source);

// int i=1/0;

//2.6更新转入账户
accountDao.updateAccount(target);
}
}
//properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8
jdbc.username=root
jdbc.password=root


//test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= SpringConfiguration.class)
public class AccountServiceTest {

@Autowired
private IAccountService as;

@Test
public void testTransfer(){
as.transfer("aaa","bbb",100f);

}

}





posted @ 2020-03-31 19:59  sasuke。  阅读(730)  评论(0编辑  收藏  举报