缓存第二篇:maven项目中ssm整合ehcache

来源:互联网 发布:阿里云主机购买 编辑:程序博客网 时间:2024/05/17 06:23

上一节做了ehcache的入门级别的学习,这节接近真实项目做了一次整合。发现上一节的有些东西是多余的,几乎没啥用。ehcache我们主要使用的是与mabatis的mapper结合使用。

环境:jdk1.7+maven3.3.9+win7
框架:ssm+maven
参考资料:
ehcache官方文档

1.工程目录

这里写图片描述

2.引入依赖 包括ssm+ehcache

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.zhanglf</groupId>    <artifactId>SpringEhcacheProject</artifactId>    <packaging>war</packaging>    <version>0.0.1-SNAPSHOT</version>    <name>EhCcheProject Maven Webapp</name>    <url>http://maven.apache.org</url>    <properties>        <!--spring版本号 -->        <spring.version>4.0.2.RELEASE</spring.version>        <!--mybatis版本号 -->        <mybatis.version>3.2.6</mybatis.version>        <!--log4j日志文件管理包版本 -->        <slf4j.version>1.7.7</slf4j.version>        <slf4j.version>1.2.17</slf4j.version>    </properties>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>javax</groupId>            <artifactId>javaee-api</artifactId>            <version>7.0</version>        </dependency>        <!-- 添加 Spring依赖 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-oxm</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>${spring.version}</version>        </dependency>        <!--spring核心包 -->        <!--spring单元测试依赖 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring.version}</version>            <scope>test</scope>        </dependency>        <!--mybatis核心包 -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>${mybatis.version}</version>        </dependency>        <!--mybatis核心包 -->        <!--mybatis-spring包 -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.2.2</version>        </dependency>        <!--mybatis-spring包 -->        <!--导入数据库连接jar包 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.30</version>        </dependency>        <!--导入数据库连接jar包 -->        <!-- 导入jbcp的jar,用来在application.xml中配置数据库, 如jndi,是主流数据库连接池之一 -->        <dependency>            <groupId>commons-dbcp</groupId>            <artifactId>commons-dbcp</artifactId>            <version>1.2.2</version>        </dependency>        <!-- 导入jbcp的jar,用来在application.xml中配置数据库, 如jndi,是主流数据库连接池之一 -->        <!-- JSTL标签类 -->        <dependency>            <groupId>jstl</groupId>            <artifactId>jstl</artifactId>            <version>1.2</version>        </dependency>        <!-- JSTL标签类 -->        <!-- ehcache 相关依赖 -->        <dependency>            <groupId>org.ehcache</groupId>            <artifactId>ehcache</artifactId>            <version>3.1.3</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-ehcache -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-ehcache</artifactId>            <version>1.0.0</version>        </dependency>        <!-- 序列化:将对象转换为JSON字符串 String strJson=JSON.toJSONString(实体对象); 反序列化: UserInfo             userInfo=JSON.parseObject(json,UserInfo.class); -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>fastjson</artifactId>            <version>1.2.17</version>        </dependency>        <!-- 数据库连接池druid、 用于数据库字段加、解密 -->        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>druid</artifactId>            <version>1.0.26</version>        </dependency>        <!-- 映入JSON -->        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.13</version>        </dependency>    </dependencies>    <build>        <finalName>SpringEhcacheProject</finalName>        <!-- Compiler插件,指定jdk版本 -->        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>2.0.2</version>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                </configuration>            </plugin>        </plugins>    </build></project>

3.引入各种配置文件

3.1 cache文件夹下的chcache.xml

<?xml version="1.0" encoding="UTF-8"?><ehcache>    <!-- java.io.tmpdir:Java临时目录。指定一个文件目录,当EhCache把数据写到硬盘上或者系统jvm内存时,将把数据写到这个文件目录下 -->    <diskStore path="java.io.tmpdir"/><!-- maxElementsInMemory:设置基于内存的缓存可存放对象的最大数目。  --><!-- eternal:如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false; --><!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了 --><!-- timeToIdleSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。 --><!-- 如果该属性值为0,则表示对象可以无限期地处于空闲状态。  --><!-- timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义。  --><!-- overflowToDisk:如果为true,表示当基于内存的缓存中的对象数目达到了maxElementsInMemory界限后,会把益出的对象写到基于硬盘的缓存中。 -->    <!-- 设定缓存的默认数据过期策略 -->    <defaultCache            maxElementsInMemory="10000"             eternal="false"             overflowToDisk="true"            timeToIdleSeconds="10"            timeToLiveSeconds="20"            diskPersistent="false"   diskExpiryThreadIntervalSeconds="120"/><!--  自定义缓存策略-学生信息缓存容器对应策略-->    <cache name="studentCache"                  maxElementsInMemory="1000"          eternal="false"                     overflowToDisk="true"               timeToIdleSeconds="10"              timeToLiveSeconds="20"/>   <!--  自定义缓存策略-教师信息缓存容器对应策略-->  <!--      <cache name="techerCache"          --><!--         maxElementsInMemory="1000"  --><!--         eternal="false"                 --><!--         overflowToDisk="true"       --><!--         timeToIdleSeconds="10"      --><!--         timeToLiveSeconds="20"/>        --></ehcache>

3.2 context文件夹下的applicationContext.xml中开启和引入缓存到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:cache="http://www.springframework.org/schema/cache"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="             http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd             http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd           http://www.springframework.org/schema/cache            http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">    <!-- 自动扫描注解的bean -->    <context:component-scan base-package="com.**.cache;com.**.service;com.**.dao">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>    </context:component-scan>    <!-- 开启spring缓存 -->    <cache:annotation-driven cache-manager="cacheManager" />      <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">          <property name="configLocation" value="classpath:/META-INF/app_config/cache/ehcache.xml"></property>      </bean>      <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">          <property name="cacheManager" ref="ehcache"></property>      </bean>  </beans>

3.3 mybatis文件夹下mybatis配置 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"      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">      <!-- 自动扫描 -->      <context:component-scan base-package="com.zhanglf" />     <!-- 引入数据源的配置文件 -->      <bean id="propertyConfigurer"          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="location" value="classpath:/META-INF/app_config/property/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/zhanglf/mapper/*.xml"></property>      </bean>      <!-- DAO接口所在包名,Spring会自动查找其下的类,这个很重要,缺少了就会注入Dao层失败报错 -->      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">          <property name="basePackage" value="com.zhanglf.dao" />          <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>  </beans>  

3.4 property文件夹下的资源配置文件 jdcb.properties

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/ehcachedatabaseusername=rootpassword=#定义初始化连接数initialSize=0#定义最大连接数maxActive=20#定义最大空闲maxIdle=20#定义最小空闲minIdle=1#定义最长等待时间maxWait=60000

3.5 web文件夹下的springmvc配置 springmvc.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">    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->    <context:component-scan base-package="com.zhanglf.controller" />    <!-- mvc注解驱动 -->    <mvc:annotation-driven />    <!-- 在对所有的拦截改为不包括easyui的拦截,这样easyui才能引入成功。 -->    <mvc:resources location="/static/js/easyui/" mapping="/static/js/easyui/**" />    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->    <bean id="mappingJacksonHttpMessageConverter"        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>application/json;charset=UTF-8</value>                <value>text/html;charset=UTF-8</value>            </list>        </property>    </bean>     <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->    <bean        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        <property name="messageConverters">            <list>                <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->            </list>        </property>    </bean>    <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->        <property name="prefix" value="/view/" />        <property name="suffix" value=".jsp" />    </bean></beans>  

3 配置文件引入完毕,开始java代码部分。

我们用的是spring结合mybatis接口实现对数据库的操作。其中的bo,dao,mapper的文件用generator生成器生成。

1.数据库用的是mysql database ,先create table student,初始化几条数据:
这里写图片描述

2.用代码生成器生成bo,dao,mapper文件,copy到项目中。代码如下

–>com.zhanglf.bo包下的 StudentBo.java

package com.zhanglf.bo;import java.io.Serializable;public class StudentBo implements Serializable {    private static final long serialVersionUID = 1L;    private Integer id;    private String name;    private Integer age;    private String address;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name == null ? null : name.trim();    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address == null ? null : address.trim();    }}

–>com.zhanglf.dao包下的 StudentDao.java 接口文件

package com.zhanglf.dao;import java.util.List;import com.zhanglf.bo.StudentBo;/** * 注意:Dao层接口直接对应mapper.xml。 * Dao层路径+接口名=mapper文件的namespace * @author Administrator * */public interface StudentDao {    int deleteByPrimaryKey(Integer id);    int insert(StudentBo record);    int insertSelective(StudentBo record);    StudentBo selectByPrimaryKey(Integer id);    //自己新增的方法。查询全部学生信息    List<StudentBo> selectAllStudent();    int updateByPrimaryKeySelective(StudentBo record);    int updateByPrimaryKey(StudentBo record);}

–> com.zhanglf.mapper 包下的StudentMapper.xml,导进来之后我有自己加了查询所有学生信息的sql语句,用红色标出了。对应的dao层也是增加了这样一个方法。

<?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.zhanglf.dao.StudentDao">    <resultMap id="BaseResultMap" type="com.zhanglf.bo.StudentBo">        <id column="id" property="id" jdbcType="INTEGER" />        <result column="name" property="name" jdbcType="VARCHAR" />        <result column="age" property="age" jdbcType="INTEGER" />        <result column="address" property="address" jdbcType="VARCHAR" />    </resultMap>    <sql id="Base_Column_List">        id, name, age, address    </sql>    <!--在mapper文件中的头部引入缓存策略-->    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>    <!--自己新增的方法-->    <select id="selectAllStudent" resultMap="BaseResultMap" >        select        *        from student    </select>    <select id="selectByPrimaryKey" resultMap="BaseResultMap"        parameterType="java.lang.Integer">        select        <include refid="Base_Column_List" />        from student        where id = #{id,jdbcType=INTEGER}    </select>    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">        delete from student        where id = #{id,jdbcType=INTEGER}    </delete>    <insert id="insert" parameterType="com.zhanglf.bo.StudentBo">        insert into student (id, name, age,        address)        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},        #{age,jdbcType=INTEGER},        #{address,jdbcType=VARCHAR})    </insert>    <insert id="insertSelective" parameterType="com.zhanglf.bo.StudentBo">        insert into student        <trim prefix="(" suffix=")" suffixOverrides=",">            <if test="id != null">                id,            </if>            <if test="name != null">                name,            </if>            <if test="age != null">                age,            </if>            <if test="address != null">                address,            </if>        </trim>        <trim prefix="values (" suffix=")" suffixOverrides=",">            <if test="id != null">                #{id,jdbcType=INTEGER},            </if>            <if test="name != null">                #{name,jdbcType=VARCHAR},            </if>            <if test="age != null">                #{age,jdbcType=INTEGER},            </if>            <if test="address != null">                #{address,jdbcType=VARCHAR},            </if>        </trim>    </insert>    <update id="updateByPrimaryKeySelective" parameterType="com.zhanglf.bo.StudentBo">        update student        <set>            <if test="name != null">                name = #{name,jdbcType=VARCHAR},            </if>            <if test="age != null">                age = #{age,jdbcType=INTEGER},            </if>            <if test="address != null">                address = #{address,jdbcType=VARCHAR},            </if>        </set>        where id = #{id,jdbcType=INTEGER}    </update>    <update id="updateByPrimaryKey" parameterType="com.zhanglf.bo.StudentBo">        update student        set name = #{name,jdbcType=VARCHAR},        age = #{age,jdbcType=INTEGER},        address = #{address,jdbcType=VARCHAR}        where id = #{id,jdbcType=INTEGER}    </update></mapper>

然后是com.zhanglf.cache包下的接口及其impl包下的实现类

package com.zhanglf.cache;import java.util.List;import com.zhanglf.bo.StudentBo;public interface ICacheService {    final String SERVICEID="ICacheService";    public List<StudentBo> getAllStudent();    public StudentBo getStudentById(int id);}
package com.zhanglf.cache.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.zhanglf.bo.StudentBo;import com.zhanglf.cache.ICacheService;import com.zhanglf.dao.StudentDao;@Service(ICacheService.SERVICEID)public class CacheServiceImpl implements ICacheService {    @Resource    private StudentDao studentDao;    @Override    //@Cacheable(name = "studentCache")    public List<StudentBo> getAllStudent() {         List<StudentBo> list = studentDao.selectAllStudent();         if(list!=null&& list.size()>0){             for (StudentBo student : list) {                System.out.println("查询得到的学生的姓名:"+student.getName()+",学生的年龄:"+student.getAge()+"学生地址:"+student.getAddress());            }         }        return list;    }    @Override    public StudentBo getStudentById(int id) {        StudentBo student = studentDao.selectByPrimaryKey(id);        System.out.println("查询id为"+id+"的学生姓名是:"+student.getName()+",住址:"+student.getAddress());        return student;    }}

然后是com.zhanglf.controller包下的StudentController.java

package com.zhanglf.controller;import java.util.ArrayList;import java.util.List;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.zhanglf.bo.StudentBo;import com.zhanglf.cache.ICacheService;@Controller@RequestMapping("/student")public class StudentController {    @Resource(name = ICacheService.SERVICEID)    private ICacheService cacheService;    @RequestMapping("/info")    @ResponseBody    public List getAllStudentInfo() {        List<StudentBo> list = new ArrayList<>();        list = cacheService.getAllStudent();        return list;    }    @RequestMapping("/byId")    @ResponseBody    public Integer getAllStudentInfoById(HttpServletRequest request) {        List<StudentBo> list = new ArrayList<>();        StudentBo bo = cacheService.getStudentById(Integer.valueOf(request                .getParameter("id")));        return bo.getAge();    }}

这样一个完整的项目就搭建完成了。下面部署到tomcat7,启动项目。在页面访问http://localhost:8080/SpringEhcacheProject/student/info,可以看到页面显示出全部学生信息。
这里写图片描述

那么我们该怎样测试缓存成功了没有呢?这里我们回头看一下chcache.xml中我们配置的缓存失效策略,如果我们访问后二十秒空置,不调用查询全部语句,则这个数据在缓存里就被清掉了。所以我们可以通过修改数据库中的数据,然后如果我们保持访问,则第一次查询操作加载到缓存中的数据会一直存在,即使20秒过了也会是一只是第一次加载进缓存的数据,而此时数据库中的数据我们已经改变了。可以证明缓存的存在的。然后我们停止查询操作20秒,这里不妨多等一会,在次执行这个链接访问数据库,可以看到查询到页面的数据已经和数据库中的一样了。这就说明之前的缓存已经被清除,证明缓存策略生效是出来效果的,也就是我们的搭建缓存框架是成功的。

总结:可以看到,整个配置的流程和redis配置类似-在spring中开启缓存,然后再对应的mapper文件中引入缓存,则可以把整个mapper文件中的查询语句的结果缓存到内存中(redis还需要引入工具类和自定义了实现一个接口)。而这里在mapper文件中引入<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>很重要,相反的是在CacheServiceImpl类中方法上的注解@Cacheable(name = "studentCache") 是多余的,不建议使用(配置不好启动会报错)。这里的缓存是框架级别的,是基于mapper文件的缓存,或者说是基于查询方法的缓存。我们在mapper中配置了就可以使用ehcache了。

原创粉丝点击