栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring boot 整合mybatis 开启二级缓存

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot 整合mybatis 开启二级缓存

Spring boot 整合mybatis 开启二级缓存初体验 前言

下面大部分内容来源于网上的相关帖子和官网,自己简单写了个demo体验了下,个人感觉mybatis的缓存并不是很合适
查询做缓存时,遇到更新操作就会刷新缓存,尤其是多表查询时,就会很难控制。对于那些需要缓存的热数据应该抽出来放到redis上做。

mybatis 一级缓存和二级缓存的概念

之所以称之为“二级缓存”,是相对于“一级缓存”而言的。既然有了一级缓存,那么为什么要提供二级缓存呢?我们知道,在一级缓存中,不同session进行相同SQL查询的时候,是查询两次数据库的。显然这是一种浪费,既然SQL查询相同,就没有必要再次查库了,直接利用缓存数据即可,这种思想就是MyBatis二级缓存的初衷。

另外,Spring和MyBatis整合时,每次查询之后都要进行关闭sqlsession,关闭之后数据被清空。所以MyBatis和Spring整合之后,一级缓存是没有意义的。如果开启二级缓存,关闭sqlsession后,会把该sqlsession一级缓存中的数据添加到mapper namespace的二级缓存中。这样,缓存在sqlsession关闭之后依然存在。

pom引入依赖

    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.2.2
    

    
        mysql
        mysql-connector-java
        runtime
    
        
application.properties 文件配置
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#Spring jdbc 数据源配置 需要mysql-connector-java驱动依赖
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/gefrm?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=root
spring.datasource.password=123456
#mybatis配置
mybatis.mapper-locations=classpath:mapper

SET NAMES utf8mb4;
SET
FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for bsc_dict_info_test
-- ----------------------------
DROP TABLE IF EXISTS `bsc_dict_info_test`;
CREATE TABLE `bsc_dict_info_test`
(
    `dict_id`        varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci   NOT NULL DEFAULT ' ' COMMENT '字典标识',
    `dict_name`      varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci  NOT NULL DEFAULT ' ' COMMENT '字典名称',
    `dict_sitm_id`   varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci   NOT NULL DEFAULT ' ' COMMENT '字典子项标识',
    `dict_sitm_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci  NOT NULL DEFAULT ' ' COMMENT '字典子项名称',
    `suse_flag`      char(1) CHARACTER SET utf8 COLLATE utf8_general_ci       NOT NULL DEFAULT '0' COMMENT '启用标志',
    `disp_orde`      int(11) NOT NULL DEFAULT 0 COMMENT '展示顺序',
    `memo`           varchar(4000) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ' ' COMMENT '备注'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of bsc_dict_info_test
-- ----------------------------
INSERT INTO `bsc_dict_info_test`
VALUES ('bbbbbtest', '担保人/被担保人与上市公司关联关系', '999', '无关联关系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('aaaaatest', '担保人/被担保人与上市公司关联关系', '999', '无关联关系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('bbbbbtest', '担保人/被担保人与上市公司关联关系', '999', '无关联关系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('aaaaatest', '担保人/被担保人与上市公司关联关系', '999', '无关联关系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('bbbbbtest', '担保人/被担保人与上市公司关联关系', '999', '无关联关系', '0', 190, ''0'');

SET
FOREIGN_KEY_CHECKS = 1;
  • bean 类
package com.example.mybatis_cache.bean;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;


@Data
@Builder
public class BscDictInfoTestDO implements Serializable {
    private String dictId;
    private String dictName;
    private String dictSitmId;
    private String dictSitmName;
    private String suseFlag;
    private String dispOrde;
    private String memo;

}
  • mapper
package com.example.mybatis_cache.mapper;

import com.example.mybatis_cache.bean.BscDictInfoTestDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface BscDictInfoTestMapper {
    
    List selectAll();
}
  • service
package com.example.mybatis_cache.service;


public interface BscDictService {
    void info(String name, int age);
}
  • impl
package com.example.mybatis_cache.service.impl;


import com.example.mybatis_cache.bean.BscDictInfoTestDO;
import com.example.mybatis_cache.mapper.BscDictInfoTestMapper;
import com.example.mybatis_cache.service.BscDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Component
public class BscDictServiceImpl implements BscDictService {

    @Resource
    BscDictInfoTestMapper mapper;

    @Override
    public void info(String name, int age) {
        log.warn("name:{}, age:{}", name, age);
        List list = mapper.selectAll();
        list.forEach(item -> {
            log.error("{}", item);
        });
    }
}
  • 测试类
package com.example.mybatis_cache.service.impl;

import com.example.mybatis_cache.service.BscDictService;
import com.mysql.cj.xdevapi.SessionFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;


@Slf4j
@SpringBootTest
class BscDictServiceImplTest {

    @Autowired
    private BscDictService dictService;

    @Test
    void levelOneCacheTest() {
        dictService.info("a", 1);
        log.warn("======================== 分割线  ======================");
        dictService.info("a", 2);

    }

    @Test
    void testRawLevelOne() {
    }
}
测试结果

执行2次查询,只执行了一次,第二次命中的是缓存

踩坑
  • 启动抛异常:
Error creating bean with name 'cacheAutoConfigurationValidator' defined in class path resource [org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No cache manager could be auto-configured, check your configuration (caching type is 'EHCACHE')
  • 解决 添加依赖 spring-boot-starter-cache

    org.springframework.boot
    spring-boot-starter-cache

  • 启动抛异常:
Error creating bean with name 'cacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.class]: Unsatisfied dependency expressed through method 'cacheManager' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.sf.ehcache.CacheManager]: Factory method 'ehCacheCacheManager' threw exception; nested exception is java.lang.IllegalArgumentException: Cache configuration does not exist 'ServletContext resource [/ehcache.xml]'
  • 解决:注意classpath:不能少
spring.cache.ehcache.config=classpath:ehcache.xml
参考资料
  • MyBatis二级缓存介绍
  • mybatis官网中文
  • MyBatis 二级缓存全详解
  • Spring Cache和EhCache实现缓存管理
  • 第四篇:SpringBoot中Cache缓存的使用
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921367.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号