zoukankan      html  css  js  c++  java
  • Mybatis-09

    Mybatis

    Mybatis缓存

      Mybatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。在Mybatis系统中默认定义了两个缓存:一级缓存、二级缓存。

    一级缓存

      一级缓存默认情况下是开启的,是SqlSession级别的缓存,即本地缓存。

    二级缓存

      二级缓存是基于namespace级别的缓存,即全局缓存,需要手动开启和配置。我们可以通过Cache接口来自定义二级缓存。

      由于当会话关闭或提交时,一级缓存将会消失,假设我们需要这个数据依旧保存,则我们需要用到二级缓存。

    开启缓存的步骤:

      首先在配置文件中:

    <!-- 开启全局缓存 -->
    <setting name="cacheEnable" value="true"/>

      接着在相应的mapper层下开启,当然也可以直接<cache/>,里面的参数都可以自定义。

    <cache
      eviction="FIFO"
      flushInterval="60000"
      size="512"
      readOnly="true"/>

    自定义缓存:Ehcache

      EhCache 是一个纯java进程内缓存框架,具有快速、精干等特点。Ehcache是一种广泛使用的开源Java分布式缓存。

      要使用它,我们首先需要导入相应的maven依赖

    <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
    <dependency>
        <groupId>org.mybatis.caches</groupId>
        <artifactId>mybatis-ehcache</artifactId>
        <version>1.0.3</version>
    </dependency>

      接着编写它的配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">
    
    <diskStore path="./tmpdir/Tmp_EhCache"/>
    
    <defaultCache
    eternal="false"
    maxElementsInMemory="10000"
    overflowToDisk="false"
    diskPersistent="false"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="259200"
    memoryStoreEvictionPolicy="LRU"/>
    
    <cache
    name="cloud_user"
    eternal="false"
    maxElementsInMemory="5000"
    overflowToDisk="false"
    diskPersistent="false"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="1800"
    memoryStoreEvictionPolicy="LRU"/>
    </ehcache>

      最后在相应的mapper层开启

    <cache type="org.mybatis.cache.ehcache"/>
  • 相关阅读:
    atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException
    atitit。 hb Hibernate sql 查询使用
    atitit.插件体系设计总结o73.doc
    Atitit.可视化编程jbpm6 的环境and 使用总结...
    paip.自动import的实现跟java.lang.SecurityException Prohibited package name java
    Linux下tar.xz结尾的文件的解压方法
    ubuntu下的Samba配置:使每个用户可以用自己的用户名和密码登录自己的home目录
    ubuntu Linux离线安装软件包
    C语言文件操作解析(一)[转载]
    PDF XChange Viewer文件关联
  • 原文地址:https://www.cnblogs.com/Charles-H/p/Mybatis-09.html
Copyright © 2011-2022 走看看