代码业务跟上节课一样,差不多都是复制过来的,这节课不是通过xml配置,而是通过注解
具体操作:
第一步: 创建src/main/java/com.huanf目录
第二步: 在com.huanf目录下新建controller目录,里面新建AccountController类,里面写入如下
public class AccountController {
public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = app.getBean(AccountService.class); //模拟转账,tom转给lucy,转了500元 accountService.transfer("tom","lucy",500); }
//分析我们要对哪个地方进行事务控制,答案是对业务层进行事务控制,也就是对AccountServiceImpl类的transfer方法进行事务控制 //测试验证: 当控制台出现/ by zero报错,并且双方的钱都没多没少,就事务控制证明成功了
//实验前先去AccountServiceImpl类,把那个模拟报错解开注释}
第三步: 在com.huanf目录下新建dao目录,里面新建AccountDao接口,写入如下
public interface AccountDao { public void out(String outMan,double money); public void in(String inMan,double money);}
第四步: 在com.huanf.dao目录下新建impl目录,里面新建AccountDaoImpl类,写入如下
("accountDao")public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
//转出钱的方法。这是一个事务 public void out(String outMan, double money) { jdbcTemplate.update("update account set money=money-? where name=?",money,outMan); }
//转入钱的方法。这也是一个事务 public void in(String inMan, double money) { jdbcTemplate.update("update account set money=money+? where name=?",money,inMan); }
/*这里使用的两个注解相当于我们在applicationContext.xml里面配置了如下 <bean id="accountDao" class="com.huanf.dao.impl.AccountDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean>*/}
第五步: 在com.huanf目录下新建domain目录,里面新建Account类,写入如下
//实体类,账户类,对应数据库的account表public class Account { private String name; private double money;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getMoney() { return money; }
public void setMoney(double money) { this.money = money; }}
第六步: 在com.huanf目录下新建service目录,里面新建AccountService接口,写入如下
public interface AccountService { public void transfer(String outMan,String inMan,double money);}
第七步: 在com.huanf.service目录下新建impl目录,里面新建AccountServiceImpl类,写入如下
("accountService")//@Transactional注解如果写在类的上一行,就表示这个类的所有方法都会进行事务控制。写在类里面的具体某个方法的上一行就表示只对那个方法进行事务控制//如果类上面、具体某个方法上面,都有@Transactional注解,那么就就近原则,会默认自动选取具体某个方法上面的@Transactional注解(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED) public void transfer(String outMan, String inMan, double money) {//transfer方法要进行事务控制,就在上一行加注解,并写上属性即可 accountDao.out(outMan,money); //模拟转账中间出现错误 //int i = 1/0;//则下一行将不会被执行,也就是钱转出去,但是没人收到,就导致数据的不一致。解决:开启事务通知 accountDao.in(inMan,money); }
(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED) public void transfer2(String outMan, String inMan, double money) {//transfer2方法要进行事务控制,就在上一行加注解,并写上属性即可 accountDao.out(outMan,money); //模拟转账中间出现错误 //int i = 1/0;//则下一行将不会被执行,也就是钱转出去,但是没人收到,就导致数据的不一致。解决:开启事务通知 accountDao.in(inMan,money); }//这个是举例子,如果有多个方法要事务控制,就直接在上一行加注解即可
/* 这里使用的@Service注解、@Autowired注解相当于我们在applicationContext.xml里面配置了如下 <bean id="accountService" class="com.huanf.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean>
@Transactional注解相当于我们在applicationContext.xml里面配置了如下 <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="-1" read-only="false"/> <tx:method name="trans*" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="-1" read-only="false"/> <!--其中trans*表示以trans开头的方法。上面的属性值灰色表示默认,默认是当前数据库的属性(例如隔离级别),不写的话也是默认--> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.huanf.service.impl.*.*(..))"/> </aop:config> */}
第八步: 在applicationContext.xml里面写入如下
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--组件扫描,用于识别我们的注解--> <context:component-scan base-package="com.huanf"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/> <property name="user" value="root"/> <property name="password" value="228675"/> </bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>
<!--配置平台事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入最上面的bean的DataSource数据源--> <property name="dataSource" ref="dataSource"/> </bean>
<!--事务的注解驱动,用于识别我们写的事务注解--> <tx:annotation-driven transaction-manager="transactionManager"/></beans>
第九步pom.xml里面写入如下
<dependencies> <!--spring相关的坐标--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency>
<!--事务相关的坐标--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.5.RELEASE</version> </dependency>
<!--测试的坐标--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.5.RELEASE</version> </dependency>
<!--数据源的坐标--> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.1</version> </dependency>
<!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.31</version> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency></dependencies>
第十步: 在AccountController类运行测试
上面是快速入门,下面我们会详细分析一下
1、使用@Transactional在需要进行事务控制的类或者是方法上修饰也就是可以写在类名的上一行,也可以写在方法名的上一行。注解可用的属性跟上 节课的xml配置时使用的属性一样,例如隔离级别、是否只读
2、注解如果写在类名的上一行,那么该类里面的所以方法都使用同一套注解参数配置,也就是该类所有的方法都是相同的事务控制属性
3、注解如果写在方法名的上一行,那么该注解的事务控制属性只对当前这个方法有效
4、添加了注解之后,一定要在applicationContext.xml里面开启事务的注解驱动,即添加如下
<!--事务的注解驱动,用于识别我们写的事务注解--><tx:annotation-driven transaction-manager="transactionManager"/>