1. 程式人生 > >springmvc+mybatis+ehcache配置詳解

springmvc+mybatis+ehcache配置詳解

第一次在springmvc+mybatis 專案上整合ehcache,簡單記錄一下配置和遇到問題。
首先需要新增jar, maven專案直接貼配置了:

 <dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache-core</artifactId>
  <version>2.6.9</version>
</dependency>

<dependency>
    <groupId>org.mybatis.caches</groupId
>
<artifactId>mybatis-ehcache</artifactId> <version>1.0.3</version> </dependency>

新增配置檔案ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
>
<!--磁碟儲存配置:用來指定快取在磁碟上的儲存位置。 可以使用JavaVM環境變數(user.home, user.dir, java.io.tmpdir)--> <diskStore path="G:/cache" /> <defaultCache overflowToDisk="true" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="3600" maxElementsInMemory="10000" maxElementsOnDisk
="10" diskPersistent="true" diskExpiryThreadIntervalSeconds="300" diskSpoolBufferSizeMB="1000" memoryStoreEvictionPolicy="LRU" />
<!-- name:Cache的唯一標識 maxElementsInMemory:記憶體中最大快取物件數 maxElementsOnDisk:磁碟中最大快取物件數,若是0表示無窮大 eternal:Element是否永久有效,一但設定了,timeout將不起作用 overflowToDisk:配置此屬性,當記憶體中Element數量達到maxElementsInMemory時,Ehcache將會Element寫到磁碟中 timeToIdleSeconds:設定Element在失效前的允許閒置時間。僅當element不是永久有效時使用,可選屬性,預設值是0,也就是可閒置時間無窮大 timeToLiveSeconds:設定Element在失效前允許存活時間。最大時間介於建立時間和失效時間之間。僅當element不是永久有效時使用,預設是0.,也就是element存活時間無窮大 diskPersistent:是否快取虛擬機器重啟期資料 diskExpiryThreadIntervalSeconds:磁碟失效執行緒執行時間間隔,預設是120秒 diskSpoolBufferSizeMB:這個引數設定DiskStore(磁碟快取)的快取區大小。預設是30MB。每個Cache都應該有自己的一個緩衝區 memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理記憶體。預設策略是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用) --> <!-- service 快取配置 --> <!-- <cache name="test" overflowToDisk="true" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" maxElementsInMemory="1000" maxElementsOnDisk="10" diskPersistent="true" diskExpiryThreadIntervalSeconds="300" diskSpoolBufferSizeMB="100" memoryStoreEvictionPolicy="LRU" /> --> </ehcache>

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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                        http://www.springframework.org/schema/cache 
                        http://www.springframework.org/schema/cache/spring-cache.xsd">
    <!-- 自動掃描com.xxx下的service.impl -->
    <context:component-scan base-package="com.xxx">
    </context:component-scan>
    <!-- 使用 ehcache-core-2.4.6.jar 配置 -->
    <!-- 開啟spring快取 -->
<!--    <cache:annotation-driven cache-manager="cacheManager" />
    <bean id="cacheManagerFactory"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
        <property name="configLocation" value="classpath:conf/ehcache.xml" />
    </bean>

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory" />
    </bean> -->
    <!-- 使用 ehcache-core-2.4.6.jar 配置 -->

    <!-- 使用 ehcache-core-2.6.9.jar 配置 -->
    <!-- ehcache-core jar 2.5.0以上版本需要加  p:shared="true"  -->
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">  
     <property name="configLocation" value="classpath:conf/ehcache.xml" />  
   </bean>
   <!-- 使用 ehcache-core-2.6.9.jar 配置 -->
   <!-- 注意classpath寫完整 -->
   <!-- 以下配置略了 -->

Mapper檔案中新增配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.XXX.service.container.dao.ContainerDao">
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> 不輸出日誌  mapper的property設定優先於xml的-->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache">
    <property name="timeToIdleSeconds" value="5"/> 
    <property name="timeToLiveSeconds" value="5"/> 
    <property name="maxEntriesLocalHeap" value="1000"/>  
    <property name="maxEntriesLocalDisk" value="10000000"/>  
    <property name="memoryStoreEvictionPolicy" value="LRU"/>  
    </cache>
   <!-- 以下略了 -->

配置完畢,重啟測試:
呼叫這個mapper的一個查詢,然後手動操作修改資料庫該表的資訊,查詢結果不變,查詢快取生效。
關於remove快取問題,hibernate的專案是加註解@TriggersRemove,
mybatis是呼叫了配置這個mapper裡的同一個表的非查詢語句後快取就清除了,重新查詢的資料庫(經測試有效,但是沒有在官方文件查到詳細解釋)。
注意遇到的問題:當mapper檔案裡不設定cache的timeToLiveSeconds,timeToIdleSeconds時候,timeToIdleSeconds和timeToLiveSeconds時間不準確幾分鐘就訪問資料庫了,當在mapper檔案里加上timeToIdleSeconds,timeToLiveSeconds 就OK了;
後來仔細想想應該mybatis和hibernate的還是有點區別的,mybatis的話就在mapper.xml檔案多加上timeToIdleSeconds吧。

相關推薦

springmvc+mybatis+ehcache配置

第一次在springmvc+mybatis 專案上整合ehcache,簡單記錄一下配置和遇到問題。 首先需要新增jar, maven專案直接貼配置了: <dependency> &

Spring Boot中使用MyBatis註解配置(1)

sql type .org 實體 sch 整合 PE 匯總 同傳 之前在Spring Boot中整合MyBatis時,采用了註解的配置方式,相信很多人還是比較喜歡這種優雅的方式的,也收到不少讀者朋友的反饋和問題,主要集中於針對各種場景下註解如何使用,下面就對幾種常見的情況舉

springboot中使用Mybatis註解配置

版權宣告:本文為博主原創文章,未經博主允許不得轉載。    https://blog.csdn.net/Winter_chen001/article/details/78623700 之前寫了關於Spring boot Mybatis 整合(註解版) 中使用了簡單的註解

Spring Boot中使用MyBatis註解配置

   轉自翟永超 之前在Spring Boot中整合MyBatis時,採用了註解的配置方式,相信很多人還是比較喜歡這種優雅的方式的,也收到不少讀者朋友的反饋和問題,主要集中於針對各種場景下註解如何使用,下面就對幾種常見的情況舉例說明用法。 在做下面的示例之前,先準備一

04 - springMVC三大元件配置

springMVC三大元件配置 概述 1. springMVC預設載入元件 2.配置檔案解讀 3.配置檢視解析器 概述 上篇分析了springMVC的架構流程,這篇對三大元件的配

hibernate二級快取 Ehcache配置

一、hibernate快取簡介 一級快取(session):內部快取 事務範圍:快取只能被當前事務訪問。快取的生命週期依賴於事務的生命週期,當事務結束時,快取也就結束生命週期。 二級快取(sessionFactory): 快取被應用範圍內的所有事務共享。 這些事務

Mybatis Generator配置

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

Maven搭建Spring+SpringMVC+Mybatis+Shiro專案

最近新換了一臺機子,這次主要框架使用spring+SpringMVC+Mybatis+Shiro。專案持久層使用Mybatis3.3.0,控制層使用SpringMVC4.3.6,使用Spring4.3.6管理控制器,使用Shiro1.2.4許可權管理器,資料庫連線池使

Spring整合Mybatis關鍵配置

根據官方的說法,在ibatis3,也就是Mybatis3問世之前,Spring3的開發工作就已經完成了,所以Spring3中還是沒有對Mybatis3的支援。因此由Mybatis社群自己開發了一個Mybatis-Spring用來滿足Mybatis使用者整合Spring的需求

mybatis一對一映射配置

技術分享 iat rom cti 加載 文件中 src 裏的 new 聽說mybatis一對一有三種寫法,今天我試了一下。 數據庫表準備 為了偷懶,我直接就拿用戶權限菜單裏的菜單表和菜單與權限的中間表做實現,他們原來是多對多的關系,這邊我假設這兩張表是一對一。 表 g

SpringMVC原理及非註解配置

ges 控制器 sof 靈活 size 實現 query -c requests 1. Spring介紹   Spring MVC是Spring提供的一個強大而靈活的web框架。借助於註解,Spring MVC提供了幾乎是POJO的開發模式,使得控制器的開發和測試更加簡單

Mybatis Generator最完整配置

tor text qualifier sql limited 包含 正常 選擇 format <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration

SpringMVC 框架系列之組件概述與配置

align 概述 handle ont htm 配置文件 掃描器 springmvc 解析 在上一篇文章 SpringMVC 框架系列之初識與入門實例 的實例中,我們已經知道,SpringMVC 框架是一個 web 層的框架,本篇文章就詳細解釋一下 SpringMVC 框架

Mybatis中接口和對應的mapper文件位置配置

文件名 存儲 prope ocl user enc directory CA https Mybatis中接口和對應的mapper文件位置配置詳解 原鏈接為:https://blog.csdn.net/fanfanzk1314/article/details/714

mybatis 代碼生成器(IDEA, Maven)及配置(部分配置你應該不知道)

win 項目 找到 mini 屬性 新建 ini 默認 sub 在使用 mybatis 過程中, 當手寫 JavaBean和XML 寫的越來越多的時候, 就越來越同意出錯。這種重復性的工作, 我們當然不希望做那麽多。 還好, mybatis 為我們提供了強大的代碼生成--M

【SpringBoot學習之路】12.SpringMVC自動配置

轉載宣告:商業轉載請聯絡作者獲得授權,非商業轉載請註明出處.原文來自 © 呆萌鍾【SpringBoot學習之路】12.SpringMVC自動配置詳解  SpringMVC自動配置 Spring MVC auto-configuration Sprin

深入淺出Mybatis原始碼系列(三)---配置之properties與environments(mybatis原始碼篇)

上篇文章《深入淺出Mybatis原始碼系列(二)---配置簡介(mybatis原始碼篇)》我們通過對mybatis原始碼的簡單分析,可看出,在mybatis配置檔案中,在configuration根節點下面,可配置properties、typeAliases、plugins、

【轉】Mybatis Generator最完整配置

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration   PUBLIC "-//mybatis.org//DTD MyBatis Generator Confi

mybatis配置

1.SqlMapConfig.xml configuration作為根標籤有如下子標籤,dtd資訊已經將其子標籤出現的順序以及次數規定好了 <1>properties(屬性),該標籤中有resource(類路徑)或url(檔案路徑例:url=”file:/

SSM框架整合配置(spring,spring mvc,mybatis)

當今SSM框架已經成為了一種主流,其中spring,spring mvc和mybatis框架的功能很強大,給我們程式設計師節省了很多力氣,可以說這三種框架簡直就是我們程式設計師的福音,但是我們都知道,框架在自身帶來便捷的同時,也存在很多的配置檔案,更別說當三個框架整合的時候那就更加的困難了,