ssm框架复习巩固

ssm框架复习的第七天:AOP注解开发  Spring的事务管理

1.AOP注解开发的步骤:

1.导入依赖

     <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
<-Aspectjweaver依赖-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

2.通知类和目标类

/*
    日志开发的业务代码
 */
@Component
@Aspect
public class LogUtils {
    public void printLog(){
        System.out.println("记录日志.....");
    }
    //切点表达式
    @Pointcut("execution(* com.swlz.service..*(..))")
    public void pc(){

    }

    //    方法执行之前
    @Before("pc()")
    public void beforeLog(){
        System.out.println("记录日志  ...beforeLog");
    }
//    方法执行之后
   @AfterReturning("pc()")
    public void afterReturingLog(){
        System.out.println("记录日志  ...afterReturingLog");
    }
//    方法出现异常的时候
    @AfterThrowing("pc()")
    public void afterThrowingLog(){
        System.out.println("记录日志 ...afterThrowingLog");
    }
//    所有方法执行完之后,也就是最终
    @After("pc()")
    public void afterLog(){
        System.out.println("记录日志 ...afterLog");
    }
}
package com.swlz.service.impl;


import com.swlz.service.IUserService;
import org.springframework.stereotype.Service;

/**
 * 用户基本增删改查的业务代码
 */
@Service("userService")
public class UserServiceImpl implements IUserService {
    public void findAll() {
        System.out.println("UserServiceImpl 调用Dao查询所有用户");
    }
    public void saveUser(String user) {

        System.out.println("UserServiceImpl 调用Dao保存用户" + user);

    }
    public void updateUser() {

        System.out.println("UserServiceImpl 调用Dao更新用户");
    }
    public void deleteUser() {

        System.out.println("UserServiceImpl 调用Dao删除用户");
    }

}

3.配置类

@Configuration
@ComponentScan(basePackages = "配置包扫描,例如:com.swlz")
@EnableAspectJAutoProxy
public class ApplicationConfig {

}

4.测试类

    @Test
    public void testAOP(){
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        IUserService userService = context.getBean("userService", IUserService.class);
        userService.findAll();
    }

2.声明式事务管理

Spring的声明式事物管理

1. jdbc事务

1. 为了保证程序在运行过程中数据的正确性

例子:转账

1. 向一个账号减钱

2. 向另外一个账户加钱

事物的四大特性  

 ACID

1. 原子性

事物的多个操作要么同时执行成功,要么同时执行失败

2. 一致性

事物在执行前后数据要保持一致

3. 隔离性(很难得到保证)

 多个事物在同时执行的时候,相互不能影响

 事务的隔离级别

1.未提交读(读未提交)

2.提交读(读已提交)

3.可重复读

4.串性化(可序列化)

4. 持久性

事务一旦提交,数据就会持久保存到数据库

 jdbc事物控制(多个操作必须使用同一个连接对象)

1. connection

  

2. jdbc事物控制(多个操作必须使用同一个连接对象)

1. connection

  1. setAtuoCommit(false)  开启事务 设置事务手动提交 默认是自动提交

  2. commit();  提交事务

  3. rollback(); 回滚事务

用户一次请求对应一个线程

一个用户的请求---对应一个线程----对应一个数据库连接(对象) 

用户的请求 使用 当前线程 当前连接 使用同一个线程和同一个连接对象

TreadLocal类(底层结构是一个map,key:Thread,value:connection):用来绑定当前线程的

对应一个线程--对应一个数据库连接(对象) 

3.声明式事物管理基于xml配置

spring基于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: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/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">
<!--Spring 声明式事务配置-->
<!--第一步:配置Spring的事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--第二步: 配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--第四步,在通知中配置哪些方法需要被事物控制-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
<tx:method name="find*" propagation="SUPPORTS" isolation="REPEATABLE_READ" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--第三步配置aop-->
<aop:config>
<!--配置切点-->
<aop:pointcut id="pt" expression="execution(* com.bianyiit.service..*(..))"/>
<!--配置切面 = 切点 + 通知-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>

今日学习感受:最后一句话:现在拼命学习的你,一定是未来的你在想你求救!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值