SpringMVC事务控制(xml文件配置和注解配置)

事务的定义

事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。
原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的操作要么都做,要么都不做。
一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
持久性(durability)。持久性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。

编程式事务控制三大对象

1.PlatformTransactionManager
2.TransactionDefinition
3.TransactionStatus

PlatformTransactionManager对象

PlatformTransactionManager 接口是 spring 的事务管理器,它里面提供了我们常用的操作事务的方法
在这里插入图片描述

TransactionDefinition

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

TransactionStatus

在这里插入图片描述

SpringMVC要做的事情

在SpringMVC中我们主要要做的是当事务出现问题的时候把数据给回滚
比如A向B转钱
这要有两个动作:1.A 减钱 2.B加钱 如果1 和2 中间出现问题就把数据给回滚

转钱例子的环境搭建(用配置文件的方式)

在搭建环境的时候一共需要三个包 Controller Service 和Dao
Dao是最底层的 他相当于是只提供最底层的零件
Service是用Dao层提供的零件来组装成工具来让Service层来使用
Controller是用Service提供的工具来完成一些功能
在Dao层和Service层 我们要先写接口 来确定我们要完成什么功能
然后再写这些接口的实现类 impl

这下面的实现类中都提供了set方法是为了让spring容器通过xml配置文件来注入

dao层接口

public interface AccountDao {

    public void out(String outMan,double money);
    public void in(String inMan,double money);
}

dao层接口的实现

public class AccountDaoimpl implements AccountDao {
 private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    }

    @Override
    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    }
}

service层接口

public interface AccountService {
    public void transfer(String outMan,String inMan,double money);

}

service层接口的实现

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    public void setAccountDao(AccountDaoimpl accountDao) {
        this.accountDao = accountDao;
    }
    @Override
    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan,money);
        //  int a=1/0;
        accountDao.in(inMan,money);
    }
}

实现功能的controller

public class AccountController {
   public static void main(String[] args) {
       ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
       AccountService bean = app.getBean(AccountService.class);
       bean.transfer("tom","lucy",500);
   }
}

配置文件applicationContext.xml

<?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"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
   <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/test1"/>
       <property name="user" value="root"/>
       <property name="password" value="123456"/>
   </bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="accountDao" class="com.tong.dao.impl.AccountDaoimpl">
   <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
   <bean id="accountService" class="com.tong.service.impl.AccountServiceImpl">
       <property name="accountDao" ref="accountDao"/>
   </bean>



   <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
   </bean>
<tx:advice id="txadvice" transaction-manager="transcationManager">
   <tx:attributes>
       <tx:method name="*"/>
   </tx:attributes>
</tx:advice>
<aop:config>
   <aop:advisor advice-ref="txadvice" pointcut="execution(* com.service.*.*(..))"/>
</aop:config>


</beans>

关于配置文件的详解

这里来着重说一下说一下配置文件中关于事务控制的三部分内容

平台事务管理器

平台事务管理器提供了常用的操作事务的方法

<!--            平台事务管理器-->
    <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    选择用那个管理器👆
        <property name="dataSource" ref="dataSource"/>
        讲驱动配置导入👆
    </bean>

上面的datasource如下

  <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/test1"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
    </bean>

通知 (事务)的增强

<!--    通知 事务的增强-->
<tx:advice id="txAdvice" transaction-manager="transcationManager">
    <tx:attributes>
        <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
    </tx:attributes>
</tx:advice>

name:切点方法名称
isolation:事务的隔离级别
propogation:事务的传播行为
timeout:超时时间
read-only:是否只读

配置事务的aop织入

<!--    配置事务的aop织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"/>
              新加的功能(事务功能)    👆                👆原来的转账功能
    </aop:config>

织入指的是把原来的功能和新加的功能结合到一起的过程
execution(* com.itheima.service.impl. * . * (.*.))
com前面实际上要写 public void 这种东西 但是 *的意思是全部都可以
这个里面是指定对应的类 其中 的意思是当前的所有 两个点..指的是当前包的下面的所有

用注解的方式配置声明式事务控制

编写 AccoutDao

@Repository("accountDao")
👆是这个在spring容器中的名字
public class AccountDaoImpl implements AccountDao {
@Autowired
👆是从spring容器中根据一样的类来配置
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);
}
}

编写 AccoutService

@Service("accountService")
 👆这个在spring容器中的名字
@Transactional
  👆 这是标记当前这个是事务
public class AccountServiceImpl implements AccountService {
@Autowired
👆是从spring容器中找到一样的类来注入到👇中
private AccountDao accountDao;
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = 
Propagation.REQUIRED)
👆配置通知的属性
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
int i = 1/0;
accountDao.in(inMan,money);
}
}

编写controller

 public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-anno.xml");
        AccountService accountService = app.getBean(AccountService.class);
        accountService.transfer("tom","lucy",500);
    }

编写 applicationContext.xml 配置文件

主要的是下面这些

<!—之前省略datsSource、jdbcTemplate、平台事务管理器的配置-->
<!--组件扫描-->
<context:component-scan base-package="要扫描的地方"/>
<!--事务的注解驱动-->
<tx:annotation-driven/>

全部的是

<?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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <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/test1"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
    </bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="accountDao" class="com.tong.dao.impl.AccountDaoimpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
    <bean id="accountService" class="com.tong.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>



    <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
<tx:advice id="txadvice" transaction-manager="transcationManager">
    <tx:attributes>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:advisor advice-ref="txadvice" pointcut="execution(* com.service.*.*(..))"/>
</aop:config>

</beans>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值