https://blog.csdn.net/RicardoDing/article/details/79899686

近期,在使用spring和mybatis框架编写代码时,sqlSession不需要手动关闭这一点引起了我的兴趣。我们都知道,单独使用mybatis时,sqlSeesion使用完毕后是需要进行手动关闭的,但为什么在和spring整合后就不需要了呢?在查阅了资料后得知,这是使用了spring中的AOP面向切面编程和动态代理技术。但是我认为脱离代码谈技术就是耍流氓。因此,我对 MyBatis-Spring 整合包的源码进行了简单的探究,水平有限,如有错漏,请多指教
  首先,我们来编写一段简单的原始DAO开发的代码
  mybatisConfig.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>
    <settings>
        <!-- 懒加载设置为 true -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 积极加载设置为false -->
        <setting name="aggressiveLazyLoading" value="false"/>
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <!-- 自定义别名 -->
    <typeAliases>
        <package name="com.po"/>
    </typeAliases>
    <!-- 加载映射文件 -->
    <mappers>
        <mapper resource="sqlMap/StudentMapper.xml"/>
        <!-- 批量加载映射文件 -->
        <package name="com.mapper"/>
    </mappers>
</configuration>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

这里插一段题外话,在配置文件中踩到了一个小坑,就是<mappers>节点下没办法同时使用<mapper>和<package>节点的问题,编译器一直报错,最后查询dtd约束才发现,配置文件要求先使用<mapper>节点,再使用<package>节点,顺序混乱就会报错
  spring配置文件applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描加载classpath路径下的所有properties文件,classpath路径就是target/classes文件夹下的路径-->
    <context:property-placeholder location="classpath*:*.properties" />
    <!--配置数据源,数据库连接池使用阿里巴巴的druid连接池-->
    <bean );
            }

            session.close();
        }

    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

  因此,我们最终得出结论,spring整合mybatis之后,通过动态代理的方式,使用SqlSessionTemplate持有的sqlSessionProxy属性来代理执行sql操作(比如下面SqlSessionTemplate类的insert方法)

    public int insert(String statement) {
        return this.sqlSessionProxy.insert(statement);
    }

    1
    2
    3

  最终,insert方法执行完操作后,会执行finally里面的代码对sqlSeesion进行关闭,因此,spring整合mybatis之后,由spring管理的sqlSeesion在sql方法(增删改查等操作)执行完毕后就自行关闭了sqlSession,不需要我们对其进行手动关闭
  本次探寻源码也到此位置了,这也是我第一次认真开始写博客(我更喜欢记笔记),以后我还是会尽量多写博客。本文原创,转载请和本人联系
---------------------
作者:旋律秋凉
来源:CSDN
原文:https://blog.csdn.net/RicardoDing/article/details/79899686
版权声明:本文为博主原创文章,转载请附上博文链接!

相关文章:

  • 2022-01-08
  • 2019-11-05
  • 2022-02-20
  • 2021-11-19
  • 2021-06-17
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
猜你喜欢
  • 2021-04-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
相关资源
相似解决方案