Mybatis secondary cache configuration steps: 1. Enable the secondary cache; 2. Configure the secondary cache; 3. Specify the concurrency level of the cache; 4. Use the secondary cache; 5. Clear the secondary cache. MyBatis provides a second-level cache function to improve query performance. The second-level cache is a cache that spans multiple SQL Sessions. It can reduce the number of accesses to the database and improve application performance. When using the second-level cache, you need to pay attention to thread safety issues to ensure that multiple threads do not modify the same data at the same time.
The operating system for this tutorial: Windows 10 system, DELL G3 computer.
MyBatis provides a second-level cache function to improve query performance. The second-level cache is a cache that spans multiple SQL Sessions, which can reduce the number of database accesses and improve application performance. The following are the configuration steps for MyBatis second-level cache:
1. Turn on the second-level cache
In the global configuration file of MyBatis (mybatis-config.xml), add Configure as follows:
<settings> <setting></setting> </settings>
This will enable the second-level cache function of MyBatis.
2. Configure the second-level cache
In the Mapper XML file that requires second-level cache, add the following configuration:
<cache/>
This will enable The second-level cache function of this Mapper.
3. Specify the cache concurrency level
The default cache concurrency level of MyBatis is 1, which means that only one thread is allowed to access the cache. If you need a higher concurrency level, you can add the following configuration in the Mapper XML file:
<cache concurrent="3"/>
This will set the cache concurrency level to 3. Note that the higher the concurrency level, the greater the memory usage. You need to choose based on the actual situation.
4. Use the second-level cache
In the SQL statement of Mapper, use the useCache attribute to specify whether to use the second-level cache. For example:
<select id="selectUserById" resultType="User" useCache="true"> SELECT * FROM user WHERE id = #{id} </select>
In this example, useCache="true" means using the second level cache. If the query result already exists in the cache, the cached result is returned directly, otherwise the database is queried and the result is stored in the cache.
5. Clear the second-level cache
If you need to clear the second-level cache of a Mapper, you can use the clearCache() method. For example:
userMapper.clearCache(); // 清空 UserMapper 的缓存
This will clear the second-level cache of this Mapper. If you need to clear the second-level cache of all Mappers, you can add the following configuration to the global configuration file of MyBatis:
<settings> <setting name="clearCacheOnLogout" value="true"/> </settings>
This will clear all second-level caches every time the user logs out.
The above are the configuration steps of MyBatis second-level cache. It should be noted that when using the second-level cache, you need to pay attention to thread safety issues to ensure that multiple threads do not modify the same data at the same time. At the same time, data consistency issues also need to be considered when using the second-level cache to ensure that the data remains consistent across multiple SQL Sessions.
The above is the detailed content of How to configure mybatis second level cache. For more information, please follow other related articles on the PHP Chinese website!

缓存的概述和分类概述缓存就是一块内存空间.保存临时数据为什么使用缓存将数据源(数据库或者文件)中的数据读取出来存放到缓存中,再次获取的时候,直接从缓存中获取,可以减少和数据库交互的次数,这样可以提升程序的性能!缓存的适用情况适用于缓存的:经常查询但不经常修改的(eg:省市,类别数据),数据的正确与否对最终结果影响不大的不适用缓存的:经常改变的数据,敏感数据(例如:股市的牌价,银行的汇率,银行卡里面的钱)等等MyBatis缓存类别一级缓存:它是sqlSession对象的缓存,自带的(不需要配置)不

mybatis分页的方式:1、借助数组进行分页,首先查询出全部数据,然后再list中截取需要的部分。2、借助Sql语句进行分页,在sql语句后面添加limit分页语句即可。3、利用拦截器分页,通过拦截器给sql语句末尾加上limit语句来分页查询。4、利用RowBounds实现分页,需要一次获取所有符合条件的数据,然后在内存中对大数据进行操作即可实现分页效果。

MyBatis允许使用插件来拦截的方法Executor(update,query,flushStatements,commit,rollback,getTransaction,close,isClosed)ParameterHandler(getParameterObject,setParameters)ResultSetHandler(handleResultSets,handleOutputParameters)StatementHandler(prepare,parameterize,ba

背景实际开发过程中经常需要查询节点树,根据指定节点获取子节点列表,以下记录了获取节点树的操作,以备不时之需。使用场景可以用于系统部门组织机构、商品分类、城市关系等带有层级关系的数据结构;设计思路递归模型即根节点、枝干节点、叶子节点,数据模型如下:idcodenameparent_code110000电脑0220000手机0310001联想笔记本10000410002惠普笔记本1000051000101联想拯救者1000161000102联想小新系列10001实现代码表结构CREATETABLE`

当某些sql因为不知名原因堵塞时,为了不影响后台服务运行,想要给sql增加执行时间限制,超时后就抛异常,保证后台线程不会因为sql堵塞而堵塞。一、yml全局配置单数据源可以,多数据源时会失效二、java配置类配置成功抛出超时异常。importcom.alibaba.druid.pool.DruidDataSource;importcom.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;importorg.apache.

简介今天开发时想将自己写好的代码拿来优化,因为不想在开发服弄,怕搞坏了到时候GIT到生产服一大堆问题,然后把它分离到我轮子(工具)项目上,最后运行后发现我获取List的时候很卡至少10秒,我惊了平时也就我的正常版本是800ms左右(不要看它很久,因为数据量很大,也很正常。),前提是我也知道很慢,就等的确需要优化时,我在放出我优化的plus版本,回到10秒哪里,最开始我刚刚接到这个app项目时,在我用PageHelper.startPage(page,num);(分页),还没等查到的数据封装(Pa

一、什么是缓存缓存是内存当中一块存储数据的区域,目的是提高查询效率。MyBatis会将查询结果存储在缓存当中,当下次执行相同的SQL时不访问数据库,而是直接从缓存中获取结果,从而减少服务器的压力。什么是缓存?存在于内存中的一块数据。缓存有什么作用?减少程序和数据库的交互,提高查询效率,降低服务器和数据库的压力。什么样的数据使用缓存?经常查询但不常改变的,改变后对结果影响不大的数据。MyBatis缓存分为哪几类?一级缓存和二级缓存如何判断两次Sql是相同的?查询的Sql语句相同传递的参数值相同对结

一、思路将分页所需的内容都放到一个实体类中分页数据所需要的实体类!内包含页码,页大小,总条数,总页数,起始行pagehelpr提供了这个类pageInfo,不需要我们自己创建二、主要逻辑select*from表名limit起始行,展示几条数据#第n页每页展示五条数据select*from表名limit(n-1)*5,5#每页展示多少条pageSize3#总共有多少条totalselectcount(*)from表名#总页数pagespages=total%pagesSize==0?total/p


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!
