Spring学习笔记

1、Spring

1.1、简介
  • Spring:春天 ---->给软件行业带来了春天!

  • 2002,首次推出了Spring框架的雏形:Interface21框架

  • Spring框架即以Interface21框架为基础,经过重新设计,并不断丰富其内含,于2004年3月24日,发布了1.0正式版

  • Rod Johnson,Spring Framework创始人,著名坐着。很难想象Rod Johnson的学历,真的让好多人大吃一惊,他是悉尼大学的博士,然而他的专业不是计算机,而是音乐学

  • spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架

  • SSH:Struct2 + Spring + Hibernate

  • SSM:SpringMVC + Spring + Mybatis

官网:https://spring.io/projects/spring-framework#overview

官方下载地址:https://repo.spring.io/ui/native/release/org/springframework/spring

GitHub:https://github.com/spring-projects/spring-framework

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.9</version>
</dependency>

1.2、 优点
  • SPring是一个开源的免费的框架(容器)
  • Spring是一个轻量级的、非入侵式的框架
  • 控制翻转(IOC),面向切面编程(AOP)
  • 支持事务的处理,对框架整合的支持

总结一句话:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!

1.3、组成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dP69TlcQ-1631344880898)(/Users/longshao/Library/Application Support/typora-user-images/image-20210911115612974.png)]

1.4、拓展

在Spring官网有这个介绍:现代化的Java开发,说白就是基于Spring的开发

img

  • Spring Boot
    • 一个快速开发的脚手架
    • 基于SpringBoot可以快速的开发单个微服务
    • 约定大于配置
  • Spring Cloud
    • SpringCloud是基于SpringBoot实现的

因为现在大多数公司都在使用SpringBoot进行快速开发,学习SpringBoot的前提,需要完全掌握Spring及SpringMVC!承上启下的作用!

弊端:发展了太久之后,违背了原来的理念!配置十分繁琐,人称“配置地狱”

2、 IOC理论推导

  1. UserDao接口
  2. UserDaoImpl实现类
  3. UserService业务接口
  4. UserServiceImpl业务实现类

在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改原代码!如果程序代码量十分大,修改一次的成本代价十分昂贵!

我们使用一个set接口实现,已经发生了革命性的变化

IOC本质

**控制反转IOC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IOC的一种方法,**也有人认为DI只是IOC的另一种说法。没有IOC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,所谓控制反转就是:获得依赖对象的方式反转了。

采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。

控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IOC容器,其实现方法是依赖注入(Dependency Injection,DI)。

3、 HelloSpring

1. 导入Spring相关jar包

注:spring需要导入commons-logging进行日志记录,我们利用maven,他会自动你那个下载对应的依赖项

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.2.6.RELEASE</version>
</dependency>
2. 编写相关代码

2.1 编写一个User实体类

public class User {
    private Integer age;
    private String name;

    public User() {
        System.out.println("User init");
    }

    public User(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

2.2 编写beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring创建对象,在spring中这些都称为bean-->
<!--
    id = 对象名
    class = new 的对象
    property 相对于给对象中的属性赋值
        name = 属性名
        value = 属性值
        ref = 引用类型的值
-->
    <bean id="user" class="com.zzxx.pojo.User">
        <property name="name" value="zhangsan"/>
    </bean>
</beans>

3.3 测试运行

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 这边也可以直接给定类型,就不用强转了
//User user = (User) context.getBean("user",User.class);
    User user = context.getBean("user");
System.out.println(hello.toString());

思考问题

  • User对象是谁创建的?

    User对象是由Spring创建的

  • User对象的属性是怎么设置的?

    User对象的属性是由Spring容器设置的

这个过程就叫控制反转:

控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的。

反转:程序本身不创建对象,而变成被动的接收对象。

依赖注入:就是利用set方法来进行注入的。

IOC是一种编程思想,由主动的编程编程被动的接收

可以通过newClassPathXmlApplicationContext去浏览一下底层源码。

现在我们彻底不用在程序中去改动代码了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓的IOC,就是对象由Spring来创建,管理,装配!

4、IOC创建对象的方式

4.1、 使用无参构造创建对象(默认)
4.2、使用有参构造创建对象
  • 下标赋值
<bean id="user" class="com.zzxx.pojo.User">
    <constructor-arg index="0" value="zhangsan"/>
</bean>
  • 通过类型(不建议使用)多个参数类型一样的时候,就不适用了
<bean id="user" class="com.zzxx.pojo.User">
    <constructor-arg type="java.lang.String" value="zhangsan"/>
</bean>
  • 直接通过参数名(建议使用)
<bean id="user" class="com.zzxx.pojo.User">
    <constructor-arg name="name" value="zhangsan"/>
</bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!

5、Spring配置

5.1、别名alias
<alias name="user" alias="USER"/>
5.2、Bean的配置
  • id:bean的唯一标识符,相当于我们学的对象名
  • class:bean对象所对应的全限定名:包名+类名
  • name:也是别名,可以起多个别名,可以用空格、逗号、分号等隔开
5.3、import

这个import,一般用于团队开发使用,它可以将多个配置文件,导入合并为一个applicationContext.xml

<import resource="beans2.xml"/>
<import resource="beans.xml"/>

然后这样就可以直接使用总的配置

6、DI依赖注入

6.1、构造器注入

前面已经介绍过了

6.2、Set方式注入【重点】
  • 依赖注入:set注入
  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中所有的属性,由容器来注入
    <bean id="address" class="com.study.pojo.Address">
        <property name="address" value="山西省"/>
    </bean>
    <bean id="student" class="com.study.pojo.Student">
<!--        普通类型-->
        <property name="name" value="小张"/>
<!--        引用类型-->
        <property name="address" ref="address"/>
<!--        数组类型-->
        <property name="book">
            <array>
                <value>西游记</value>
                <value>水浒传</value>
                <value>红楼梦</value>
            </array>
        </property>
<!--        List-->
        <property name="hobby">
            <list>
                <value>打代码</value>
                <value>读书</value>
            </list>
        </property>
<!--        Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>LOL</value>
                <value>BOB</value>
                <value>COC</value>
            </set>
        </property>
<!--        Map-->
        <property name="card">
            <map>
                <entry key="身份证" value="11111111111111111"/>
                <entry key="银行卡" value="888888888888"/>
            </map>
        </property>
<!--        null-->
        <property name="wife">
            <null></null>
        </property>
<!--        Properties-->
        <property name="info">
            <props>
                <prop key="学号">2009312146</prop>
                <prop key="班级">计算机2001</prop>
                <prop key="年级">大四</prop>
            </props>
        </property>
    </bean>
6.3、拓展方式注入
  • P命名空间
  • C命名空间
  1. 导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
  1. 编写xml
<bean id="user" class="com.study.pojo.User" p:age="18" p:name="小张"/>
<bean id="user1" class="com.study.pojo.User" c:age="20" c:name="小李"/>
6.4、bean的作用域
  1. 单例模式【默认】singleton
<bean id="user" class="com.study.pojo.User" p:age="18" p:name="小张" scope="singleton"/>
  1. 原型模式(prototype) 每次从容器中get,都会产生一个新的对象
<bean id="user" class="com.study.pojo.User" p:age="18" p:name="小张" scope="prototype"/>

7、Bean的自动装配

7.1、ByName
<bean id="cat" class="com.study.pojo.Cat"/>
<bean id="dog" class="com.study.pojo.Dog"/>
<bean id="people" class="com.study.pojo.People" autowire="byName">
    <property name="name" value="小张"/>
</bean>
  • 在容器上下文中查找,和自己对象set方法后面的值对应的bean的id
7.2、ByType
<bean class="com.study.pojo.Cat"/>
<bean class="com.study.pojo.Dog"/>
<bean id="people" class="com.study.pojo.People" autowire="byType">
    <property name="name" value="小张"/>
</bean>
  • 在容器上下文中查找,和自己对象属性类型相同的bean
7.3、注解实现自动装配

使用注解须知:

  1. 导入约束
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解支持-->
    <context:annotation-config/>
  1. 配置注解的支持
<!--开启注解支持-->
<context:annotation-config/>
@Autowired 和 @Qualifer【常用】
@Autowired
@Qualifier(value = "cat11")
private Cat cat;
@Autowired
private Dog dog;
  • 直接在实体类属性上注解
  • 查找顺序:先按类型,再按名字
  • @Qualifer(value = “ ”) 可以直接指定ByName进行匹配 与Autowired配套使用
@Resource
  1. 导入jar包
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>
  1. 使用
@Resource
private Cat cat;
@Resource(name = "dog11")
private Dog dog;
7.4、小结

@Resource与@Autowired的区别:

  • 都是自动装配的,都可以放在属性字段上
  • @Resource默认通过byName实现,找不到,则通过byType
  • @Autowired默认通过byType实现,找不到,则通过byName

8、使用注解开发

  • Spring4之后,使用注解开发,需要保证aop包导入
  • 使用注解开发需要保证Context约束,增加注解支持
8.1、bean
@Component:组件放在类上,说明这个类被Spring管理,就是Bean
@Component
public class User {
8.2、属性如何注入
@Value 
@Component
public class User {
    @Value("小张")
    private String name;
8.3、衍生的注解
  • @Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层
    • dao【@Repository】
    • service【@Service】
    • controller【@Controller】
  • 这四个功能都是一样的,都是代表某个类注册到Spring中装配Bean
8.4、自动装配置
  • @Autowired
  • @Qualifer
  • @Resource
8.5、作用域
  • @Scope
@Component
@Scope(value = "prototype")
public class User {
    @Value("小张")
    private String name;
8.6、小结

xml与注解

  • xml更加万能,适用于任何场合,维护方便简单!

  • 注解:不是自己的类是用不了的,维护相对复杂!

    xml与注解配合使用是最佳的

  • xml用来管理bean

  • 注解只负责完成属性的注入

  • 我们在使用过程中,只需要注意一个问题,必须让注解生效,开启注解支持

9、使用java的方式配置Spring

9.1、编写实体类
  • 注入值
@Component
public class User {
    @Value("小张")
    private String name;
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
9.2、编写配置类
  • @Configuration,会注册到容器中,有误他本身就是一个@Component

  • @Configuration代表这是一个配置类,等价于之前的 beans.xml

  • @bean注册一个bean,相对于之前的Bean标签

  • 方法名 相对于标签中的 id

  • 返回值 相对于标签中的class

@Configuration
@ComponentScan("com.study.pojo")
@Import(StudyConfig2.class)
public class StudyConfig {
    @Bean
    public User user(){
        return new User();
    }
}
9.3、测试
  • 注解方式使用 new AnnotationConfigApplicationContext(StudyConfig.class);
public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(StudyConfig.class);    
    User user = context.getBean("user", User.class);
    System.out.println(user.getName());
}

10、代理模式

  • SpringAOP的底层!
  • 代理模式分类:
    • 静态代理
    • 动态代理
10.1、静态代理
  • 抽象角色(租房这个事情):一般使用接口或者抽象类解决
  • 真实角色(租房的人):被代理的角色
  • 代理角色(中介):代理真实角色,代理真实角色后,我们一般会做一些附属操作
  • 客户:访问代理的人

代理模式的好处:

  • 可以使真实的角色更加纯粹!不用关心一些公共的业务
  • 公共也就交给代理角色!实现了业务的分工
  • 公共业务发送扩展的时候,方便集中管理

缺点:

  • 一个真实角色就会产生一个代理角色;代码量会翻倍 效率变低

10.2、动态代理

  • 动态代理和静态代理角色一样
  • 动态代理代理类是动态生成的,不是我们直接写好的
  • 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
    • 基于接口 JDK动态代理
    • 基于类 cglib
    • java字节码实现 javasist

需要了解两个类:Proxy:代理,invocationHandler:调用处理程序

  1. 创建代理处理程序
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyInvocationHandler implements InvocationHandler {
    private Object target;
    public void setTarget(Object target) {
        this.target = target;
    }
    public Object getProxy(){
//        新建一个代理实例
//        参数:类加载器,需要代理的类的接口,调用处理器
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }
    //处理代理实例并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        seeHost();
        Object result = method.invoke(target, args);
        contract();
        return result;
    }
    //    看房
    public void seeHost(){
        System.out.println("带客户看房");
    }
    //    签租赁合同
    public void contract(){
        System.out.println("签租赁合同");
    }
}

提取为工具类:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyInvocationHandler implements InvocationHandler {
    private Object target;
    public void setTarget(Object target) {
        this.target = target;
    }
    public Object getProxy(){
//        新建一个代理实例
//        参数:类加载器,需要代理的类的接口,调用处理器
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }
    //处理代理实例并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(target, args);
        return result;
    }
}
  1. 调用过程
public static void main(String[] args) {
    //真实对象
    HostBoss hostBoss = new HostBoss();
    //代理对象 通过动态获得
    ProxyInvocationHandler pih = new ProxyInvocationHandler();
    //需要代理的对象
    pih.setTarget(hostBoss);
    //得到代理对象
    Rent proxy = (Rent) pih.getProxy();
    proxy.rent();
}

11、AOP

11.1、使用spring实现AOP
方式一:原生的Spring API 接口
  1. 导入jar包
<!--        Aop织入-->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>
  1. 编写日志
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("after"+target.getClass().getName()+"-->"+method.getName());
    }
}
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
    //method 要执行的目标对象的方法
    //args 参数
    //target 目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("before执行了"+target.getClass().getName()+"-->"+method.getName());
    }
}
  1. 真实的对象(需要被代理的对象)
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("add一个用户");
    }
    @Override
    public void delete() {
        System.out.println("delete一个用户");
    }
    @Override
    public void update() {
        System.out.println("update一个用户");
    }
    @Override
    public void query() {
        System.out.println("query一个用户");
    }
}
  1. 编写配置文件(最重要的一步)
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.study.service.UserServiceImpl"/>
    <bean id="afterLog" class="com.study.log.AfterLog"/>
    <bean id="beforeLog" class="com.study.log.BeforeLog"/>
<!--    配置aop 需要导入约束-->
    <aop:config>
<!--        切入点     execution(修饰符 返回值 类名 方法名 参数)-->
        <aop:pointcut id="pointcut" expression="execution(* com.study.service.UserServiceImpl.*(..))"/>
<!--        执行环绕增加-->
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>
  1. 测试
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.delete();
    }
方式二:自定义来实现AOP
  1. 编写切面
public class DiyPointCut {
    public void before(){
        System.out.println("=======方法执行前=========");
    }
    public void after(){
        System.out.println("=======方法执行后=========");
    }
}
  1. 配置
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.study.service.UserServiceImpl"/>
    <bean id="diy" class="com.study.diy.DiyPointCut"/>
    <aop:config>
        <aop:aspect ref="diy">
<!--            切入点-->
            <aop:pointcut id="pointcut" expression="execution(* com.study.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
方式三:使用注解实现
  1. 编写切面类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.study.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=======方法执行前========");
    }
    @After("execution(* com.study.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=======方法执行后========");
    }
    @Around("execution(* com.study.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("环绕前");
        pj.proceed();
        System.out.println("环绕后");
    }
    @AfterReturning("execution(* com.study.service.UserServiceImpl.*(..))")
    public void AfterReturning(){
        System.out.println("AfterReturning执行");
    }
}
  1. 配置
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.study.service.UserServiceImpl"/>
    <bean id="annotation" class="com.study.diy.AnnotationPointCut"/>
<!--    开启注解支持-->
    <aop:aspectj-autoproxy/>
  1. 测试
import com.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.delete();
    }
}
  • 执行顺序:
    • 环绕前 - > before - > 目标方法执行 - > 环绕后 - > after

12、 整合Mybatis

步骤:

  1. 导入相关jar包
  • junit
  • mybatis
  • mysql数据库
  • spring相关的
  • aop织入

mybatis-spring

  1. 编写配置文件

  2. 测试

12.1、回忆Mybatis
  1. 编写实体类
  2. 编写核心配置文件
  3. 编写接口
  4. 编写Mapper.xml
  5. 测试
12.2、Mybatis-Spring
什么是Mybatis-Spring?

MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。它将允许 MyBatis 参与到 Spring 的事务管理之中,创建映射器 mapper 和 SqlSession 并注入到 bean 中,以及将 Mybatis 的异常转换为 Spring 的 DataAccessException。 最终,可以做到应用代码不依赖于 MyBatis,Spring 或 MyBatis-Spring。

  1. 编写数据源配置
  2. sqlSessionFactory
  3. sqlSessionTemplate
  4. 需要给接口加实现类
  5. 将自己写的实现类,注入到Spring中
  6. 测试使用即可

13、声明式事务

  1. 回顾事务
  • 把一组业务当成一个业务来做,要么都成功,要么都失败
  • 事务在项目开发中,十分的重要,涉及到数据的一致性问题,不能马虎
  • 确保完整性和一致性

事务的ACID原则:

  • 原子性

  • 一致性

  • 隔离性

    • 多个业务可能操作同一个资源,防止数据损坏
  • 持久性

    • 事务一旦提交,无论系统发生什么问题,结果都不会再被影响,被持久化的写到存储器中
2、Spring中的事务管理
  • 声明式事务:AOP
  • 编程式事务:需要在代码中,进行事务的管理

思考:

为什么需要事务?

  • 如果不配置事务,可能存在数据提交不一致的情况
  • 如果我们不在Spring中去配置声明式事务,我们就需要在代码中手动配置事务
  • 事务在项目的开发中十分重要,涉及到数据的一致性和完整性问题,不容马虎!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值