SpringMvc+MyBatis+ehcache+ExtJS分页

本文介绍如何在MyBatis项目中集成EhCache实现数据缓存,并提供了一套完整的配置示例,包括必要的Jar包、配置文件及具体使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,需要的jar包

除了springmvc和mybatis的一些jar包以外,其他的需要的jar包如下

1.ehcache-2.8.3.jar
  网上很多资料列出来的是ehcache-core-2.4.4.jar,这个包,经过实测这个包无效,需要使用上面列出来的包
2.mybatis-ehcache-1.0.0.jar

二,目录结构如下

image

三,主要的配置文件

先加入ehache.xml文件后,主要的一步是在spring-mybatis.xml中加入

     <!-- 使用ehcache缓存 --> 
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">
         <property name="configLocation" value="classpath:ehcache.xml" />
    </bean>

以下是主要的配置文件

1,ehache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache dynamicConfig="false" monitoring="off" updateCheck="false"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <diskStore path="java.io.tmpdir"/>
    <!-- 定义缓存策略
        eternal="false"                   // 元素是否永恒,如果是就永不过期(必须设置)
          maxEntriesLocalHeap="1000"         // 堆内存中最大缓存对象数,0没有限制(必须设置)
          overflowToDisk="false"          // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)
          diskPersistent="false"          // 磁盘缓存在VM重新启动时是否保持(默认为false)
          timeToIdleSeconds="0"             // 导致元素过期的访问间隔(秒为单位). 当eternal为false时,这个属性才有效,0表示可以永远空闲,默认为0
          timeToLiveSeconds="600"         // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期
          memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU
   -->
    <defaultCache eternal="false" maxEntriesLocalHeap="0" timeToIdleSeconds="300" timeToLiveSeconds="300"/>
    <cache name="myCache" 
          maxElementsInMemory="10000"           
        eternal="true"          
        overflowToDisk="false"           
            timeToIdleSeconds="0"           
        timeToLiveSeconds="600"          
        memoryStoreEvictionPolicy="LFU" />  
   
</ehcache>

 

2,spring-mvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    <!-- 开启注解-->  
    <mvc:annotation-driven />
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="com.myd.controller" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                        <value>application/json;charset=UTF-8</value>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            </list>
        </property>
    </bean>

    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="" />
        <property name="suffix" value="" />
    </bean>
     
    <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
    <bean id="multipartResolver" 
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean>
    
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">    
         <property name="exceptionMappings">    
             <props>    
                 <prop key="java.lang.Exception">error</prop>    
                 <prop key="java.lang.Throwable">error</prop>    
             </props>    
         </property>    
         <property name="statusCodes">    
             <props>    
                 <prop key="error">500</prop>    
                 <prop key="error">404</prop>    
             </props>    
         </property>    
         <!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->    
         <property name="warnLogCategory" value="WARN"></property>    
         <!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->    
         <property name="defaultErrorView" value="error"></property>    
         <!-- 默认HTTP状态码 -->    
         <property name="defaultStatusCode" value="500"></property>    
     </bean> 
     
</beans>

 

3,spring-mybatis.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    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  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/cache 
                        http://www.springframework.org/schema/cache/spring-cache.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.myd" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>
 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>
 
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/myd/mapping/*.xml"></property>
    </bean>
 
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.myd.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
 
    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

     <!-- 使用ehcache缓存 -->  
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">
         <property name="configLocation" value="classpath:ehcache.xml" />
    </bean>


</beans>

4.jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8
username=root
password=mayude
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

5.log4j.properties

log4j.rootLogger = debug,stdout,D,E

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d %p [%c] - %m%n

log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout

四,使用缓存

 

通过上述的配置已经配置好缓存了,可是如何使用呢,使用缓存时需要在mybatis的xml配置文件中加入如下的配置

例如UserMapper.xml

<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
 -->
<mapper namespace="com.myd.mapper.UserMapper">
<!-- 使用缓存 -->
<!-- 
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
 -->  

<cache type="org.mybatis.caches.ehcache.LoggingEhcache" >  
    <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>

 
    <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
    使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
    resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
    User类就是users表所对应的实体类
    -->
    <!-- 查询所有的user对象 -->
    <select id="getall" parameterType="com.myd.entity.User" resultType="com.myd.entity.User">
        select * from user where 1=1
        <if test="user_name!=null and user_name !=''">
            and user_name=#{user_name}
        </if>
        <if test="user_number!=null and user_number !=''">
              and user_number=#{user_number}
        </if>
    </select>
    
    <!-- 根据分页查询数据 -->
     <select id="findpage"  resultType="com.myd.entity.User" parameterType="java.util.HashMap">
        select * from user where 1=1 
        <if test="user.user_name!=null and user.user_name !=''">
            and user_name=#{user.user_name}
        </if>
        <if test="user.user_number!=null and user.user_number !=''">
            and user_number=#{user.user_number}
        </if>
        limit #{pageable.start},#{pageable.limit}
    </select>
    
    <!-- 添加数据 -->
    <insert id="save" useGeneratedKeys="true" keyProperty="user_pk" parameterType="com.myd.entity.User">
        <!-- 设置uuid为主键,只针对mysq数据库 -->
        <selectKey keyProperty="user_pk" resultType="String" order="BEFORE">
            select uuid()        
        </selectKey>
        insert into user (user_pk, user_number, user_name, user_password,fk_roles_pk,user_sex,user_birthday,fk_station_pk,user_intime,user_phone,user_email) 
        values 
        (
            #{user_pk}, #{user_number}, #{user_name}, #{user_password},#{fk_roles_pk},#{user_sex},#{user_birthday},#{fk_station_pk},#{user_intime},#{user_phone},#{user_email}
        )
        
    </insert>
    
    <!-- 删除数据 -->
    <delete id="delete" >
        delete from user where user_pk IN
        <foreach collection="list" item="id"  open="(" separator="," close=")">  
            #{id}  
         </foreach>  
    </delete>
</mapper>

 

五,测试缓存是否生效

可以自己写一个测试类,来看是否生效,我这儿主要是已经和extjs做了整合,所以测试的时候直接刷新页面查看效果

第一次加载页面的时候发现是从数据库中查询的数据,查看控制台的输出日志发现如下,会输出sql语句,命中率为0

对比两次的输出结果,就可以看到缓存起作用了

2015-09-29 16:34:19,523 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2015-09-29 16:34:19,538 DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@55249084] was not registered for synchronization because synchronization is not active
2015-09-29 16:34:19,570 DEBUG [com.myd.mapper.UserMapper] - Cache Hit Ratio [com.myd.mapper.UserMapper]: 0.0
2015-09-29 16:34:19,585 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2015-09-29 16:34:20,147 DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring
2015-09-29 16:34:20,162 DEBUG [com.myd.mapper.UserMapper.getall] - ==>  Preparing: select * from user where 1=1
2015-09-29 16:34:20,194 DEBUG [com.myd.mapper.UserMapper.getall] - ==> Parameters:
2015-09-29 16:34:20,272 DEBUG [com.myd.mapper.UserMapper.getall] - <==      Total: 29

当第二次刷新页面的时候,就会发现取得是之前缓存的数据,同时会显示缓存命中率

2015-09-29 16:36:57,778 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2015-09-29 16:36:57,778 DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67f1379] was not registered for synchronization because synchronization is not active
2015-09-29 16:36:57,779 DEBUG [com.myd.mapper.UserMapper] - Cache Hit Ratio [com.myd.mapper.UserMapper]: 0.3333333333333333
2015-09-29 16:36:57,779 DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67f1379]
2015-09-29 16:36:57,780 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2015-09-29 16:36:57,780 DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@38f40746] was not registered for synchronization because synchronization is not active
2015-09-29 16:36:57,781 DEBUG [com.myd.mapper.UserMapper] - Cache Hit Ratio [com.myd.mapper.UserMapper]: 0.5
2015-09-29 16:36:57,781 DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@38f40746]
2015-09-29 16:36:57,783 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor] - Written [com.myd.utils.Page@598602dc] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@16358f38]
2015-09-29 16:36:57,787 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Null ModelAndView returned to DispatcherServlet with name 'SpringMVC': assuming HandlerAdapter completed request handling
2015-09-29 16:36:57,788 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request

六,说明

各个接口还有action就不详细说了,都在源代码里面,或者查看另外一篇博客,里面有详细的说明,只是在之前代码的基础上增加了ehcache缓存功能

另外一篇的地址是http://my.oschina.net/mayude/blog/504304

另外用extjs5实现了一个简单的后台管理框架,其中的员工管理实现了列表获取还有分页等简单的功能

七,代码下载地址

http://download.youkuaiyun.com/detail/myude/9144999

转载于:https://my.oschina.net/mayude/blog/512010

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值