Summary of 5 design patterns in the MyBatis framework

Preface

The five design patterns used in the MyBatis framework are:1. Builder mode (generator mode). 2. Factory mode. 3. Singleton mode. 4. Agent mode. 5. Adapter mode.

1. Builder mode (generator mode)

During the initialization process of the MyBatis environment, SqlSessionFactoryBuilder will call XMLConfigBuilderread

AllMyBatisMapConfig.xml and all files, build the core objects for MyBatis operation *Mapper.xml

Configuration object, and then construct a pair using the Configuration object as a parameterSqlSessionFactory

elephant.

2. Factory mode

  • In MyBatis, for example SqlSessionFactory uses the factory pattern. The factory does not have such complicated logic and is a simple factory pattern.

  • SqlSessioncan be considered as a core interface for MyBatis work. Through this interface, you can execute SQL Statements, getting Mappers, managing transactions. Similar to the object connecting to MySQL. Connection

3. Singleton mode

  • There are two places where the singleton mode is used in MyBatis, ErrorContext and LogFactory, among which ErrorContext​​ ​​​​​It is a singleton used within the scope of each thread to record the execution environment error information of the thread, and is used to obtain log objects configured for the project. LogFactory is a log factory provided to the entire MyBatis.

 public class ErrorContext {
     /**
      * 每个线程各自的数据
      */
     private static final ThreadLocal<ErrorContext> LOCAL = new ThreadLocal<>();
 ​
     private ErrorContext() {
     }
 ​
     public static ErrorContext instance() {
         ErrorContext context = LOCAL.get();
         if (context == null) {
             context = new ErrorContext();
             LOCAL.set(context);
         }
         return context;
     }
 ​
 }
  • The constructor is privately modified, with a static local instance variable and a method to obtain the instance variable. In the method of obtaining the instance, first determine whether it is empty, and if so, create it first, and then return the constructed object.

  • The only interesting thing here is that the static instance variable of LOCAL is modified with ThreadLocal, which means that it belongs to each thread. The data, and in the instance() method, first obtain the instance of this thread, if not, create the thread's unique ErrorContext.

4. Agent mode

The proxy mode can be considered as the mode used by the core of MyBatis. It is precisely because of this mode that we only need to write Mapper.javainterface and do not need to implement it. , MyBatis background helps us complete the execution of specific SQL.

5. Adapter mode

  • In the MyBatis logging package, there is a Log interface:

    This interface defines the logging method used directly by MyBatis, and who specifically implements the Log interface?

  • MyBatis providesa variety of log framework implementations. These implementations all match the interface methods defined by this Log interface. Ultimately Implements the adaptation of all external log frameworks to the MyBatis log package.

Guess you like

Origin blog.csdn.net/qq_30713721/article/details/134904721
Recommended