集成步骤
1.添加项目依赖
<!-- org.mybatis.caches " mybatis-redis -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-redis</artifactId>
<version>1.0.0-beta2</version>
</dependency>
2. 配置redis
首先启动redis 服务
然后在src/main/resources目录下新建 redis.properties
host=172.25.246.13
port=6379
connectionTimeout=5000
soTimeout=5000
password=
database=0
clientName=
3. 修改PrivilegeMapper.xml中的缓存配置
redis-cache提供了 org.mybatis.caches.redis.RedisCache
<?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接口和XML文件关联的时候, namespace的值就需要配置成接口的全限定名称 -->
<mapper namespace="com.artisan.mybatis.xml.mapper.PrivilegeMapper">
<!-- 集成Redis缓存 -->
<cache type="org.mybatis.caches.redis.RedisCache"/>
<!--其他配置-->
</mapper>
RedisCache在保存缓存数据和获取缓存数据的时候,使用了Java序列化和反序列化,因此必须保证被缓存的对象必须实现Serializable接口。