1.下載下傳mybatis相關包與ehcache相關包
下載下傳位址為:https://github.com/mybatis/ehcache-cache/releases
配置檔案
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<!--
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(較少使用)
-->
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!--
配置自定義緩存
maxElementsInMemory:緩存中允許建立的最大對象數
eternal:緩存中對象是否為永久的,如果是,逾時設定将被忽略,對象從不過期。
timeToIdleSeconds:緩存資料的鈍化時間,也就是在一個元素消亡之前,
兩次通路時間的最大時間間隔值,這隻能在元素不是永久駐留時有效,
如果該值是 0 就意味着元素可以停頓無窮長的時間。
timeToLiveSeconds:緩存資料的生存時間,也就是一個元素從建構到消亡的最大時間間隔值,這隻能在元素不是永久駐留時有效,
如果該值是0就意味着元素可以停頓無窮長的時間。
overflowToDisk:記憶體不足時,是否啟用磁盤緩存。
memoryStoreEvictionPolicy:緩存滿了之後的淘汰算法。
-->
<!-- <cache name="testCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU" />
-->
</ehcache>
spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
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/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.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 使用 annotation -->
<context:annotation-config />
<!-- 使用 annotation 自動注冊bean,并檢查@Controller, @Service, @Repository注解已被注入-->
<context:component-scan base-package="com.ding.dao" />
<!-- 引入資料庫配置檔案 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath*:/config/data.properties</value>
</property>
</bean>
<!--配置資料庫連接配接池-->
<bean id="dataSource" class="org.apache.commons.dbcp2.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="20"/>
<property name="minIdle" value="10"/>
<!-- 可以在池中保持空閑的最大連接配接數,超出設定值之外的空閑連接配接将被回收,如設定為負數,則不限制 -->
<property name="maxIdle" value="0"/>
<!-- 可以在這個池中同時被配置設定的有效連接配接數的最大值,如設定為負數,則不限制 -->
<property name="maxTotal" value="250"/>
<!--最大等待秒數,機關為毫秒, 超過時間會報出錯誤資訊-->
<property name="maxWaitMillis" value="3600"/>
<!-- 自我中斷,預設是false-->
<property name="removeAbandonedOnBorrow" value="true" />
<!-- 一個被抛棄連接配接可以被移除的逾時時間,機關為秒 ,連接配接回收-->
<property name="removeAbandonedTimeout" value="300"/>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mybatis xml的dao配置方式 -->
<property name="mapperLocations" value="classpath:com/ding/mapper/*mapper.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ding.dao"/>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
</bean>
<!-- 緩存配置(兩種) -->
<!-- 啟用緩存注解功能(請将其配置在Spring主配置檔案中) -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap實作的緩存管理器(該功能是從Spring3.1開始提供的) -->
<!--
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
-->
<!-- 若隻想使用Spring自身提供的緩存器,則注釋掉下面的兩個關于Ehcache配置的bean,并啟用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基于的Ehcache實作的緩存管理器 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/config/ehcache.xml"/>
<property name="shared" value="true" />
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
<!--開啟事務驅動-->
<tx:annotation-driven/>
<!-- 事務的配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事務通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 事務傳播規則 -->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置AOP (把切面點與事務通知關聯形成一個切面) -->
<aop:config>
<aop:pointcut expression="execution (* com.*.service.*.*(..))" id="txPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
</beans>
dao類 mybatis :user_mapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.ding.dao.UserDao">
<!--mybatis ehcache緩存配置 -->
<!-- 以下兩個<cache>标簽二選一,第一個可以輸出日志,第二個不輸出日志 -->
<!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache" /> -->
<cache type="org.mybatis.caches.ehcache.EhcacheCache" >
<property name="timeToIdleSeconds" value="3600"/><!--1 hour-->
<property name="timeToLiveSeconds" value="3600"/><!--1 hour-->
<property name="maxEntriesLocalHeap" value="1000"/>
<property name="maxEntriesLocalDisk" value="10000000"/>
<property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>
<!--結果集-->
<resultMap id="userMap" type="com.ding.pojo.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<result property="age" column="age"/>
</resultMap>
<!-- 查詢所有的使用者資料,ID和dao中的方法對應 -->
<select id="findAll" resultMap="userMap">
SELECT * FROM user
</select>
<!-- 統計user表的總條數 -->
<select id="findCount" resultType="int">
select count(*) from user
</select>
<!-- 新增使用者資訊 -->
<insert id="addUser" parameterType="com.ding.pojo.User" useGeneratedKeys="true" keyProperty="id">
insert into user(id,name,password,age) values(#{id},#{name},#{password},#{age})
</insert>
<!-- 修改資料 -->
<update id = "modifyUser" parameterType="com.ding.pojo.User" >
update user set name=#{name},password = #{password},age =#{age} where id =#{id}
</update>
<!-- 删除資料 -->
<update id = "deleteUser" parameterType="int" >
delete from user where id=#{id}
</update>
<!-- 根據ID查找資料 -->
<select id="findById" parameterType="int" resultMap="userMap">
select * from user where id =#{id}
</select>
</mapper>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>springMVC-mybatis</display-name>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath*:/config/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/config/spring-mybatis.xml</param-value>
</context-param>
<!--spring 監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化加載spring mvc配置檔案的位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/config/springMVC-servlet.xml</param-value>
</init-param>
<!--延時加載時間-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVCDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
本次做測試:正常查詢資料10條不加緩存,浏覽器響應大概需要15-30s,加緩後響應時間平均大概0-4s之間.