Master MyBatis Second‑Level Cache to Supercharge Your Queries

This article explains what MyBatis second‑level cache is, why it’s needed, how to configure it globally and per‑mapper, the underlying CachingExecutor mechanism, and typical scenarios where enabling the cache can dramatically reduce database load and speed up read‑heavy applications.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Master MyBatis Second‑Level Cache to Supercharge Your Queries

MyBatis Second-Level Cache

Cache is data stored in memory, often derived from database query results; using cache avoids frequent database interactions and improves query response speed.

MyBatis provides cache support, divided into first-level and second-level cache as shown:

First-level cache: SqlSession‑level cache, valid only within a SqlSession.

Second-level cache: mapper‑level cache, shared across the same namespace; it must be manually enabled.

Why Use MyBatis Second-Level Cache?

First-level cache’s sharing scope is limited to a single SqlSession; enabling second-level cache allows multiple SqlSessions to share cached data.

How to Configure MyBatis Second-Level Cache

1. Enable globally in the MyBatis configuration file:

<settings>
    <!-- Enable logging -->
    <setting name="logImpl" value="STDOUT_LOGGING"/>
    <!-- Enable second-level cache (global) -->
    <setting name="cacheEnabled" value="true"/>
</settings>

2. Configure cache for a specific mapper namespace:

<!-- cache node: configure cache for current namespace mapper -->
<cache eviction="LRU" flushInterval="100000" readOnly="true" size="1024"/>

3. Ensure cached entity classes implement the Serializable interface so they can be serialized and deserialized.

Implementation Principle

When second-level cache is enabled, MyBatis decorates the Executor with CachingExecutor; before the first-level cache query process, CachingExecutor checks the second-level cache. The workflow is:

All statements under the same namespace share the same Cache, making the second-level cache a global variable shared by multiple SqlSessions.

The query execution flow becomes: second-level cache → first-level cache → database.

Application Scenarios

Second-level cache is recommended for data with high query frequency and low update frequency.

For read‑heavy requests where real‑time freshness is not critical, using MyBatis second-level cache can reduce database access and improve response speed.

Typical cases include time‑consuming statistical SQLs, telephone bill queries, etc.; by setting an appropriate refresh interval, MyBatis can automatically clear the cache based on data change frequency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMyBatisORMSecond-Level Cache
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.