search
HomeJavajavaTutorialTutorial on using XML mapping cache in Java's MyBatis framework

MyBatis includes a very powerful query caching feature that can be configured and customized very easily. By default, caching is not enabled. To enable secondary caching, you need to add a line to your SQL mapping file:

<cache/>

That's literally it. The effect of this simple statement is as follows:
1. All select statements in the mapping statement file will be cached.
2. All insert, update and delete statements in the mapping statement file will refresh the cache.
3. The cache will use the Least Recently Used (LRU, least recently used) algorithm to recover.
4. According to the schedule (such as no Flush Interval, no refresh interval), the cache will not be refreshed in any time order.
5. The cache will store 1024 references to the list collection or object (regardless of what the query method returns).
6. The cache is considered a read/write cache, which means that object retrieval is not shared and can be safely modified by the caller without interfering with potential changes made by other callers or threads. Revise.

All of these properties can be modified through the cache element's properties. for example:

<cache
 eviction="FIFO"
 flushInterval="60000"
 size="512"
 readOnly="true"/>

This more advanced configuration creates a FIFO cache, flushed every 60 seconds, that stores 512 references to the resulting object or list, and the returned object is considered read-only and therefore modified between callers in different threads They can cause conflicts.
Available eviction strategies (eviction) are:

LRU – Least Recently Used: Remove objects that have not been used for the longest time.

FIFO – First in, first out: Remove objects in the order they enter the cache.

SOFT – Soft Reference: Removes objects based on garbage collector status and soft reference rules.

WEAK – Weak References: More aggressively remove objects based on garbage collector state and weak reference rules.

The default is LRU.

flushInterval can be set to any positive integer, and they represent a reasonable period of time in milliseconds. The default is not set, that is, there is no refresh interval, and the cache is only refreshed when the statement is called.
size (number of references) can be set to any positive integer, keeping in mind the number of objects you cache and the amount of memory resources available in your environment. Default value is 1024.
The readOnly property can be set to true or false. A read-only cache returns the same instance of the cached object to all callers. Therefore these objects cannot be modified. This provides important performance advantages. A read-write cache returns a copy of the cached object (via serialization). This is slower, but safer, so defaults to false.

Using custom cache
In addition to these ways of customizing caching, you can also completely override caching behavior by implementing your own cache or creating adapters for other third-party caching solutions.

<cache type=”com.domain.something.MyCustomCache”/>

This example shows how to use a custom cache implementation. The class specified by the type attribute must implement the org.mybatis.cache.Cache interface. This interface is one of the many complex interfaces in the MyBatis framework, but simply give it what it does.

public interface Cache {
 String getId();
 int getSize();
 void putObject(Object key, Object value);
 Object getObject(Object key);
 boolean hasKey(Object key);
 Object removeObject(Object key);
 void clear();
 ReadWriteLock getReadWriteLock();
}

Reference cache
Maybe at some point in the future you'll want to share the same cache configuration and instances across namespaces. In such cases you can use the cache-ref element to reference another cache.​

<cache-ref namespace=”com.someone.application.data.SomeMapper”/>

For more tutorials on the use of XML mapping cache in Java's MyBatis framework, please pay attention to the PHP Chinese website!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function