The source code of Mybatis has a clear hierarchy and uses a variety of design patterns. Learning the source code of Mybatis is of great help to improve our module design ability. Today, let’s take a look at myBatis source skeleton exactly what it looks like.

Mybatis source code import

1, mybatis source download address

MyBatis source code download: github.com/MyBatis/MyB…

2. Source code package import process:

  1. Download the Mybatis source code
  2. Check the Maven version, which must be at least 3.25
  3. The Mybatis project is a Maven project. The JDK must be version 1.8
  4. Pom file summary add true to all false
  5. Run the MVN clean install -dmaven.test. skip = true command in the project directory to install the current project to the local repository

Two, Mybatis source code overall architecture

1, the overall structure of the source code

Mybatis source code is mainly divided into three layers, the basic support, core processing layer and interface layer

  • Basic support layer: Technical components focus on the implementation of the underlying technology, and provide generic components for the upper business, with strong versatility and no business meaning;
  • Core processing layer: The business component focuses on the business process implementation of MyBatis and depends on the basic support layer;
  • Interface layer: Unified access interface provided by MyBatis makes it easy to develop and allows developers to program for SqlSession;

2. Why is myBatis design layered?

1. Code and systems are more maintainable.

System layer, each level has its own positioning, each level of internal components have their own division of labor, the system will become very clear, maintenance is very clear;

2. Facilitate the division of labor of the development team and improve the development efficiency;

Mybatis, for example, such a big source framework can’t be a person, he needs a team, the team must have a division of labor between, given the level of division, the division of labor will become easy, developers can focus on a certain layer of a module implementation, improved concentration, nature will also improve development efficiency;

3. Improve system scalability and performance.

After system layering, we only need to make clear the calling interface between layers, then we can change from logical layering to physical layering. What happens when the system concurrency throughput comes up? To improve the scalability and performance of the system, we can deploy different layers on different server clusters, different components on different machines, and use multiple machines to withstand the pressure, which improves the performance of the system. When the pressure is high, the expansion node is added to the machine, when the pressure is small, the compression node is reduced to the machine, the scalability of the system is so;

3. Appearance mode (facade mode)

From the source code architecture analysis, especially the design of the interface layer, it can be seen that the overall architecture of MyBatis conforms to the facade pattern. Facade pattern definition: Provides a unified interface for accessing a set of interfaces in a subsystem. The facade pattern defines a high-level interface to make the subsystem easier to use. The class diagram below

Facade pattern: Provides a unified interface for accessing a set of interfaces in a subsystem. The facade pattern defines a high-level interface to make the subsystem easier to use.

Facade role: Provides an external interface that provides easy client access externally and internally that provides access to all functions in the subsystem.

SubSystem: a SubSystem can be one or more modules in the entire system. Each module is composed of several classes. These classes may have more complex relationships with each other.

Advantages:

The interface of the complex subsystem is made simple and available, and the dependence of the client on the subsystem is reduced to achieve the effect of decoupling.

Demeter’s law of the six principles is followed, which encapsulates the details internally and exposes only the necessary interfaces externally.

Usage scenario:

A complex module or subsystem provides an interface for external access

Subsystems are relatively independent – external access to subsystems is only a black box operation

Three, the design pattern of six ideas

The purpose of learning source code in addition to learning programming skills and experience, the most important thing is to learn the design of the source code and the flexible application of design patterns, so before learning source code it is necessary to in-depth understanding of several principles of object-oriented design, so that they have a good design ideas and ideas;

1. Single responsibility principle

A class or interface is only responsible for a single function, try to design a single function interface;

2. Rely on the reverse principle

High – level modules should not depend on the implementation of low – level modules to decouple high – level and low – level modules. Interface oriented programming, when the implementation changes, only need to provide a new implementation class, do not need to modify the high-level module code;

3. Open-closed principle

The program is open for external expansion and closed for modification; In other words, when requirements change, we can add new modules to meet the new requirements, rather than changing the original implementation code to meet the new requirements;

4. Demeter’s Law

An object should have minimal knowledge of other objects and minimize coupling between classes. To realize this principle, we should pay attention to two points. On the one hand, when doing the class structure design, we should try to reduce the access rights of members, and use private as far as possible. In addition, there should be no dependencies between classes if direct calls are not necessary; Again, this law emphasizes loose coupling between classes;

5. Richter’s substitution principle

All references to the base class (parent) must transparently use the objects of its children;

6. Interface isolation principle

A client should not rely on interfaces it does not need. A class’s dependency on another class should be based on the smallest interface.

Today as Mybatis source code appetizer, let you review the basic principles of the design pattern and the basic architecture of the design of Mybatis, the next chapter we use the data structure and source code of Mybatis log module analysis!