当前位置: 首页 > news >正文

mybatis开启二级缓存

例子

mybatis-config.xml

<configuration><settings><setting name="cacheEnabled" value="true"/></settings><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/test"/><property name="username" value="xxx"/><property name="password" value="xxx"/></dataSource></environment></environments><mappers><mapper resource="mapper/UserMapper.xml"/></mappers>
</configuration>

UserMapper.xml
要加上"<cache></cache>"

<mapper namespace="com.db.dbdemo.mapper.UserMapper"><cache></cache><select id="selectAll" resultType="com.db.dbdemo.entity.User" >select user_id, name, age, gender, id_type, id_number from user</select>
</mapper>
@Testpublic void testCache() {Configuration configuration = sqlSessionFactory.getConfiguration();Cache cache = configuration.getCache("com.db.dbdemo.mapper.UserMapper");cache.putObject("hah", 1);cache.getObject("hah");}

输出:
在这里插入图片描述

mybatis cache涉及以下几个类:
Cache接口:

public interface Cache {/*** @return The identifier of this cache*/String getId();/*** @param key*          Can be any object but usually it is a {@link CacheKey}* @param value*          The result of a select.*/void putObject(Object key, Object value);/*** @param key*          The key* @return The object stored in the cache.*/Object getObject(Object key);/*** As of 3.3.0 this method is only called during a rollback* for any previous value that was missing in the cache.* This lets any blocking cache to release the lock that* may have previously put on the key.* A blocking cache puts a lock when a value is null* and releases it when the value is back again.* This way other threads will wait for the value to be* available instead of hitting the database.*** @param key*          The key* @return Not used*/Object removeObject(Object key);/*** Clears this cache instance.*/void clear();/*** Optional. This method is not called by the core.** @return The number of elements stored in the cache (not its capacity).*/int getSize();/*** Optional. As of 3.2.6 this method is no longer called by the core.* <p>* Any locking needed by the cache must be provided internally by the cache provider.** @return A ReadWriteLock*/default ReadWriteLock getReadWriteLock() {return null;}}

其下的实现类及装饰类
在这里插入图片描述

  • SynchronizedCache:同步Cache,实现比较简单,直接使用synchronized修饰方法。
  • LoggingCache:日志功能,装饰类,用于记录缓存的命中率,如果开启了DEBUG模式,则会输出命中率日志。
  • SerializedCache:序列化功能,将值序列化后存到缓存中。该功能用于缓存返回一份实例的Copy,用于保存线程安全。
  • LruCache:采用了Lru算法的Cache实现,移除最近最少使用的Key/Value。
  • PerpetualCache: 作为为最基础的缓存类,底层实现比较简单,直接使用了HashMap。
http://www.lryc.cn/news/411043.html

相关文章:

  • Oracle大型数据库管理(一)Oracle大型数据库管理全面指南
  • Arcgis中查找空间距离范围内字段相等的数据
  • js中map属性
  • CS224W—03 GNN
  • 库存超卖问题解决方式
  • 30岁决心转行,AI太香了
  • C#知识|文件与目录操作:目录的操作
  • 从零到一:用Go语言构建你的第一个Web服务
  • 塔子哥的环游之旅-腾讯2023笔试(codefun2000)
  • 力扣SQL50 换座位
  • SOPHGO算能科技BM1684芯片修改内存布局
  • CUDA实现矩阵乘法的性能优化策略
  • element ui 修改table筛选按钮为自定义按钮
  • 面向对象编程:一切皆对象
  • GIT版本管理与分支控制
  • 大模型算法备案流程最详细说明【流程+附件】
  • JAVA GUI 基本使用
  • 【涵子来信】——AI革新:1.新时代是便捷的,要会用
  • 自定义线程池实现(一)
  • 计算机毕业设计选题推荐-零食批发商仓库管理系统-Java/Python项目实战
  • 基于springboot+vue+uniapp的校园快递平台小程序
  • 这两个大龄程序员,打算搞垮一个世界软件巨头!
  • LabVIEW放大器自动测量系统
  • 全面整理人工智能(AI)学习路线图及资源推荐
  • react antd upload custom request处理多个文件上传
  • ALB快速实现IPv4服务的负载均衡
  • 【LLM】-12-部署Langchain-Chatchat-0.3.x版本
  • 优化网络接收缓存减少数据丢包
  • 数据透视——判别分析
  • 书生大模型学习笔记 - 连接云端开发机