Spring+MyBatisPlus+Ehcache 使用MyBatisPlus二级缓存

1、依赖及版本

        <!-- Mybatis Plus 核心库  mybatis通用代码生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.0.8</version>
            <exclusions>
                <exclusion>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-generate</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency> <!-- 开启二级缓存 -->
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.1.0</version>
        </dependency>

        <!-- spring依赖此处省略 想必大家都会 版本是5.0.2.RELEASE-->

2、Ehcache核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <!--
      maxElementsInMemory:缓存中最大允许创建的对象数
      maxInMemory:设定内存中创建对象的最大值。
      eternal:设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超时限制且元素永不消亡。
      timeToIdleSeconds:设置某个元素消亡前的停顿时间。
      timeToLiveSeconds:为元素设置消亡前的生存时间.
      overflowToDisk:设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘上。
      memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
      diskPersistent:重启时内存不持久化到硬盘。
     -->

    <diskStore path="java.io.tmpdir"/>
    <defaultCache maxElementsInMemory="10000"
                  memoryStoreEvictionPolicy="LRU"
                  eternal="false"
                  timeToIdleSeconds="300"
                  timeToLiveSeconds="300"
                  overflowToDisk="false"
                  diskPersistent="false" />

    <cache name="districtDataCache"
           maxElementsInMemory="4000"
           eternal="true"
           overflowToDisk="false"
           diskPersistent="false"
           memoryStoreEvictionPolicy="LRU"/>
</ehcache>

    <!-- 这部分配置大家根据情况配置,不懂自行百度-->

3、Spring XML配置文件整合MyBatisPlus

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- properties文件加载 -->
    <context:property-placeholder location="classpath:properties/*.properties" ignore-unresolvable="true"/>
    <!-- 数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <property name="maxWait" value="${jdbc.maxWait}"/>
    </bean>

    <!-- Mybatis sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="typeAliasesPackage" value="com.brillilab.instrument.entity.po"/>
        <property name="configuration" ref="myBatisConfiguration"/>
        <!-- MP 全局配置注入 -->
        <property name="globalConfig" ref="globalConfig"/>
        <property name="plugins">
            <array>
                <!-- 分页插件配置 -->
                <bean id="paginationInterceptor"
                      class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
                    <!-- 配置分页插件使用的方言 -->
                    <property name="dialectType" value="postgresql"/>
                </bean>
                <!-- 乐观锁插件 -->
                <!--<bean id="optimisticLockerInterceptor"-->
                      <!--class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor">-->
                <!--</bean>-->
                <!-- 性能拦截器,兼打印sql,不建议生产环境配置-->
                <bean id="performanceInterceptor"
                      class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor"/>
            </array>
        </property>
    </bean>

    <!-- MybatisConfiguration -->
    <!-- mybatis-pluse封装的CRUD使用二级缓存时 mybatis-plus 版本必须要低于2.0.9, 否则不生效,只有在mapper.xml中有的SQL方法才生效.  -->
    <bean name="myBatisConfiguration" class="com.baomidou.mybatisplus.MybatisConfiguration">
        <property name="cacheEnabled" value="true"/>
        <property name="mapUnderscoreToCamelCase" value="true"/>
    </bean>

    <!-- 定义 MP 全局策略 -->
    <bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
        <!-- 逻辑删除 定义下面3个参数-->
        <property name="sqlInjector" ref="logicSqlInjector"/>
        <property name="logicDeleteValue" value="-1"/>
        <property name="logicNotDeleteValue" value="1"/>
        <!-- 全局ID类型: 0, "数据库ID自增", 1, "用户输入ID", 2, "全局唯一ID", 3, "全局唯一ID"-->
        <property name="idType" value="0"/>
        <!-- 2.1-gamma+ 数据库自动识别,无需配置数据库类型-->
        <property name="dbType" value="postgresql" />
    </bean>

    <!-- 逻辑删除Sql注入器-->
    <bean id="logicSqlInjector" class="com.baomidou.mybatisplus.mapper.LogicSqlInjector"/>

    <!-- MapperScannerConfigurer -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.brillilab.instrument.mapper"/>
    </bean>

</beans>

4、Mapper.xml文件中的缓存设置

<mapper namespace="com.brillilab.instrument.mapper.AppointmentMapper">
    <cache type="org.mybatis.caches.ehcache.EhcacheCache" flushInterval="60000" readOnly="true">
        <property name="timeToIdleSeconds" value="3600"/>
        <property name="timeToLiveSeconds" value="3600"/>
        <property name="maxEntriesLocalHeap" value="1000"/>
        <property name="maxEntriesLocalDisk" value="10000000"/>
        <property name="memoryStoreEvictionPolicy" value="LRU"/>
    </cache>

5、使用效果

        从上面的两张图片可以看出,第一张是使用Ehcache缓存之前的测试结果,可以看出响应时间在65ms到227ms之间,相比之下,第二张图在使用了Ehcache缓存之后,响应时间有了大幅度的缩短,在10ms到20sm之间。

猜你喜欢

转载自blog.csdn.net/weweeeeeeee/article/details/81624747