Spring笔记

1. Spring

1.简介

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

  • 目的:解决企业应用开发的复杂性

  • 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能

  • 范围:任何Java应用

  • Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。

导入:

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

2. 优点

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

弊端:发展了太久远,违背了原来的理念,配置十分繁琐!(配置地狱!)

3. 组成

  • 核心容器(Spring Core)

  • 应用上下文(Spring Context)

  • Spring面向切面编程(Spring AOP)

  • JDBC和DAO模块(Spring DAO)

  • 对象实体映射(Spring ORM)

  • Web模块(Spring Web)

  • MVC模块(Spring Web MVC)

4. 拓展

SpringBoot(构建一切)–> SpringCloud(协调一切)–>SpringCloudDataFlow(连接一切)

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

Spring和SpringMVC,承上启下的作用!

2. IOC理论推导

原本业务实现步骤:

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

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

使用Set接口实现,发生革命性变化

public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}
  • 之前,陈旭是主动创建对象,控制权在程序员手上
  • 使用set注入后,程序不再具有主动性,而变成了被动接收对象!

这种思想,从本质解决了问题,让我们不用再去管理对象的创建了。系统的耦合性大大降低,可以更加专注于业务的实现!这是IOC的原型!

IOC本质

  • 控制反转IOC(Inversion of control),是一种设计思想,DI(依赖注入)是实现IOC的一种方法

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

  • IOC是Spring框架的核心内容

3. HelloSpring

  1. 创建实体类

    public class Hello {
        private String str;
    
        public String getStr() {
            return str;
        }
        public void setStr(String str) {
            this.str = str;
        }
        @Override
        public String toString() {
            return "Hello{" +
                    "str='" + str + '\'' +
                    '}';
        }
    }
    
  2. 编写spring文件

    <?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
        类型 变量名 = new 类型();
        Hello hello = new Hello();
        id = 变量名
        class = new 的对象
    
        property 相当于给对象中的属性设置一个值
        bean = 对象 new Hello();
        -->
        <bean id="hello" class="org.why.pojo.Hello">
            <property name="str" value="Spring"/>
        </bean>
    </beans>
    
  3. 测试

    public  void test() {
        //解析beans.xml文件 , 生成管理相应的Bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //getBean : 参数即为spring配置文件中bean的id
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
    

思考

  • Hello 对象是谁创建的 ? hello 对象是由Spring创建的
  • Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的

这个过程就叫控制反转 :

  • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
  • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .

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

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

4. IOC创建对象的方式

  1. 使用无参构造创建对象,默认!

  2. 使用有参构造:

    1. 下标赋值

      <!--第一种:下标赋值-->
      <bean id="user" class="org.why.pojo.User">
          <constructor-arg index="0" value="why"/>
      </bean>
      
    2. 类型

      <!--第二种方式:通过类型创造,不建议使用!-->
      <bean id="user" class="org.why.pojo.User">
          <constructor-arg type="java.lang.String" value="why2"/>
      </bean>
      
    3. 参数名

      <!--第三种:直接通过参数名来设置-->
      <bean id="user" class="org.why.pojo.User">
          <constructor-arg name="name" value="why3"/>
      </bean>
      

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!

5. Spring配置

1. 别名

<!--添加别名后,原来的名字和别名都能使用-->
<alias name="user" alias="userAlias"/>

2. Bean的配置

<!--
id: bean的唯一标识符,相当于对象名
class:bean对象对应的全限定名:包名+类名
name:也是别名,而且name可以同时取多个别名(,;空格 都可以分割)
-->
<bean id="user2" class="org.why.pojo.UserT" name="user3,user3;user4 user5">
    <property name="name" value="whyT"/>
</bean>

3. Import

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

多人开发时,不同类的的注册在不同bean中,我们可以利用import将所有人的beans合并为一个总文件

  • applicationContext

    <?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
        <import resource="beans.xml"/>
        <import resource="beans2.xml"/>
        <import resource="beans3.xml"/>
    </beans>
    

    【注意:如果两个bean同名不同文件,默认情况下,spring会覆盖先前的bean!】

6. ID(依赖注入)

1. 构造器注入

​ 【见4.2!】

2. Set方式注入【重点】

  • 依赖注入:Set注入
    • 依赖:bean对象的创建依赖于容器!
    • 注入:bean对象中的所有属性,由容器来注入

【环境搭建】

  1. 复杂类型

    public class Address {
        private String address;
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
    }
    
  2. 真实测试对象

    public class Student {
        private String name;
        private Address address;
        private String[] boosl;
        private List<String> bobbys;
        private Map<String,String> card;
        private Set<String> games;
        private boolean wife;
        private Properties info;
    	...
    }
    
  3. applicationContext

    <?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="address" class="org.why.pojo.Address"/>
    
        <bean id="student" class="org.why.pojo.Student">
            <!--第一种:普通值注入,value-->
            <property name="name" value="why"/>
            <!--第二种:Bean注入,ref-->
            <property name="address" ref="address"/>
            <!--第三种:数组注入-->
            <property name="boosl">
                <array>
                    <value>语文</value>
                    <value>数学</value>
                    <value>英语</value>
                </array>
            </property>
            <!--List-->
            <property name="bobbys">
                <list>
                    <value></value>
                    <value></value>
                    <value>rap</value>
                </list>
            </property>
            <!--Map-->
            <property name="card">
                <map>
                    <entry key="身份证" value="1111"/>
                    <entry key="银行卡" value="2222"/>
                </map>
            </property>
            <!--Set-->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>DNF</value>
                </set>
            </property>
            <!--Null-->
            <property name="wife">
                <null/>
            </property>
            <!--Properties-->
            <property name="info">
                <props>
                    <prop key="学号">20190101</prop>
                    <prop key="性别"></prop>
                </props>
            </property>
        </bean>
    </beans>
    
  4. 测试类

    public class MyTest {
        @Test
        public void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Student student = (Student) context.getBean("student");
            System.out.println(student.toString());
        }
    }
    

3. 扩展方式注入

可以使用p命名空间好c命名空间进行注入!

  • 使用:

    <?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:p="http://www.springframework.org/schema/p"
           xmlns:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--p命名空间注入,可以直接注入属性的值(p就是property)-->
        <bean id="user" class="org.why.pojo.User" p:name="why" p:age="18"/>
        <!--c命名空间注入,通过构造器注入(c就是construct-args)-->
        <bean id="user2" class="org.why.pojo.User" c:age="19" c:name="why2"/>
    </beans>
    
  • 测试:

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
        User user = context.getBean("user2",User.class);
        System.out.println(user);
    }
    
  • 注意点:p命名和c命名不能直接使用,需要导入xml约束!

    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    

4. Bean Scopes(bean的作用域)

  1. singleton(单例模式)
    • 默认模式
  2. prototype(原型模式)
    • 每次从容易中get,都会产生一个新对象
  3. 其他(web)
    1. request(web-请求)
    2. session(web-会话)
    3. application(web-全局)
    4. websocket

7. Bean的自动装配

  • 自动装配是Spring满足bean依赖的一种方式
  • Spring会在上下文中自动寻找,并自动给bean装配属性

在Spring中有三种装配的方式

  1. 在xml中显示的装配
  2. 在java中显示的装配
  3. 隐式的自动装配bean【重点!】

1. 测试

环境搭建:一个人有两个宠物

2. ByName自动装配

<!--
	byName:会在容器上下文中自动查找,和自己对象set方法后面值对应的bean id
-->
<bean id="people" class="org.why.pojo.People" autowire="byName">
    <property name="name" value="why"/>
</bean>

3. ByType自动装配

<bean class="org.why.pojo.Cat"/>
<bean class="org.why.pojo.Dog"/>
<!--
	byType:会在容器上下文中自动查找,和自己对象属性类型相同的bean
-->
<bean id="people" class="org.why.pojo.People" autowire="byType">
    <property name="name" value="why"/>
</bean>

【小结】

  • byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
  • bytype的时候,需要保证所有的bean的class唯一,并且这个bean需要好自动注入的属性的类型一致!

4. 使用注解实现自动装配

  1. 导入约束:

    context约束

  2. 配置注解的支持

    <context:annotation-config/>

    <?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/>
    
    </beans>
    
  • @Autowired注解

    直接在属性上使用即可,也可以在set方法上使用

    使用Autowired可以不用编写Set方法,前提是自动装配属性在IOC(Spring)容器中存在,且符合命名规则

    科普:

    //@Nullable 	字段标记该注解,说明该字段可以为null
        
    public People(@Nullable String name) {
            this.name = name;
    }
    
    //如果显示定义了Autowired的require属性为false,说明这个对象可以为null,否则不许为空
    @Autowired(required = false)
    

    如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解无法完成时,我们可以通过使用**@Qualifier**(value = “xxx”)去配合@Autowired的使用,指定一个唯一的bean对象注入!

    @Autowired
    @Qualifier(value = "dog2")
    private Dog dog;
    
  • Resource注解

    @Resource(name = "cat2")
    private Cat cat;
    
    @Resource
    private Dog dog;
    

【小结:】

@Autowired和@Resource的区别

  • 都是用来自动装配,都可以放在属性字段上
  • @Autowired 通过byType的方式实现,并要求这个对象存在
  • @Resource 默认通过byName的方式实现,如果找不到名字,则通过byType实现;都找不到则报错
  • 执行顺序不同:@Autowired 通过byType的方式实现,@Resource 默认通过byName的方式实现

8. 使用注解开发

使用注解需要导入context约束,增加注解支持!

<?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/>
	<!--指定要扫描的包,这个包下的注解就会生效-->
	<context:component-scan base-package="org.why"/>
</beans>

注解说明:

  • @AutoWired:通过类型自动装配,如果@Autowired不能唯一自动装配上属性,需要使用**@Qualifier**(value = “xxx”)
  • @Nullable:字段使用该注解时,值可以为null
  • @Resource:通过名字自动装配,不唯一再判断类型
  • @Component:组件,放在类上,说明该类被Spring管理,等价于bean
  1. bean

  2. 属性如何注入

    //等价于<bean id="user" class="org.why.pojo.User>
    @Component //组件
    public class User {
        
        public String name;
        //相当于<property name="name" value="why"/>
        @Value("why")
        public void setName(String name) {
            this.name = name;
        }
    }
    
  3. 衍生的注解

    @Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层

    • dao【@Repository
    • service【@Service
    • controller【@Controller

    这个四个注解功能一样,都代表将某个类注册到Spring中,装配bean

  4. 自动装配置

    @AutoWired

    @Nullable

    @Resource

  5. 作用域

    @Component
    @Scope("singleton")//作用域 单例
    public class User {
    
        public String name;
        @Value("why")
        public void setName(String name) {
            this.name = name;
        }
    }
    
  6. 小结

    xml与注解:

    • xml 更加万能,适用于任何场合!维护简单方便
    • 注解 不是自己类使用不了,维护相对复

    xml与注解最佳实现:

    • xml用来管理bean
    • 注解只负责完成属性的注入

    使用注解记得开启注解支持

    <context:annotation-config/>
    <!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="org.why"/>
    

9. 使用Java的方式配置Spring

该方式完全不使用Spring的xml配置,全权交给java来做

  • 实体类
@Component  //说明该类被Spring注册到了容器中
public class User {
    private String name;

    public String getName() {
        return name;
    }
    @Value("why")
    public void setName(String name) {
        this.name = name;
    }
}
  • 配置文件
//这个也会被注册到容器中,因为它本身就是个@Component
// @Configuration代表这是个配置类,等价于beans。xml
@Configuration
@ComponentScan("org.why.pojo")//可以不写
@Import(MyConfig02.class)
public class MyConfig {
    //注册一个bean,相当于bean标签
    //该方法名字,相当于id属性
    //该方法返回值,相当于bean标签class属性
    @Bean
    public User user(){
        return new User();//返回要注入的bean对象
    };
}
@Configuration
public class MyConfig02 {
}
  • 测试类
@Test
public void test01(){
    //使用配置方法。只能通过AnnotationConfig来获取容器,通过配置类的class对象加载
    ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
    User user = context.getBean("user", User.class);
    System.out.println(user.getName());
}

这种纯java配置方式,在springboot项目中随处可见

10. 代理模式(详见23设计模式笔记)

代理模式 --------------SpringAOP底层 【AOP和MVC必问】

代理模式的分类;

  • 静态代理
  • 动态代理

1. 静态代理

角色分析:

  • 抽象角色:使用接口或抽象类来解决
  • 真实角色:被代理的角色
  • 代理角色:代理真实角色,帮真实角色做附属操作
  • 客户:访问代理对象

好处:

  • 使真实角色操作更加纯粹
  • 公共业务交个代理角色,实现了业务的分工
  • 公共业务扩展时,方便集中管理

缺点:

  • 一个真实角色就会产生一个代理角色,代码量翻倍

2. 动态代理

  • 动态代理和静态代理角色一样

  • 动态代理的代理类是动态生成,不是直接写好

  • 动态代理的实现

    • 接口:JDK动态代理
    • 类:cglib
    • 字节码:javasist【主流】

需要了解的两个类:

  • Proxy-------代理
  • InvocationHandler-----调用处理程序

好处:

  • 静态代理的所有好处
  • 动态代理类代理的是接口,对应一类业务
  • 动态代理类可以实现多个实现同一接口的类

11. AOP

1. 什么是AOP

AOP就是面向切面编程

2. AOP在Spring中的使用

提供声名事务;允许用户自定义切面

  • 横切关注点:与业务逻辑无关的方法或功能
  • 切面(aspect):横切关注点被模块化的特殊对象。即:它是一个类
  • 通知(advise):切面必须完成的工作。即:类中的方法
  • 目标(target):被通知的对象
  • 代理(proxy):想目标对象通知后创建的对象
  • 切入点(PointCut):切面通知执行的“地点”的定义
  • 连接点(JoinPoint):与切入点匹配的执行点
aop

SpringAOP中,通过定义Advice定义横切逻辑spring中支持5种类型的advise

  • Before advice:方法前通知

  • AfterReturningAdvice:方法后通知

  • MethodInterceptor :方法前后通知

  • ThrowsAdvice :有异常才会通知

  • IntroductionInterceptor :类中增加新的方法属性时通知

3. 使用Spring实现AOP

导入依赖和约束

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>
<beans xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
  • 方式一:使用Spring的API接口(SpringAPI接口的实现)
<!--方式一:使用原生Spring API接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
    <!--切入点:expression:表达式,execution(要执行的位置)-->
    <aop:pointcut id="pointcut" expression="execution(* org.why.service.UserServiceImpl.*(..))"/>

    <!--执行环绕增加!-->
    <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
  • 方式二:自定义来实现(主要是切面定义)
public class DiyPointCut {
    public void before(){
        System.out.println("==before==");
    }
    public void after(){
        System.out.println("==after==");
    }
}
<!--方式二:自定义类-->
<bean class="org.why.diy.DiyPointCut" id="diyPointCut"/>
<aop:config>
    <!--自定义切面-->
    <aop:aspect ref="diyPointCut">
        <!--切入点-->
        <aop:pointcut id="point" expression="execution(* org.why.service.UserServiceImpl.*(..))"/>
        <!--通知-->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>
  • 方式三:使用注解实现!
//方式三:使用注解方式实现AOP

@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* org.why.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("==方法前==");
    }

    @After("execution(* org.why.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("==方法后==");
    }

    @Around("execution(* org.why.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("==环绕前==");
        //获得签名
        Signature signature = joinPoint.getSignature();
        System.out.println("signature:" + signature);
        //执行方法
        joinPoint.proceed();
        System.out.println("环绕后");
    }
}
<!--方式三-->
<bean class="org.why.diy.AnnotationPointCut" id="annotationPointCut"/>
<!--开启注解支持   jdk(默认)proxy-target-class="false  cglib:proxy-target-class="false-->
<aop:aspectj-autoproxy />

12. Mybatis-Spring

步骤:

  1. 导入jar包
    • Junit
    • mybatis
    • mybatis数据库
    • spring相关
    • aop织入
    • mybatis-spring【new】
  2. 编写配置文件
  3. 测试

1. 回忆mybatis

  1. 编写实体类
  2. 编写核心配置文件
  3. 编写接口
  4. 编写mapper.xml
  5. 测试

2. mybatis-spring

  1. 编写数据源配置

    <!--DataSource:使用spring的数据源替换mybatis的配置  -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
        
    </bean>
    
  2. sqlSessionFactory

    <!--sqlSessionFactory-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis配文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:org/why/mapper/*.xml"/>
    </bean>
    
  3. sqlSessionTemplate

    <!--SqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能用构造器注入sessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sessionFactory"/>
    </bean>
    
  4. 需要给接口加实现类

    方法一:

    public class UserMapperImpl implements UserMapper {
        //原来所有操作都是用sqlSession来执行
        //现在都是用sqlSessionTemplate
        private SqlSessionTemplate sqlSession;
    
        public void setSqlSession(SqlSessionTemplate sqlSession){
            this.sqlSession = sqlSession;
        }
    
        @Override
        public List<User> selectUser() {...}
    }
    

    方法二:

    public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
        @Override
        public List<User> selectUser() {
            return getSqlSession().getMapper(UserMapper.class).selectUser();
        }
    }
    
  5. 将自己写的实现类,注入到spring中

    <bean id="userMapper" class="org.why.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
    
    <bean id="userMapper2" class="org.why.mapper.UserMapperImpl2">
        <!--继承的父类需要配置属性-->
        <property name="sqlSessionFactory" ref="sessionFactory"/>
    </bean>
    
  6. 测试使用

    @Test
    public void test1(){
        
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        List<User> userList = userMapper.selectUser();
        for (User user : userList) {
            System.out.println(user);
        }
    }
    

13. 声明式事务

1. 回顾事务

  • 把一组业务当成一个业务;要么都成功,要么都失败!
  • 涉及到数据一致性问题,十分重要
  • 确保完整性和一致性

事务的ACID原则:

  • 原则性
    • 要么都发生,要么都不发生
  • 一致性
    • 事务前后数据的完整性必须保持一致
  • 隔离性
    • 多个业务可能操作同一个资源,防止数据损坏
  • 持久性
    • 事务一旦提交,无论系统发生什么问题,结果不再被影响,被持久化写到存储器中

2. spring中的事务

  • 声明式事务:AOP
  • 编程式事务:需要在代码中,进行事务的管理(尽量不使用!!!)
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <constructor-arg ref="dataSource"/>
</bean>

<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--给哪些方法配置事务-->
    <!--配置事务的传播特效:propagation(7种),默认REQUIRED-->
    <tx:attributes>
        <tx:method name="add" propagation="REQUIRED"/>
        <tx:method name="delete" propagation="REQUIRED"/>
        <tx:method name="update" propagation="REQUIRED"/>
        <tx:method name="query" read-only="true"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>


<!--配置事务切入-->
<aop:config>
    <aop:pointcut id="txPointCut" expression="execution(* org.why.mapper.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值