1.在业务层使用@Transactional 注解
在类上,方法上都可以注解
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public void transfer(String sourceName, String targetName, Float money) {
@Service("accountService")
@Transactional
public class AccountServiceImpl implements IAccountService {
@Autowired//注解:删除set方法,不需要在AccountServiceImpl中ref引用AccountDao(bean.xml的配置)
private IAccountDao accountDao;
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
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);
}
}
2.持久层
/**持久层实现类*/
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {//删除extends JdbcDaoSupport ,定义JdbcTemplate
@Autowired//注解不用写set方法,也不用像xml用ref引用
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());
}
}
3. spring中基于注解的声明式事务控制配置步骤
1.配置事务管理器(spring提供的,不是我们自己写的)
2、开启spring对注解事务的支持
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
3、在需要事务支持的地方使用@Transactional注解
<!--配置spring创建容器时要扫描的包-->
<context:component-scan base-package="com.jh"></context:component-scan>
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/maven?serverTimezone=GMT%2B8"></property>
<property name="username" value="root"></property>
<property name="password" value="jh7851192"></property>
</bean>
<!--spring中基于注解的声明式事务控制配置步骤
1.配置事务管理器(spring提供的,不是我们自己写的)
2、开启spring对注解事务的支持
3、在需要事务支持的地方使用@Transactional注解
-->
<!--配置spring提供的事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启spring对注解事务的支持
transaction-manager引用id=transaction-manager事务管理器
-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>