class=”markdown_views prism-atom-one-light”>
Get started quickly with Mybatis
- 1. Mybatis Quick Start
- Second, Mybatis add, delete, modify and check
- Third, Mybatis core configuration file overview
- Fourth, Mybatis corresponding API
- 5. Dao layer implementation of Mybatis
- 6. In-depth Mybatis mapping file
- Seven, Mybatis core configuration file in-depth
- Eight, Mybatis multi-table operation
- Nine, Mybatis annotation development
- 10. SSM framework integration
- Eleventh, Extra Story
1. Mybatis Quick Start
1. Add Mybatis coordinates
dependency
groupIdorg.mybatis</groupId
artifactIdmybatis</artifactId>
version3.4.6</version
</dependency
dependency
groupIdmysql</groupId>
artifactIdmysql-connector-java</artifactId
version8.0.26</version
</dependency
2. Create a data table
CREATE DATABASE Mybatis;
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(255),
gender CHAR(1),
email VARCHAR(255)
)
3. Write entity classes
package com.mybatis.domain;
public class Employee {
private Integer id;
private String lastname;
private String email;
public String gender;
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastname='" + lastname + '\'' +
", email='" + email + '\'' +
", gender='" + gender + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
4. Write the mapping file
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"
mapper namespace="employeeMapper"
select id="findAll" resultType="com.mybatis.domain.Employee"
select * from tbl_employee
</select
</mapper
5. Write core files
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"
> /result #Common field
#collection: Handle one-to-many mapping relationship
#ofType: Indicates the type of data stored in the collection corresponding to this attribute
collection property='' ofType='emp type'
id property='' column='' /id #Primary key mapping, property attribute name, column field name
result property='' column='' /result #Common Field
/collection
/resultMap
2. Through collection(step by step query)
resultMap id='' type='' #id uniquely identifies type entity type
id property='' column='' /id #Primary key mapping, property attribute name, column field name
result property='' column='' /result #Common Field
#collection: Handle one-to-many mapping relationship
#ofType: Indicates the type of data stored in the collection corresponding to this attribute
collection property=''
ofType='emp type'
select=''
column=''>
/collection
/resultMap
#MyBatis cache
1. MyBatis's first-level cache (default enabled )
The first-level cache level is at the SqlSession level. The data queried through the same SqlSession will be cached. The next time the same data is queried, it will be directly obtained from the cache and will not be re-accessed from the database.
Four Situations That Invalidate the L1 Cache
1. Different SqlSessions correspond to different first-level caches
2. The same SqlSession but different query conditions
3. The same SqlSession performed an addition, deletion and modification operation during two queries
4. Manually cleared the cache during two queries of the same SqlSession
2. MyBatis's second level cache
The second-level cache is at the SqlSessionFactory level. The results of SqlSession queries created through the same SqlSessionFactory will be cached. The next time you query the same data, it will be directly obtained from the cache and will not be re-accessed from the database.
Conditions for enabling the second-level cache
1. In the configuration file, set the global configuration attribute cacheEnable = "true ", the default is true, no need to set
2. Set the label in the mapping file cache/
3. The second-level cache must be valid after the sqlsession is closed or submitted
4. The entity type converted by the queried data must implement the serialization interface
L2 cache invalidation
1. Arbitrary additions, deletions, and modifications between two queries will invalidate the first-level and second-level caches at the same time
Second level cache related configuration (cache/)
1.eviction attribute: cache recycling strategy
LRU (Least Recently Used) - Least Recently Used: Remove objects that have not been used for the longest time.
FIFO (First in First out)-first in first out : Remove objects in the order they entered the cache.
SOFT - soft references: Remove objects based on garbage collector state and soft reference rules.
WEAK - weak references: more aggressively remove objects based on garbage collector state and weak reference rules. The default is LRU.
2.flushInterval attribute: refresh interval 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
3.size attribute: reference number, positive integer
Represents how many objects the cache can store at most, too large will easily lead to memory overflow
4.readOnly attribute: read-only, true/false
true: read-only cache ; will return the same instance of the cache object to all callers. Therefore these objects cannot be modified. This provides a significant performance advantage.
false: read-write cache ; will return a copy of the cached object ( through serialization ). This will be slower, but safe, so the default is false.
3.MyBatis cache query order
1. Query the second-level cache first, because there may be data that other programs have checked out in the second-level cache, which can be used directly
2. If the second-level cache does not hit, then query the first-level cache
3. If the first level cache does not hit, then query the database
4. After the SqlSession is closed, the data in the first level cache will be written into the second level cache
number”>2. The same SqlSession but different query conditions
3. The same SqlSession performed an addition, deletion and modification operation during two queries
4. Manually cleared the cache during two queries of the same SqlSession
2. MyBatis’s second level cache
The second-level cache is at the SqlSessionFactory level. The results of SqlSession queries created through the same SqlSessionFactory will be cached. The next time you query the same data, it will be directly obtained from the cache and will not be re-accessed from the database.
Conditions for enabling the second-level cache
1. In the configuration file, set the global configuration attribute cacheEnable = “true “, the default is true, no need to set
2. Set the label in the mapping file cache/
3. The second-level cache must be valid after the sqlsession is closed or submitted
4. The entity type converted by the queried data must implement the serialization interface
L2 cache invalidation
1. Arbitrary additions, deletions, and modifications between two queries will invalidate the first-level and second-level caches at the same time
Second level cache related configuration (cache/)
1.eviction attribute: cache recycling strategy
LRU (Least Recently Used) – Least Recently Used: Remove objects that have not been used for the longest time.
FIFO (First in First out)-first in first out : Remove objects in the order they entered the cache.
SOFT – soft references: Remove objects based on garbage collector state and soft reference rules.
WEAK – weak references: more aggressively remove objects based on garbage collector state and weak reference rules. The default is LRU.
2.flushInterval attribute: refresh interval 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
3.size attribute: reference number, positive integer
Represents how many objects the cache can store at most, too large will easily lead to memory overflow
4.readOnly attribute: read-only, true/false
true: read-only cache ; will return the same instance of the cache object to all callers. Therefore these objects cannot be modified. This provides a significant performance advantage.
false: read-write cache ; will return a copy of the cached object ( through serialization ). This will be slower, but safe, so the default is false.
3.MyBatis cache query order
1. Query the second-level cache first, because there may be data that other programs have checked out in the second-level cache, which can be used directly
2. If the second-level cache does not hit, then query the first-level cache
3. If the first level cache does not hit, then query the database
4. After the SqlSession is closed, the data in the first level cache will be written into the second level cache