This is the 26th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Mybatis design mode

preface

Although there are 23 kinds of design patterns in 3 categories, most of them stay at the conceptual level. Mybatis source code uses a large number of design patterns. By observing the application of design patterns in them, we can have a deeper understanding of design patterns.

Mybatis uses at least the following design patterns:

model Mybatis reflect
Builder pattern For example SqlSessionFactoryBuilder, Environment;
Factory method pattern Examples are SqlSessionFactory, TransactionFactory, LogFactory
The singleton pattern Examples are ErrorContext and LogFactory;
The proxy pattern The core Mybatis implementation, MapperProxy, ConnectionLogger, JDK dynamic proxy and executor. Loader package use cglib or Javassist for lazy loading
Portfolio model Such as SqlNode and each subclass ChooseSqlNode;
Template method pattern Such as BaseExecutor and SimpleExecutor, BaseTypeHandler and all the subclasses such as IntegerTypeHandler;
Adapter mode For example, the Mybatis interface of Log and its adaptation to JDBC, LOG4j and other logging frameworks;
Decorator pattern For example, implementations of individual decorators in the cache.decorators subpackage of the Cache package;
Iterator pattern For example, the iterator pattern PropertyTokenizer;

1 Builder mode

The Builder pattern is defined as “separating the construction of a complex object from its representation so that the same construction process can create different representations.” , it belongs to create the class mode, generally speaking, if an object construction is complicated, is beyond the scope of the constructor can contain, you can use the factory pattern and the Builder pattern, relative to the factory pattern will produce a complete product, the Builder is applied to build more complex object, even will only build a part of the product, In plain English, a complex object is built step by step from multiple simple objects

Example: Producing a computer using the Builder design pattern

  • 1. Divide the target class to be constructed into multiple components (computer can be divided into host, monitor, keyboard, speaker and other components);
  • 2. Create a build class.
  • 3. Create parts in turn;
  • Assemble parts into target objects

1. Define the computer

2. ComputerBuilder

call

2. Embodiment in Mybatis

Build SqlSessionFactory

Initialization of Mybatis is more complex than a single constructor. So we use Builder mode, we use a lot of builders, we build layers, and the core object Configuration is built using XmlConfigBuilder.

During the initialization of the Mybatis environment, SqlSessionFactoryBuilder calls XMLConfigBuilder to read all mybatismapconfig. XML and all * mapper. XML files. Build the core object of Mybatis to run the Configuration object, and then use this Configuration object as a parameter to build an SqlSessionFactory object.

When XMLConfigBuilder builds the Configuration object, XMLMapperBuilder is also called to read the Mapper file. The XMLMapperBuilder uses the XMLStatementBuilder to read and build all SQL statements.

// Parse the 
       tag
mapperElement(root.evalNode("mappers"));
Copy the code

A similar feature in this process is that these Builders read files or configurations and then do a lot of XpathParser parsing, configuration or syntax parsing, reflecting generated objects, caching results, and so on, much more than a constructor can do. Therefore, a large number of Builder model to solve

The SqlSessionFactoryBuilder class builds the factory object SqlSessionFactory based on different input parameters. As a result, Mybatis will use Builder mode, and other design modes will be updated in the future