Spring

1.什么是Spring?----> 简化开发!

the source for modern java 现代化的java开发   
  • spring是开源的免费的容器。
  • spring是一个轻量级的,非入侵式的框架。
  • 控制反转(IOC),面向切面编程 (AOP)。
  • 支持事务处理,对框架整合的支持。

核心:IOC和AOP

目的:简化开发,降低耦合性

Spring如何简化开发?

为了降低Java开发的复杂性,Spring采用了以下4种关键策略:

1、基于POJO的轻量级和最小侵入性编程,所有东西都是bean;

2、通过IOC,依赖注入(DI)和面向接口实现松耦合;

3、基于切面(AOP)和惯例进行声明式编程;

4、通过切面和模版减少样式代码,RedisTemplate,xxxTemplate;

官方下载: https://repo.spring.io/release/org/springframework/spring/

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

Spring七大组件

在这里插入图片描述

二、IOC、DI、AOP

总结:IOC/DI 无论要什么对象,都可以直接去SpringIOC容器中获取,而不需要自己操作(new、setXxx())

1.IOC 控制反转

在配置文件加载的时候,所有的bean就已经被实例化了,要用直接去get,就可以了
内存中只有一份实例

将创建对象、属性值的方式 进行了反转,从new、setXxx()反转为 从SpringIOC容器getBean()

(1)对象由spring进行创建、管理、装配

  (2)  IOC3种创建对象的方式

a.构造函数索引

<bean id="user" class="com.hardy.pojo.User">
    <constructor-arg index="0" value="hardy"/>
</bean>

b.参数类型匹配【不建议使用,string类型会冲突】

<bean id="user" class="com.hardy.pojo.User">
    <constructor-arg type="java.lang.String" value="hardy"/>
</bean>

c.参数名匹配

<bean id="user" class="com.hardy.pojo.User">
     <constructor-arg name="name" value="hardy3"/>
</bean>

 

2.DI 依赖注入

本质:就是利用set方法来进行注入,将属性值注入给了属性。将属性注入给了bean,将bean注入给了ioc容器

(1)构造器注入

(2)set、get注入

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

(3)p命名空间注入
            引入p命名空间
                 xmlns:p="http://www.springframework.org/schema/p"
            简单类型:
                p:属性名="属性值"
            引用类型:
                p:属性名-ref="引用的id"
            注意:多个p赋值的时候要有空格
            注意:无论是String还是Int/short/long在赋值时都是value="值",因此建议此种情况 需要配合 name\type进行区分

复杂类型的依赖注入

查看代码
<?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">

    <bean id="address" class="com.hardy.pojo.Address">
        <property name="address" value="郑州"/>
    </bean>

    <bean id="student" class="com.hardy.pojo.Student">
        <!-- 第一种,普通值注入  value-->
        <property name="name" value="hardy"/>
        <!-- 第二种,bean注入  ref-->
        <property name="address" ref="address"/>
        <!-- 第三种,数组注入-->
        <property name="books">
            <array>
                <value>三国演义</value>
                <value>水浒传</value>
                <value>红楼梦</value>
                <value>西游记</value>
            </array>
        </property>
        <!-- 第四种,list集合注入-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>看书</value>
                <value>学Java</value>
            </list>
        </property>
        <!-- 第五种,map注入-->
        <property name="card">
            <map>
                <entry key="校园卡" value="201112084"/>
                <entry key="银行卡" value="2637643487362476376"/>
            </map>
        </property>
        <!-- 第六种,set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>梦幻西游</value>
                <value>绝地求生</value>
            </set>
        </property>
        <!-- 第七种,null值注入-->
        <property name="wife">
            <null/>
        </property>
        <!-- 第八种,Properties 特殊值注入-->
        <property name="info">
            <props>
                <prop key="driver">201112084</prop>
                <prop key="url">hardy</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>
</beans>

测试

public class MyTest {
    public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Student student = (Student) context.getBean("student");

        System.out.println(student.toString());
        /**
         * Student{
         *  name='hardy',
         *  address=Address{address='郑州'},
         *  books=[三国演义, 水浒传, 红楼梦, 西游记],
         *  hobbys=[听歌, 看书, 学Java],
         *  card={
         *      校园卡=201112084,
         *      银行卡=263764348736246},
         *  games=[LOL, 梦幻西游, 绝地求生],
         *  wife='null',
         *  info={
         *      password=root,
         *      url=hardy,
         *      driver=201112084,
         *      username=root}}
         */
    }
}

3. AOP 面向切面编程

横切关注点:跨越应用程序多个模块的方法或功能。即:与我们逻辑无关的,但是我们需要专注的部分,就是横切关注点。如:日志,安全,缓存,事务等等
切面(aspect):横切关注点被 模块化 的特殊对象。即:它是一个类
通知(advice):切面必须要完成的工作。即:它是类中的一个方法
目标(target):被通知的对象
代理(proxy):向目标对象应用通知之后创建的对象
切入点(PointCut):切面通知 执行的 “地点” 的定义
连接点(joinPoint):与切入点匹配的执行点


3.1使用spring的API接口

(1)、使用前需要导入aop的依赖:

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>
</dependencies>

(2).编写通知类

public class Log implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(method.getClass().getName()+"类,执行了"+method.getName()+"方法");
    }
}

public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法"+returnValue);
    }
}

(3).配置xml ==>在切入点切入类的引用

    <!--注册bean    -->
    <bean id="userService" class="com.hardy.service.UserServiceImpl"/>
    <bean id="log" class="com.hardy.log.Log"/>
    <bean id="afterLog" class="com.hardy.log.AfterLog"/>

    <!--方式一:  使用原生spring API接口    -->
    <!--配置aop-->
    <aop:config>
        <!--切入点   expression:表达式   execution(要执行的位置! *  *  *  * ) 第一个*表示方法类型-->
        <aop:pointcut id="pointcut" expression="execution(* com.hardy.service.UserServiceImpl.*(..))"/>

        <!--  执行环绕增加      -->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

(4).测试

public class MyTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是UserService接口
        UserService userservice = (UserService) context.getBean("userService");

        userservice.query();
    }
}

3.2使用自定义类实现AOP

3.2.1自定义log类

public class DiyPointCut {
    public void before(){
        System.out.println("========方法执行前=========");
    }
    public void after(){
        System.out.println("========方法执行后=========");
    }
}

public class DiyPointCut {
    public void before(){
        System.out.println("========方法执行前=========");
    }
    public void after(){
        System.out.println("========方法执行后=========");
    }
}

3.2.2xml配置==>在切入点切入类的方法

    <!--注册bean    -->
    <bean id="userService" class="com.hardy.service.UserServiceImpl"/>

    <!--方式二:  自定义类    -->
    <bean id="diy" class="com.hardy.diy.DiyPointCut"/>

    <aop:config>
        <aop:aspect ref="diy">
            <!-- 切入点-->
            <aop:pointcut id="point" expression="execution(* com.hardy.service.UserServiceImpl.*(..))"/>
            <!--  切面 -->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

3.2.3 测试

public class MyTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是UserService接口
        UserService userservice = (UserService) context.getBean("userService");

        userservice.query();
    }
}

3.3使用注解实现AOP

3.3.1自定义一个切面类

查看代码
//方式三:使用注解方式实现AOP
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.hardy.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("========方法执行前=========");
    }
    @After("execution(* com.hardy.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("========方法执行后=========");
    }

    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.hardy.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("========环绕前=========");

        Signature signature = pj.getSignature();//获得签名
        System.out.println("signature:"+signature);//打印调用的方法
        //执行方法
        Object proceed = pj.proceed();

        System.out.println("========环绕后=========");
    }
}

3.3.2配置xml

    <!--注册bean    -->
    <bean id="userService" class="com.hardy.service.UserServiceImpl"/>

    <!--方式三:  使用注解实现   -->
    <bean id="annotationPointCut" class="com.hardy.diy.AnnotationPointCut"/>
    <!--开启注解支持    -->
    <aop:aspectj-autoproxy/>

3.3.3测试

public class MyTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是UserService接口
        UserService userservice = (UserService) context.getBean("userService");

        userservice.query();
    }
}

结果:环绕在最外层最早和最晚执行

三、Spring配置

1..bean的相关配置

(1)bean的6个作用域

    a、singleton:全局只能有1个 【Spring的默认机制】,支持延迟加载(懒加载 @Lazy),用到时才会创建

     对应单例模式的饿汉式(对应懒加载)和饱汉式

查看代码
<!--C命名空间注入,通过有参构造器注入,constructor  -->
    <bean id="user2" class="com.hardy.pojo.User" c:name="张三" c:age="23" scope="singleton"/>
    
    @Test
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
        User user = (User) context.getBean("user2",User.class);
        User user2 = (User) context.getBean("user2",User.class);
        System.out.println(user==user2);
    }
    //结果为:true


    b、prototype:每一个变量都有一个自己的 -->原型模式:每次获取都会产生新对象

查看代码
<!--C命名空间注入,通过有参构造器注入,constructor  -->
    <bean id="user2" class="com.hardy.pojo.User" c:name="张三" c:age="23" scope="prototype"/>

	@Test
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
        User user = (User) context.getBean("user2",User.class);
        User user2 = (User) context.getBean("user2",User.class);
        System.out.println(user==user2);
    }
    //结果为:false


     c、request   以下的只能在web中使用
    d、session
    e、application
    f、websocket

(2)bean的自动装配

  • xml与注解:
    • xml 更加万能,适用于任何场合,维护简单方便
    • 注解 不是自己的类使用不了,维护相对复杂
  • xml与注解的最佳实践:
    • xml用来管理 bean
    • 注解只负责完成属性的注入
    • 生产过程中:唯一需要注意,想让注解生效,就必须开启注解的支持
  • 自动装配是spring满足bean依赖的一种方式
  • spring会在上下文中自动寻找,并自动给bean装配属性

XML型Bean自动装配

(一)byName:需要保证所有bean的ID唯一,并且这个bean需要和自动注入的属性的set方法的值一致

        <bean id="cat" class="com.hardy.pojo.Cat"/>
        <bean id="dog222" class="com.hardy.pojo.Dog"/>

        <!--  byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的  bean id
              byType:会自动在容器上下文中查找,和自己对象类型相同的  bean id
              必须保证这个类型全局唯一  不然xml中直接报错
        -->
        <bean id="people" class="com.hardy.pojo.People" autowire="byName">
            <property name="name" value="张三" />
        </bean>

(二)byType:需要保证所有bean的class唯一,并且这个bean需要和自动注入的类型一致。

        <bean id="cat" class="com.hardy.pojo.Cat"/>
        <bean id="dog222" class="com.hardy.pojo.Dog"/>
        
        <!--  byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的  bean id
              byType:会自动在容器上下文中查找,和自己对象类型相同的  bean id
              必须保证这个类型全局唯一  不然xml中直接报错
        -->
        <bean id="people" class="com.hardy.pojo.People" autowire="byType">
            <property name="name" value="张三" />
        </bean>

(三)注解型实现自动装配

1.注册bean

@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的< beans>,作用为:配置spring容器(应用上下文),
@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的< bean>,作用为:注册bean对象。

@Component 有几个衍生的注解,我们在web开发中,会按照mvc三层架构分层
        dao 【@Repository】
        service 【@Service】
        controller 【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到spring容器中,装配bean

2.注入bean

2.1基于Spring的注解

@Autowired//自动注入bean,通过类型,名字,默认true,不允许为空

@Autowired(required = false)//说明这个对象可以为null,否则不允许为空,
在属性上个使用,也可以在set上使用
我们可以不用编写set方法,前提是你自动装配的属性在IOC容器中,且符合名字byname

如果@Autowired自动装配环境比较复杂。自动装配无法通过一个注解完成的时候

@Qualifier(value = “dog”)去配合使用,指定一个唯一的id对象

源码分析:@Autowired注入首先根据byType注入,当类型大于1时在根据byName【实质byId】注入

@Scope("prototype") //作用域,可以加载实体类上

@Value("hardy") //对实体类属性注入

@Nullable:字段标记了这个注解,说明这个字段可以为null

2.2 基于Java的注解(JDK1.5之后提供的注解,由JDK提供)

@Resource 注解:自动装配通过名字,类型,默认根据名字

        @Resource(name="stuDao2")    根据名字
       @Resource(type=Student.class)根据类型

public class People {

    @Autowired
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog222")
    private Dog dog;
    private String name;
}

@Resource和@Autowired的区别:
      1、都是用来自动装配的,都可以放在属性字段上
      2、@Autowired 默认通过byType的方式实现
      3、@Resource 默认通过byname的方式实现,如果找不到名字,则通过byType方式实现
      4、执行顺序不同:@Autowired 默认通过byType的方式实现,@Resource 默认通过byname的方式实现

添加注解支持和头文件引用

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

    <!--开启注解支持-->
    <context:annotation-config/>
    
    <!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.hardy.dao"/>

</beans>

 

 Spring的三种装配模式:

1.在xml中显示的配置
2.在Java中显示的配置
3.隐式的自动装配bean【重要】

 

2.使用Java方式配置Spring-->注解

1.配置类

查看代码
@Configuration
/**
 * 在一个类上,加上Configuration 这个类就变成了配置类
 * Configuration也会被spring托管,因为他本身就是一个component
  */
@ComponentScan("com.hardy")
@Import(HardyConfig2.class)
public class HardyConfig {

    //注册一个bean  id 就是方法名  class属性  就是方法的返回值
    @Bean
    public User getUser(){
        return new User();
    }
}

2.实体类

查看代码
@Component
public class User {
    private  String name;

    public String getName() {
        return name;
    }
    
    @Value("hardy")
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

3.测试类

public class MyTest {
    public static void main(String[] args) {
        //如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig 上下文来获取容器,
        // 通过配置类的class对象加载。
        ApplicationContext context = new AnnotationConfigApplicationContext(HardyConfig.class);
        User user = (User) context.getBean("getUser");//配置类的id名就是方法名
        System.out.println(user.getName());
    }
}

动态代理与静态代理

动态代理代理的是接口,静态代理代理的是实体类

 静态代理

在这里插入图片描述

角色分析:

  • 抽象角色:一般会使用接口或者抽象类来解决【接口Rent】
  • 真实角色:被代理的角色【接口实现类Host】-->房东
  • 代理角色:代理别人的角色,里面处理一些业务【接口实现类Proxy】-->中介
  • 客户:访问代理对象的人

代理模式的好处:

  • 可以使真实角色的操作更加纯粹,不用去关注一些公共的业务
  • 公共业务交给代理角色,实现业务分工
  • 公共业务发生扩展时,方便集中管理

缺点:

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

代码实现:

1、Rent . java 即抽象对象--租房

public interface Rent {
   public void rent();
}

2、Host . java 即真实角色--房东(要出租)

public class Host implements Rent{
   public void rent() {
       System.out.println("房屋出租");
  }
}

3、Proxy . java 即代理角色--中介

public class Proxy implements Rent {

   private Host host;
   public Proxy() { }
   public Proxy(Host host) {
       this.host = host;
  }

   //租房
   public void rent(){
       seeHouse();
       host.rent();
       fare();
  }
   //看房
   public void seeHouse(){
       System.out.println("带房客看房");
  }
   //收中介费
   public void fare(){
       System.out.println("收中介费");
  }
}

4、Client . java 即客户--房客

public class Client {
   public static void main(String[] args) {
       //房东要租房
       Host host = new Host();
       //中介帮助房东
       Proxy proxy = new Proxy(host);

       //你去找中介!
       proxy.rent();
  }
}

动态代理

  • 动态代理的代理类是动态生成的,不是我们直接写好的
  • 动态代理分为两大类:基于接口的动态代理、基于类的动态代理
    - 基于接口----JDK动态代理
    - 基于类:cglib
    - java字节码实现:javasist 【常用】

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

  • proxy这个类用来动态生成代理对象
  • InvocationHandler用来处理业务

InvocationHandler:是有代理实例实现的调用处理程序

      Object invoke(Object proxy, 方法 method, Object[] args);
               //参数
               //proxy - 调用该方法的代理实例 
               //method -所述方法对应于调用代理实例上的接口方法的实例。方法对象的声明类将是该方法声明的接口,它可以是代理类继承该方法的代理接口的超级接口。
               //args -包含的方法调用传递代理实例的参数值的对象的阵列,或null如果接口方法没有参数。原始类型的参数包含在适当的原始包装器类的实例中,例java.lang.Integer           

代码实现:

1、Rent . java 即抽象对象--租房

public interface Rent {
   public void rent();
}

2、Host . java 即真实角色--房东(要租房)

public class Host implements Rent{
   public void rent() {
       System.out.println("房屋出租");
  }
}

3、ProxyInvocationHandler. java 即代理角色--中介(核心!)

public class ProxyInvocationHandler implements InvocationHandler {
   private Rent rent;

   public void setRent(Rent rent) {
       this.rent = rent;
  }

   //生成代理类,重点是第二个参数,获取要代理的抽象角色!之前都是一个角色,现在可以代理一类角色
   public Object getProxy(){
       return Proxy.newProxyInstance(this.getClass().getClassLoader(),
               rent.getClass().getInterfaces(),this);
  }

   // proxy : 代理类 method : 代理类的调用处理程序的方法对象.
   // 处理代理实例上的方法调用并返回结果
   @Override
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
       seeHouse();
       //核心:本质利用反射实现!
       Object result = method.invoke(rent, args);
       fare();
       return result;
  }

   //看房
   public void seeHouse(){
       System.out.println("带房客看房");
  }
   //收中介费
   public void fare(){
       System.out.println("收中介费");
  }

}

4、Client . java 真实角色--租客

public class Client {

   public static void main(String[] args) {
       //真实角色
       Host host = new Host();
       //代理实例的调用处理程序
       ProxyInvocationHandler pih = new ProxyInvocationHandler();
       pih.setRent(host); //将真实角色放置进去!
       Rent proxy = (Rent)pih.getProxy(); //动态生成对应的代理类!
       proxy.rent();
  }

}

 

动态代理的好处:

  • 可以使真实角色的操作更加纯粹,不用去关注一些公共的业务
  • 公共业务交给代理角色,实现业务分工
  • 公共业务发生扩展时,方便集中管理
  • 一个动态代理类代理的一个接口,一般就是对应的一类业务
  • 一个动态代理类可以代理多个类,只要是实现了同一个接口即可

四、Spring整合Mybatis

4.1导入依赖

4.1.1dependency依赖
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.19.RELEASE</version>
        </dependency>
        <!--spring操作数据库的话,还需要spring-jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>
4.1.2maven资源导出问题
<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

4.2编写配置文件

4.3测试

4.4 mybatis

4.4.1编写实体类

@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}

4.4.2编写核心配置文件 -->   mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration>
    
    <typeAliases>
        <package name="com.hardy.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?userSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper class="com.hardy.mapper.UserMapper"/>
    </mappers>
</configuration>

4.4.3 编写接口

public interface UserMapper {
    List<User> selectUser();
}

4.4.4编写Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.hardy.mapper.UserMapper">
        <select id="selectUser" resultType="user">
            select * from mybatis.user;
        </select>
</mapper>

4.4.5测试

    @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sessionFactory.openSession(true);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectUser();
        for (User user : users) {
            System.out.println(user);
        }
    }

4.4.6 mybatisUtils工具类

//sqlSessionFactory -->sqlSession
public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try{
            //使用mybatis第一步:获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";//注意
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //
    public static SqlSession getSqlSession(){
         return sqlSessionFactory.openSession(true);
    }

}

4.5  Mybatis-spring

1.编写数据源配置
2.sqlSessionFactory
3.sqlSessionTemplete

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

    <!--Datasource:使用spring的数据源替换mybatis的配置  c3p0 dbcp druid
        我们这里使用spring提供的jdbc:org.springframework.jdbc.datasource
    -->
    <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?userSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    
    <!--创建sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--bound mybatis-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--注册usermapper-->
        <property name="mapperLocations" value="classpath:com/hardy/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate,就是我们使用的sqlsession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--通过构造器给SqlSessionTemplate 传入参数   它需要一个sqlSessionFactory
        且它没有set方法,只能用构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

4.给接口添加实现类

//给接口添加实现类
public class UserMapperImpl implements UserMapper{

    //原来我们所有操作都使用sqlsession,现在所有都使用SqlSessionTemplate,他俩一样
    private SqlSessionTemplate sqlSession;

    //spring万物皆注入  一定要来个set方法
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List<User> selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        return  mapper.selectUser();
    }
}

5.实现类注入bean

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

    <import resource="spring-dao.xml"/>

    <!--将实习类 注入到spring中-->
    <bean id="userMapper" class="com.hardy.mapper.UserMapperImpl">
        <!--UserMapperImpl 类中需要SqlSessionTemplate这个参数,传入      -->
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
</beans>

6.测试

    @Test
    public void test() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = (UserMapper) context.getBean("userMapper");
        for (User user : mapper.selectUser()) {
            System.out.println(user);
        }
    }

4.5.2  方式二:SqlSessionDaoSupport

在这里插入图片描述

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

    <!--Datasource:使用spring的数据源替换mybatis的配置  c3p0 dbcp druid
        我们这里使用spring提供的jdbc:org.springframework.jdbc.datasource
    -->
    <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?userSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--创建sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--bound mybatis-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--注册usermapper-->
        <property name="mapperLocations" value="classpath:com/hardy/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate,就是我们使用的sqlsession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--通过构造器给SqlSessionTemplate 传入参数   它需要一个sqlSessionFactory
        且它没有set方法,只能用构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

2.实现类

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    @Override
    public List<User> selectUser() {
//        SqlSession sqlSession = getSqlSession();
//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        return mapper.selectUser();
        return getSqlSession().getMapper(UserMapper.class).selectUser();
    }
}

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

    <import resource="spring-dao.xml"/>

    <!--方式二:将实现类 注入到spring中-->
    <bean id="userMapper2" class="com.hardy.mapper.UserMapperImpl2">
        <!--UserMapperImpl 类中需要SqlSessionTemplate这个参数,传入      -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

4.测试

    @Test
    public void test() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = (UserMapper) context.getBean("userMapper2");
        for (User user : mapper.selectUser()) {
            System.out.println(user);
        }
    }

区别方式一与方式二:

1.都是引用了sqlSessionTemplete对象

2.方式一直接引用类对象;方式二直接是继承父类方法

五、Spring中的事务

增删改需要事务,查询不需要(查询设置为只读)

  • 声明式事务:AOP 【交由容器管理事务】
  • 编程式事务:需要在代码中,进行事务的管理 【需要改变代码】

5.1 spring中的七种事务传播属性:propagation 传播

required(依赖):支持当前事务,如果当前没有事务,就新建一个事务 【默认】
supports(支持):支持当前事务,如果当前没有事务,就以非事务的方式执行
mandatory(强制):支持当前事务,如果当前没有事务,就抛出异常
required_new:新建事务,如果当前存在事务,把当前事务挂起
not_supported:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
never:以非事务方式执行,如果当前存在事务,则抛出异常
nested(嵌套):支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务
applicationContext.xml配置事务

<!--结合AOP实现事务的织入-->
    <!--配置事务的类:spring-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--给哪些方法配置事务-->
        <!--配置事务的传播特性:new-->
        <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>

案例

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--Datasource:使用spring的数据源替换mybatis的配置  c3p0 dbcp druid
        我们这里使用spring提供的jdbc:org.springframework.jdbc.datasource
    -->
    <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?userSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--创建sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--bound mybatis-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--注册usermapper-->
        <property name="mapperLocations" value="classpath:com/hardy/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate,就是我们使用的sqlsession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--通过构造器给SqlSessionTemplate 传入参数   它需要一个sqlSessionFactory
        且它没有set方法,只能用构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <!--配置声明式事务  官网的-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--有属性可以用 property 赋值,没有可以用构造器-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--结合AOP实现事务的织入-->
    <!--配置事务的类:spring-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--给哪些方法配置事务-->
        <!--配置事务的传播特性:new-->
        <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(* com.hardy.mapper.*.*(..))"/>
        <!--切入    txAdvice包下的所有方法 都会编织上事务   -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

</beans>

 

posted @ 2022-04-19 11:50  小吴dnd  阅读(32)  评论(0编辑  收藏  举报