ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AOP----事务

2021-12-15 21:02:55  阅读:143  来源: 互联网

标签:username 事务 isbn 书得 int price ---- bookShopDao AOP


MVC三层架构:
由视图层的.jsp请求到controller控制层
在这里插入图片描述
@Aspect 切面

@PointCut 描述在哪些类哪些方法织入代码

@Advice 在方法的什么执行时机(之前或者之后)去执行

Advice分为5种

@Before,前置通知

@After(finally) 后置通知,方法执行完后

@AfterReturning,返回通知,方法成功执行之后

@AfterThrowing,异常通知,发生异常之后

@Around,环绕通知

spring配置文件 有mybatis配置链接数据库

<?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: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/context https://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="servicey业务层的完整包名"/>

    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
         <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
         <property name="url" value="jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai"/>
         <property name="username" value="root"/>
         <property name="password" value="root"/>
         <!--根据你的并发量来评估-->
         <property name="maxActive" value="10"/>
         <property name="minIdle" value="5"/>
         <property name="initialSize" value="5"/>
         <property name="maxWait" value="5000"/>
    </bean>

    <!--把mybatis配置文件中的内容整合spring中,
    spring封装了一个类SqlSessionFactoryBean 类中的属性对应为mybatis配置文件中标签名-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="datasource"/>
         <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="basePackage" value="dao层的完整包名"/>
    </bean>
</beans>

使用事务的步骤:

(1)由spring创建事物管理

<!--①事物管理 理解为事物切面-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="datasource"/>
</bean>

(2)开启事物注解

<!--②开启事物注解
       transaction-manager:可以省略 但是前提是你的事物管理的id必须是transactionManager

    http://www.springframework.org/schema/tx
-->
<tx:annotation-driven transaction-manager="transactionManager"/>

(3) 在需要使用事物管理的上面加入注解

@Transactional
public void purchase(String isbn, String username)  {
       //业务代码
      //1.根据书得编号查询书得价格
    int price = bookShopDao.findBookPriceByIsbn(isbn);
    //2.查询指定书得库存
    int stock = bookShopDao.findBookStockByIsbn(isbn);
    if(stock>0) {
        //2.1. 修改库存数量
        bookShopDao.updateBookStock(isbn);
    }else{
        throw new RuntimeException("编号为"+isbn+"的库存不足");
    }
    //3.查询指定用户的余额
    int balance = bookShopDao.findAccountBalanceByUsername(username);
    if(balance>=price){
        //3.1.扣款
        bookShopDao.updateAccount(username,price);
    }else{
         throw new RuntimeException("账号为"+username+"的余额不足");
    }

}

2事物得传播行为

在这里插入图片描述

基于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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.ykq.service"/>

    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
         <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
         <property name="url" value="jdbc:mysql://localhost:3306/transaction?serverTimezone=Asia/Shanghai"/>
         <property name="username" value="root"/>
         <property name="password" value="root"/>
         <!--根据你的并发量来评估-->
         <property name="maxActive" value="10"/>
         <property name="minIdle" value="5"/>
         <property name="initialSize" value="5"/>
         <property name="maxWait" value="5000"/>
    </bean>

    <!--把mybatis配置文件中的内容整合spring中,
    spring封装了一个类SqlSessionFactoryBean 类中的属性对应为mybatis配置文件中标签名-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="datasource"/>
         <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="basePackage" value="com.ykq.dao"/>
    </bean>
    <!--①事物管理 理解为事物切面-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="datasource"/>
    </bean>

    <!--配置事物得属性 必须规范-->
    <tx:advice id="myadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置切面-->
    <aop:config>
          <aop:pointcut id="pointcut" expression="execution(* com.ykq.service.*.*(..))"/>
          <aop:advisor advice-ref="myadvice" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

标签:username,事务,isbn,书得,int,price,----,bookShopDao,AOP
来源: https://blog.csdn.net/m0_46938055/article/details/121946511

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有