The MyBatis notes circulated inside JD.com are short and concise, full of source code details

foreword

More and more companies have used MyBatis in a formal production environment. I think the reason for its popularity is that most projects are table-oriented programming , and Java objects are only used as data containers, and queries and model changes are designed in On a table, the so-called business logic is a bunch of sql collections that are added, deleted, modified, and checked, so Mybatis is very convenient to use.

Why learn MyBatis?

In addition to what I said above that MyBatis is very popular in China, there are the following points:

①Learning MyBatis well can help us solve the problem of data layer development;

There are many design patterns and concurrent programming skills in the MyBatis source code. Learning the source code well is of great help to improve the coding ability ;

③Finally, the source code of Mybatis is also a frequently asked point in interviews with big factories.

Based on the above situation, this time I will share with you the MyBatis notes privately stored by Ali p8 , to help you learn MyBatis-related knowledge from the shallower to the deeper, and enjoy the details of the source code, so that developers not only know what it is , but also why it is .

Note: Due to the large amount of content, some parts will be shown this time. If you are not satisfied with reading and want to have a deeper understanding of this notebook and thoroughly grasp the underlying principles of MyBatis, you can get it by thinking in the background [717]! !

This article is divided into two parts, but I want to go against the routine. First, I will show you the source code, and then I will show you the overall learning content of MyBatis. There are not many bbs, so I will go directly to it.

A: Sophisticated MyBatis source code analysis - overall architecture

①Overall structure

  • project structure

  • Overall structure

  • base support layer

  • core processing layer

  • interface layer

B: Refined MyBatis source code analysis - basic support layer

① Basic support layer

  • parser module

  • reflection module

  • exception module

  • data source module

  • transaction module

  • cache module

  • type module

  • I/O module

  • log module

  • annotation module

  • Binding module

C: Refined MyBatis source code analysis - MyBatis initialization (1) loading mybatis-config.xml

①MyBatis initialization

②Initialization (1) loading mybatis-config.xml

  • SqlSessionFactoryBuilder

  • XMLConfigBuilder

  • Configuration

D: Refined MyBatis source code analysis - MyBatis initialization (2) loading Mapper interface and XML mapping file

①MyBatis initialization

②Initialization (2) Load Mapper interface and mapping file

  • Analysis entry

  • MapperAnnotationBuilder

  • XMLMapperBuilder

  • XMLStatementBuilder

  • MapperBuilderAssistant

  • RequestMapping

  • ResultMap

  • MappedStatement

E: Refined MyBatis source code analysis - MyBatis initialization (3) SQL initialization (on)

①MyBatis initialization

②Initialization (3) SQL initialization (on)

  • LanguageDriver

  • XMLScriptBuilder

  • NodeHandler

  • DynamicContext

  • SqlNode

  • EachlCache

F: Refined MyBatis source code analysis - MyBatis initialization (4) SQL initialization (below)

①MyBatis initialization

②Initialization (4) SQL initialization (below)

  • SqlSourceBuilder

  • ParameterExpression

  • ParameterMapping

  • SqlSource

  • BoundSql

  • DefaultParameterHandler

G: Exquisite MyBatis source code analysis - Executor of SQL execution process (1)

① MyBatis SQL execution process

②Executor of SQL execution process (1)

  • Executor

  • BaseExecutor

  • SimpleExecutor

  • ReuseExecutor

  • BatchExecutor

  • L2 cache

  • Where Executor is created

H: Exquisite MyBatis source code analysis - StatementHandler of SQL execution process (2)

① MyBatis SQL execution process

②SQL execution process (2) StatementHandler

  • StatementHandler

  • RoutingStatementHandler

  • BaseStatementHandler

  • SimpleStatementHandler

  • PreparedStatementHandler

  • CallableStatementHandler

  • KeyGenerator

  • Jdbc3KeyGenerator

  • SelectKeyGenerator

  • NoKeyGenerator

I: Exhaustive MyBatis source code analysis - ResultSetHandler of SQL execution process (3)

① MyBatis SQL execution process

②SQL execution process (3) ResultSetHandler

  • ResultSetWrapper

  • ResultSetHandler

  • DefaultResultSetHandler

J: Exquisite MyBatis source code analysis-lazy loading of SQL execution process (4)

① MyBatis SQL execution process

②Delayed loading of SQL execution process (4)

  • ResultLoader

  • ResultExtractor

  • ResultLoaderMap

  • ProxyFactory

  • JavassistProxyFactory

  • CglibProxyFactory

K: Exquisite MyBatis source code analysis - SqlSession session and SQL execution entry

①SqlSession session and SQL execution entry

  • SqlSessionFactoryBuilder

  • DefaultSqlSessionFactory

  • DefaultSqlSession

  • MapperMethod

O: Exhaustive MyBatis source code analysis - plug-in mechanism

① Plug-in mechanism

  • Implant the plugin logic

  • Pagination plugin example

  • Interceptor

  • Invocation

  • Plugin

  • InterceptorChain

P: Exhaustive MyBatis source code analysis- MyBatis-Spring source code analysis

①Configuration example

②SqlSessionFactoryBean

③MapperFactoryBean

④SqlSessionDaoSupport

⑤MapperScannerConfigurer

⑥ClassPathMapperScanner

⑦ @MapperScan annotation

⑧Custom <mybatis:scan /> tag

⑨SqlSessionTemplate

⑩SqlSessionHolder

⑪SqlSessionUtils

⑫SqlSessionSynchronization

Q: Exhaustive MyBatis source code analysis - Spring-Boot-Starter source code analysis

①Configuration example

②MybatisProperties

③SpringBootVFS

④MybatisAutoConfiguration

⑤Construction method

  • afterPropertiesSet method

  • sqlSessionFactory method

  • sqlSessionTemplate method

  • MapperScannerRegistrarNotFoundConfiguration

  • AutoConfiguredMapperScannerRegistrar

⑥spring.factories file

A: MyBatis quick start

  • Why do you need an ORM framework?

  • MyBatis Quick Start

B: MyBatis development points

Easily grasp the main points, you will clearly know:

①Is it resultType or resultMap?

② How to pass multiple parameters?

③How to get the primary key?

④ Understand SQL elements and SQL parameters, dynamic SQL, code generator, relational query, cache and other knowledge.

C: Overview of MyBatis source code

A preliminary exploration of the source code paves the way for in-depth study of the MyBatis source code in the future. From here you will learn:

①How to download MyBatis source code

② Source code architecture analysis

③ Appearance mode (facade mode)

④Six design principles that need to be followed in object-oriented design

D: Log module analysis

MyBatis does not provide a log implementation class and needs to be connected to a third-party log component. However, third-party log components have their own log levels, and they are different. MyBatis provides four levels: trace, debug, warn, and error.

Therefore, you need to master the following 5 points:

①Log module requirements analysis

②Adapter mode

③How to achieve priority loading of log components?

④Proxy mode and dynamic proxy

⑤ Elegant enhanced log function

E: Data source module analysis

The data source module focuses on the creation of data sources and the source code analysis of database connection pools; the creation of data sources is more complicated. For the creation of complex objects, you can consider using the factory model to optimize.

This chapter will mainly introduce the following 4 points:

①Simple factory mode

②Factory mode

③Creation of data source

④Analysis of database connection pool technology

F: Cache module analysis

①MyBatis cache module needs to meet the following requirements:

  • The implementation of MyBatis cache is based on Map, and reading and writing data from the cache is the core basic function of the cache module;

  • In addition to the core functions, there are many additional additional functions, such as: preventing cache breakdown, adding cache clearing strategies (fifo, lru), serialization functions, logging capabilities, timing emptying capabilities, etc.;

  • Additional functionality can be added to the core base functionality in any combination.

Based on the core cache capability of Map, it is the biggest problem for the Mybatis cache module to elegantly enhance the capabilities of blocking, clearing strategies, serialization, logs, etc. in any combination. It is the tradition to expand multiple additional capabilities by means of dynamic proxy or inheritance. The method has the following problems:

These methods are static, and users cannot control the method and timing of adding behaviors; in addition, there are many combinations of new functions, and the use of inheritance may lead to the existence of a large number of subclasses. To sum up, the MyBtis cache module uses the decorator pattern to implement the cache module.

② Decorator mode

③The use of decorators in the cache module

④The unique identifier of the cache CacheKey

G: Reflection module analysis and MyBatis process

Phase 1: Configuration Loading Phase

① Builder mode

  • What is the builder pattern

  • Difference from factory mode

② Configure the core class loaded

  • The three core classes of the builder

  • About the Configuration object

③ Configuration loading process

The second stage: agent encapsulation stage

①Mybatis interface layer

  • SqlSession

  • strategy pattern

  • SqlSessionFactory

②binding module analysis

  • binding module core class

  • Binding module running process

The third stage: data access stage

①About the Executor component

② Template mode in Executor

③Three important brothers of Executor

④About StatementHandler

⑤About ResultHandler

The principle of combining with spring

①What is MyBatis-Spring

②MyBatis-Spring integration configuration best practice

③MyBatis-Spring integration principle analysis

plug-in development

① Understand plug-ins

②Quick start of plug-in development

③Chain of Responsibility Model

④ Plug-in module source code analysis

Obtaining method: You need this Ali technical officer's private collection "MyBatis Notes"  and you can get it by thinking in the background [717]!!!

Guess you like

Origin blog.csdn.net/xxxzzzqqq_/article/details/130641147