Mybatis knowledge points sorting



1. Concept: a persistence layer framework in Java.
2. Features and advantages:
(1) Java code and SQL code are completely separated.
(2) Good support for complex object mapping (input mapping, output mapping)
(3) Using dynamic SQL, you can prevent SQL injection.
3. Principle:
(1) Create the mybatis-config.xml configuration file
(2) Create sqlsessionfactory
(3) Write the entity class corresponding to the database table
(4) Create the SQL mapping file of mybatis. In this file, the attributes of the entity class are associated with the columns of the database table, and SQL statements can be written
(5) Get session from an instance of sqlsessionfactory
(6) Inside the session, operations are performed through the executor executor. The executor will use the mapped statement object, which encapsulates the database storage, including SQL statements, input parameters, and output result types
(7) Close session

Mybatis cache:

1. Concept:
(1) L1 cache: L1 cache is a sqlsession level cache. When operating the database, you need to construct an sqlsession object in which a data structure (HashMap) is used to store cached data. The cached data areas (hashmaps) between different sqlsessions do not affect each other.
(2) L2 cache: L2 cache is a mapper level cache. Multiple sqlsessions operate on the same mapper SQL statement. Multiple sqlsessions can share L2 cache. L2 cache is cross sqlsessions. (the principle of L2 cache is the same as that of L1 cache. The first query will put the data into the cache, and then the second query will directly fetch it from the cache. However, L1 cache is based on sqlsession, while L2 cache is based on the namespace of mapper file, that is, multiple sqlsessions can share the L2 cache area in one mapper, and if two The namespaces of two mappers are the same. Even if there are two mappers, the data queried by SQL in the two mappers will still exist in the same L2 cache area.)
Their relationship is shown in the figure below:
Mybatis knowledge points sorting

2. Use of L1 cache:
In an sqlsession, we query the user table twice according to the ID to see how they issue SQL statements:
(1) For the first time, query the user information with user ID 1. First find out whether there is user information with ID 1 in the cache. If not, query the user information from the database. Get the user information and store the user information in the first level cache.
(2) If the intermediate sqlsession performs a commit operation (insert, update, delete), the L1 cache in the sqlsession will be emptied. The purpose of this is to store the latest information in the cache and avoid dirty reading.
(3) The second time, query the user information with user ID 1. First find out whether there is user information with ID 1 in the cache. If there is in the cache, directly obtain the user information from the cache.

Mybatis knowledge points sorting

3. L2 cache usage:
Mybatis knowledge points sorting

4. Precautions:
(1) If cacheenabled = true is set; When mybatis executes a query, first check whether the L2 cache (global cache) has query results. If so, directly return the cached results; If not, execute the real query, put the query results in the cache, and then return the results to the user.
(2) L2 cache: in mybatis, each mapper can have a L2 cache. Use the < cache > node configuration.
(3) L2 cache usage scenario:

For query requests with multiple accesses and users' real-time requirements for query results are not high, at this time, mybatis secondary cache technology can be used to reduce database access and improve access speed. Business scenarios such as time-consuming statistical analysis SQL, telephone bill query SQL, etc. The implementation method is as follows: by setting the refresh interval, mybatis will automatically empty the cache every other period of time, and set the cache refresh interval flush interval according to the data change frequency, such as 30 minutes, 60 minutes, 24 hours, etc., depending on the demand.
 Mybatis Level 2 cache does not implement fine-grained data level cache well. For example, the following requirements: cache commodity information. Due to the large number of commodity information query visits, users are required to query the latest commodity information every time, At this time, if you use the secondary cache of mybatis, you can't refresh the cache information of a commodity without refreshing the information of other commodities. Because the secondary cache area of mybaits is divided by mapper, when a commodity information changes, the cache data of all commodity information will be cleared. Solving such problems may require targeted caching of data at the business layer according to needs.


1. #{} and ${}
(1) #{} is a placeholder to receive input parameters. The parameter types can be basic type, POJO and HashMap
(2) ${} is a splicing symbol, which may lead to SQL injection. It is not recommended.
2. Resultmap label:
(1) Label for result set mapping

A. Type: Specifies the type of entity class saved in the result set
B. ID: the ID of the resultmap tag
C. Automapping: the value range is true | false, which sets whether to start the automatic mapping function. The automatic mapping function is to automatically find the property name with the same lowercase name as the field name and call the setter method. When it is set to false, the mapping relationship needs to be clearly indicated in the resultmap before calling the corresponding setter method.

(3) ID: used to set the mapping relationship between the primary key field and the domain model attribute
(4) Result: used to set the mapping relationship between common fields and domain model attributes
3. SQL tag: used to encapsulate SQL statements or SQL fragments

Mybatis’s Dao development method:
1. Using the mapper proxy method:
(1) Write the interface class of
(2) Write mapper.xml
2. When writing interface classes, you need to comply with some specifications. Mybatis can automatically generate Dao implementation classes of interface classes. Specifications include:
(1) The namespace in mapper.xml needs to be equal to the address of mapper interface class
(2) The name of the method of the mapper interface class is equal to the ID of the statement (corresponding SQL statement) in mapper.xml
(3) The input parameter type of the mapper interface class is equal to the type specified by the parametertype of the statement in mapper.xml
(4) The return parameter type of the mapper interface class is equal to the type specified by the resulttype of the statement in mapper.xml

The essential difference between mybatis and Hibernate:
1. Hibernate does not need to write SQL. SQL is generated directly through HQL or hibernate. Mybatis needs to write SQL.
2. Mybatis is suitable for projects that require frequent changes, because its SQL is generated by programmers and easy to change. Hibernate is suitable for projects with few changes in requirements.

Essential difference between mybatis and ibatis:
1. Mybatis simplifies the coding process and does not need to write Dao implementation classes. Write a Dao interface and a configuration file directly. Mybatis automatically generates Dao implementation classes.

Nested query and nested result query:
Nested query: use the query twice, and then assemble it in mybatis (memory). May cause n + 1 problems.
Nested result query: use the query once, and then assemble the results.
Specific reference:…

reference resources:…

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]