Mybatis整合Redis实现二级缓存

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

<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;

最后

面试前一定少不了刷题,为了方便大家复习,我分享一波个人整理的面试大全宝典

  • Java核心知识整理

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

Java核心知识

  • Spring全家桶(实战系列)

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

  • 其他电子书资料

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

Step3:刷题

既然是要面试,那么就少不了刷题,实际上春节回家后,哪儿也去不了,我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。

以下是我私藏的面试题库:

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
private final String id;

/**

  • The {@code ReadWriteLock}.

*/

private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

@Override

public ReadWriteLock getReadWriteLock()

{

return this.readWriteLock;

最后

面试前一定少不了刷题,为了方便大家复习,我分享一波个人整理的面试大全宝典

  • Java核心知识整理

[外链图片转存中…(img-LQbprRLu-1714708584905)]

Java核心知识

  • Spring全家桶(实战系列)

[外链图片转存中…(img-FK6R4fA2-1714708584906)]

  • 其他电子书资料

[外链图片转存中…(img-G2NP4YwZ-1714708584906)]

Step3:刷题

既然是要面试,那么就少不了刷题,实际上春节回家后,哪儿也去不了,我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。

以下是我私藏的面试题库:

[外链图片转存中…(img-urQz6fRF-1714708584906)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 30
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值