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>