MyBatis Basic Learning (v)-Caching

Source: Internet
Author: User

I. Overview

MyBatis provides query caching, which can be used to reduce data pressure and improve system performance if there is data in the cache that is not retrieved from the database.

The first-level cache is a cache at the sqlsession level. The Sqlsession object needs to be constructed when manipulating the database, and there is a data structure (HASHMAP) in the object that stores the cached data. The cache data regions (HASHMAP) between different sqlsession are not affected by each other.

The second level cache is mapper level cache, multiple sqlsession to operate the same mapper SQL statement, multiple sqlsession can share a level two cache, level two cache is across sqlsession.

二、一级 Cache

For the first time, the user ID 1 is queried to find out if there is an ID of 1 user information in the cache, and if not, the user information is queried from the database.

Get the user information and store the user information in the first level cache.

If sqlsession goes to commit (INSERT, UPDATE, delete), empties the first-level cache in the sqlsession so that the cache is stored with the most up-to-date information and avoids dirty reads.

The second launch of the query user ID 1 user information, first to find out if the cache has ID 1 user information, the cache has, directly from the cache to obtain user information.

Mybatis first-level caching is supported by default.

1. Test A

Results:

Executes only one query statement.

2. Test 2

Results:

Sqlsession to perform INSERT, UPDATE, delete, the first level cache in Sqlsession is emptied.

MyBatis Basic Learning (v)-Caching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.