MyBatis-25MyBatis缓存配置【集成Redis】


在这里插入图片描述

概述

Redis是一个高性能的key-value数据库

MyBatis项目开发者提供了Redis的MyBatis二级缓存实现,项目名称为redis-cache.

项目地址

https://github.com/mybatis/redis-cache


集成步骤

1.添加项目依赖

<!-- https://mvnrepository.com/artifact/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接口


其他缓存框架

  • ignite-cache https://github.com/mybatis/ignite-cache
  • couchbase-cache https://github.com/mybatis/couchbase-cache
  • caffeine-cache https://github.com/mybatis/affeine-cache
  • memcached-cache https://github.com/mybatis/mcached-cache
  • scache-cache https://github.com/mybatis/scache-cache
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值