Article directory
Mybatis development process
Traditional development brief process
-
Import mybatis dependencies
-
Configure mybatis-config.xml
-
Placement mapper.xml
-
Define an interface and its implementing class
Brief process of interface proxy development
- Import mybatis dependencies
- Configure mybatis-config.xml
- Placement mapper.xml
- define interface
Six points for attention in interface development
-
The mapping file directory is the same as the directory where the interface is located (inconsistency requires specifying the address mapper-locations:classpath: )
-
The mapping file name is the same as the interface name
-
namespace is set to the fully qualified class name of the interface
-
id is the same as the interface method name
-
parameterType should be consistent with the interface parameter type (generally not written, write it correctly)
-
resultType is the same as the return value type of the interface method
sqlMapperConfig core configuration file
mybatis related API
input-output mapping
If the database table field name is inconsistent with the entity class attribute name, there are three ways to solve it: setting aliases, setting camel case rules, and resultMap.
Commonly used dynamic Sql tags
动态sql执⾏原理
1. SqlResource
该接⼝含义是作为sql对象的来源,通过该接⼝可以获取sql对象。其唯⼀的实现类是
XmlSqlResource,表示通过xml⽂件⽣成sql对象。
2. Sql
该接⼝可以⽣成sql语句和获取sql相关的上下⽂环境(如ParameterMap、ResultMap等),有三个
实现类: RawSql表示为原⽣的sql语句,在初始化即可确定sql语句;SimpleDynamicSql表示简单
的动态sql,即sql语句中参数通过$property$⽅式指定,参数在sql⽣成过程中会被替换,不作为
sql执⾏参数;DynamicSql表示动态sql,即sql描述⽂件中包含isNotNull、isGreaterThan等条件
标签。
3. SqlChild
该接⼝表示sql抽象语法树的⼀个节点,包含sql语句的⽚段信息。该接⼝有两个实现类: SqlTag表
示动态sql⽚段,即配置⽂件中的⼀个动态标签,内含动态sql属性值(如prepend、property值 等);SqlText表示静态sql⽚段,即为原⽣的sql语句。每条动态sql通过SqlTag和SqlText构成相应的
抽象语法树。
4. SqlTagHandler
该接⼝表示SqlTag(即不同的动态标签)对应的处理⽅式
5. SqlTagContext
⽤于解释sql抽象语法树时使⽤的上下⽂环境。通过解释语法树每个节点,将⽣成的sql存⼊
SqlTagContext。最终通过SqlTagContext获取完整的sql语句。
step-by-step query
What is a step query: Concept: Split a multi-table query into multiple single-table queries to pave the way for lazy loading
lazy loading
Lazy loading in MyBatis, also known as lazy loading, refers to delaying the select query on the associated object according to the set delay rule when performing the associated query of the table.
For example, when performing a one-to-many query, only one party is queried. When the program needs data from multiple parties, mybatis will issue an sql statement to query, so that sub-delayed loading can reduce database pressure.
The lazy loading of MyBatis only has a delay setting for the query of the associated object, and the query statement is directly executed for the main loaded object.
Note: The application requirements of lazy loading: the query of the associated object and the query of the main loaded object must be separate select statements, not
is a select query using a multi-table join
Lazy loading fails
对象调用 toString hashcode equals 等方法都会使的延迟加载失效
Lazy Loading Principle
Use CGLIB to create a proxy object of the target object. When calling the target method, enter the interceptor method, such as calling A.getB().getName(). The interceptor invoke() method finds that A.getB() is a null value, then It will send the sql of the pre-saved query associated with the B object separately, query B, and then call A.setB(b), so the object b attribute of a has a value, and then complete A.getB().getName() method call.
caching mechanism
Benefits: Reduce the number of database queries and improve execution efficiency
Disadvantage: It affects the timeliness of data and will generate dirty data
二级缓存默认是关闭的,当设置了cacheEnabled=true后,在mapper.xml中设置cache标签就会去创建一个cacah对象,所以同一个mapper的mapperStatement公用同一个cache对象
Level 1 Cache Working Mechanism
一级缓存 是 SqlSession级别的缓存。在操作数据库时需要构造sqlSession对象,在对象中有一个数据结构用于存储缓存数据。
不同的sqlSession之间的缓存数据区域是互相不影响的。也就是他只能作用在同一个sqlSession中,不同的sqlSession中的缓存是互相不能读取的
L2 cache mechanism
二级缓存是 mapper 级别的缓存,多个 SqlSession 去操作同一个Mapper的sql语句,多个 SqlSession可以共用二级缓存,二级缓存是跨 SqlSession 的。
UserMapper有一个二级缓存区域(按 namespace 划分),每个 mapper 也有自己的二级缓存区域(按namespace分)。每一个 namespace 的 mapper 都有一个二级缓存区域,如果相同两个 mapper 的 namespace ,这两个mapper执行sql查询到数据将存在相同的二级缓存区域中。