Mybatis整合Redis实现二级缓存

<slf4j.version>1.7.13</slf4j.version>

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

jcl-over-slf4j

${slf4j.version}

runtime

org.apache.logging.log4j

log4j-api

${log4j2.version}

org.apache.logging.log4j

log4j-core

${log4j2.version}

org.apache.logging.log4j

log4j-slf4j-impl

${log4j2.version}

org.apache.logging.log4j

log4j-web

${log4j2.version}

runtime

com.lmax

disruptor

${log4j2.disruptor.version}

3、在Resource中添加一个ehcache.xml的配置文件

在resources中新建ehcache.xml文件:放入缓存策略

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:noNamespaceSchemaLocation=“http://ehcache.org/ehcache.xsd”

updateCheck=“false”>

<defaultCache eternal=“false” maxElementsInMemory=“1000” overflowToDisk=“false” diskPersistent=“false”

timeToIdleSeconds=“0” timeToLiveSeconds=“600” memoryStoreEvictionPolicy=“LRU”/>

4、在applicationContext.xml中加入chache配置

在resources中新建applicationContext.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”

xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd”>

在resources中新建applicationContext-ehcache.xml文件:将ehcache缓存内容放入,配置ehcache中的一些相关配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”>

5、mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓存

进入applicationtext-mybatis.xml文件开启二级缓存,在此文件添加👇内容:

true

false

true

6、在XxxMapper.xml中配置cache,添加二级缓存的核心类

7、可以通过select标签的useCache属性打开或关闭二级缓存

<select id=“selectBooksLike1” useCache=“false” resultType=“com.lv.model.Book” parameterType=“java.lang.String”>

select * from t_mvc_book where bname like #{bname}

总结框架的缓存策略:

1、二级缓存开启,既可以缓存单条,也可以缓存多条

2、可以通过mapper.xml的useCache属性控制是否使用缓存

8、测试:

@Test

public void cacheOne() {

System.out.println(this.bookService.selectBooksIn(Arrays.asList(new Integer[]{24,28,29})));

System.out.println(this.bookService.selectBooksIn(Arrays.asList(new Integer[]{24,28,29})));

}

输出结果:第二次执行时利用了缓存

二、mybatis使用redis作为二级缓存


1、添加redis相关依赖

<redis.version>2.9.0</redis.version>

<redis.spring.version>1.7.1.RELEASE</redis.spring.version>

redis.clients

jedis

${redis.version}

org.springframework.data

spring-data-redis

${redis.spring.version}

2、导入Jackson相关依赖

<jackson.version>2.9.3</jackson.version>

com.fasterxml.jackson.core

jackson-databind

${jackson.version}

com.fasterxml.jackson.core

jackson-core

${jackson.version}

com.fasterxml.jackson.core

jackson-annotations

${jackson.version}

3、spring + redis 集成实现缓存功能(与mybatis无关)

添加两个redis的配置文件,并将redis.properties和applicationContext-redis.xml配置到applicationContext.xml文件中

注1:将redis.properties导入到applicationContext.xml文件中

在resource文件中新建文件 redis.properties:包含连接信息

#虚拟机的IP

redis.hostName=47.100.191.44

redis.port=6379

redis.password=xiaoli_redis

redis.timeout=10000

redis.maxIdle=300

redis.maxTotal=1000

redis.maxWaitMillis=1000

redis.minEvictableIdleTimeMillis=300000

redis.numTestsPerEvictionRun=1024

redis.timeBetweenEvictionRunsMillis=30000

redis.testOnBorrow=true

redis.testWhileIdle=true

在resource文件中新建文件 applicationContext-redis.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”

xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”>

<bean id=“connectionFactory” class=“org.springframework.data.redis.connection.jedis.JedisConnectionFactory”

destroy-method=“destroy”>

4、将redis缓存引入到mybatis中

创建mybatis的自定义缓存类“RedisCache”,必须实现org.apache.ibatis.cache.Cache接口

在util包内添加RedisCache.java文件:

package com.lv.util;

import org.apache.ibatis.cache.Cache;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.dao.DataAccessException;

import org.springframework.data.redis.connection.RedisConnection;

import org.springframework.data.redis.core.RedisCallback;

import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.ReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class RedisCache implements Cache //实现类

{

private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);

private static RedisTemplate<String,Object> redisTemplate;

private final String id;

/**

  • The {@code ReadWriteLock}.

*/

private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

@Override

public ReadWriteLock getReadWriteLock()

{

return this.readWriteLock;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

小编利用空余时间整理了一份《MySQL性能调优手册》,初衷也很简单,就是希望能够帮助到大家,减轻大家的负担和节省时间。

关于这个,给大家看一份学习大纲(PDF)文件,每一个分支里面会有详细的介绍。

image

这里都是以图片形式展示介绍,如要下载原文件以及更多的性能调优笔记(MySQL+Tomcat+JVM)!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
道该从何学起的朋友,同时减轻大家的负担。**[外链图片转存中…(img-rDgQL3dX-1713500137658)]

[外链图片转存中…(img-unfbiAvd-1713500137659)]

[外链图片转存中…(img-CdhVJsRs-1713500137660)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

小编利用空余时间整理了一份《MySQL性能调优手册》,初衷也很简单,就是希望能够帮助到大家,减轻大家的负担和节省时间。

关于这个,给大家看一份学习大纲(PDF)文件,每一个分支里面会有详细的介绍。

[外链图片转存中…(img-ZAg3xToJ-1713500137662)]

这里都是以图片形式展示介绍,如要下载原文件以及更多的性能调优笔记(MySQL+Tomcat+JVM)!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值