搜索 K
Appearance
博客正在加载中...
Appearance
我们在上一篇博客的基础上,改造为使用注解的方式使用事务
我们添加一个 XML 的约束,表明使用注解:添加第 6、14、15 行
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
完整代码:
package com.peterjxl.dao.impl;
import com.peterjxl.dao.IAccountDao;
import com.peterjxl.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 账户的持久层实现类
*/
@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.class), accountId);
return accounts.isEmpty() ? null : accounts.get(0); // 如果 accounts 为空,返回 null,否则返回 accounts.get(0)
}
@Override
public Account findAccountByName(String accountName) {
List<Account> accounts = jdbcTemplate.query("select * from account where name = ?", new BeanPropertyRowMapper<>(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());
}
}
由于 dao 中用到了 JdbcTemplate,我们也配置个 bean,并且加上创建容器时要扫描的包:
<context:component-scan base-package="com.peterjxl"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>然后我们删除对事务的 XML 配置:也就是删掉下面的内容
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切入点 -->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.peterjxl.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config> 然后开启 Spring 对注解事务的支持:
<tx:annotation-driven transaction-manager="transactionManager"/>然后在需要使用事务支持的地方使用 @Transactional 注解,也就是 service 实现类:
@Transactional // 事务注解
public class AccountServiceImpl implements IAccountService { 其实我们也可以在该注解里,配置事务的属性,例如:
@Transactional(propagation = Propagation.MANDATORY) // 事务注解 我们可以看其源码中,有不少属性可以配置:
public @interface Transactional {
@AliasFor("transactionManager")
String value() default "";
@AliasFor("value")
String transactionManager() default "";
Propagation propagation() default Propagation.REQUIRED;
Isolation isolation() default Isolation.DEFAULT;
}
此时我们再进行测试,也可以看到实现了事务的支持
使用注解的时候,配置步骤如下:
本项目已将源码上传到 GitHub 和 Gitee 上。并且创建了分支 demo19,读者可以通过切换分支来查看本文的示例代码