MyBatis source process analysis

mybatis core processes three stages

 

 Mybatis initialization mode builder

Model Builder (Builder Pattern) using a plurality of simple objects step by step to construct a complex object. This type of design pattern belongs create schema, which provides the best way to create objects.

 

 

Complex objects to be created: Product

Builder: given an abstract interface to standardize the construction of the various components of target products. What this interface to create a predetermined portion of the complex objects to be achieved, does not involve creating specific object member;

ConcreteBuilder: achieve Builder interface, specific business logic created for different parts of the complex objects. After the completion of the construction process, providing examples of products;

Director: call the builder to create specific parts of complex objects, that did not involve specific products in the instructor, the object is only responsible for ensuring that each part of a complete or create a certain order;

 

Builders mode uses Scene

  • The need to generate objects with complex internal structure, when an object is instantiated internal details of the object to be blocked, so that the instantiation process decouple the upper code complex objects, you can use the builder pattern; in short, if "encounter when a plurality of configuration parameters to consider builder ";

  Examples of target-dependent generation of individual components and assembly sequence, attention is assembled step by step the target object, may be used to build mode;

The difference between the builder pattern and factory pattern:

 

 

Mybatis initialization

 

 

MyBatis class diagram builder

 

 

The key class Mapper

Configuration: Mybatis start core initialization is to load all xml configuration file information into the Configuration object, a single Configuration example, life cycle application level;

MapperRegistry: registry mapper interfaces dynamic proxy class factory. In MyBatis, the interfaces implemented InvocationHandler mapperProxy, MapperProxyFactory for instance generate a dynamic proxy objects;

ResultMap: resultMap for parsing node mapper.xml file using ResultMapping encapsulated id, result other sub-elements;

MappedStatement: for storing mapper.xml file select, insert, update and delete nodes, and also contains a number of important properties of these nodes;

SqlSource: mapper.xml file sql statement will be parsed into SqlSource object parsed statements contained in the final SqlSource contains only? Placeholder can be submitted directly to the database to perform;

 

The configuration class diagram

 

 ResultMap illustration

 

 

mappedStatment illustration

 

 

MyBatis initialization process

 

 

 Acting stage

 

 

binding module analyzes

 

 

MapperRegistry: mapper interface and the corresponding registry proxy object factory;

MapperProxyFactory: instance of an object mapper for dynamic interface generation agent;

MapperProxy: InvocationHandler implements an interface, it is an enhancement to achieve mapper interface;

MapperMethod: Mapper interface encapsulates information of the corresponding method, and an information corresponding to sql statement; it is the bridge interface mapper mapping configuration file sql statement;

 

From XMLMapperBuilder.bindMapperForNamespace () start start

 

Interpretation MapperMethod

MapperMethod : Mapper interface encapsulates information of the corresponding method, and an information corresponding sql statement; it is the bridge interface mapper mapping configuration file sql statement; MapperMethod object does not record any state information, it can represent a plurality of object sharing between;

   SqlCommand: Gets the namespace configuration method from the method name and the type of SQL statements;.

   MethodSignature: information in mapper interface method (the parameters, return type);

   ParamNameResolver: Analytical mapper interface method into a reference;

 

Translation process

 

 sqlsession related classes

Create a strategy pattern SqlSession

Strategy Mode (Strategy Pattern) Strategy pattern defines a set of algorithms, and each algorithm package together, but also so that they can replace each other, so that the algorithm is independent of the clients that use it independently change.

 

 

Context: algorithm caller, using setStrategy flexible method of selection policy (strategy);

Strategy: a unified interface algorithm;

ConcreteStrategy: specific algorithm implementation;

 

Using the Scene mode strategy:

  A variety of treatments for the same type of problem, only difference is the specific behavior;

  Appear the same abstract class multiple sub-categories, but need to use if-else or switch-case time to select a specific sub-class.

 

SqlSession related classes UML

SqlSession is the most critical core interface MyBaits provided externally, through which you can perform a database read and write commands, get mappers, management affairs;

 

 

SqlSession query interfaces nesting relationship

 

 The core components of executor

Executor component analysis

Executor is one of MyBaits core interface defines the basic database operation method, SqlSession functions are implemented based on it;

 

 

Template mode

Template pattern (Template Pattern): An abstract class defines discloses a method of performing its info / template. Its subclass can override methods as needed to achieve, but the invocation will be defined in the abstract class. The algorithm defines a skeleton of the operation, some steps to subclasses delay. Template Method lets subclasses may not change the structure of an algorithm to redefine the specific implementation of the algorithm;

 

 BaseExecutor code analysis template mode scenario

BaseExecutor: abstract class that implements most of the methods executor interface, which offers the ability to cache management and transaction management, and other abstract methods need to be implemented as a subclass: doUpdate, doQuery and other methods;

Encounter process consists of a series of steps need to be performed constituted, the process from the high-level point of view is the same, but to achieve some steps may be different, this time on the need to consider the use of the template pattern. For example: Executor query operation process:

 

 

Interpretation of three implementation class Executor

  SimpleExecutor: the default configuration, use PrepareStatement object to access the database, each visit must create a new PrepareStatement objects;

  ReuseExecutor: using precompiled PrepareStatement Object Access database, Access, will reuse statement objects in the cache;

  BatchExecutor: the ability to achieve batch execute multiple SQL statements;

Executor of three important younger brother

By interpretation of SimpleExecutor doQuery () method is found, the Executor a commander, it is scheduled younger three tasks:

  StatementHandler: its role is to use the Statement PrepareStatement database or perform operations, Kai nexus effect;

  ParameterHandler: pre-compiled SQL statement to set parameters, SQL statement placeholders corresponds to an element BoundSql.parameterMappings collection, recording a corresponding parameter names and associated attributes of the parameters of the object "?"

  ResultSetHandler: result set returned to the database (the ResultSet) encapsulates return entity type designated by the user;

Executor inner workings of the process

 

 

StatementHandler analysis

StatementHandler complete the core work Mybatis, the foundation also Executor implemented; features include: Creating a statement object binding parameters for the sql statement, such as the implementation of CRUD SQL statement, the result set mapping transformation;

 

 

BaseStatementHandler: All abstract parent class subclass, initialization statement defines the sequence of operations to achieve different specific instantiation statement (template pattern) by a subclass;

RoutingStatementHandler: Excutor real component is instantiated subclass, use a static proxy mode, which create specific entity class determined from the context;

SimpleStatmentHandler: Object Access database using a statement, without parameterization;

PreparedStatmentHandler: using precompiled PrepareStatement Object Access database;

CallableStatmentHandler: call a stored procedure;

 

ResultSetHandler analysis

The results from the database query ResultSetHandler obtained according to a mapping rule mapping configuration file, into a corresponding result set object;

 

Guess you like

Origin www.cnblogs.com/Soy-technology/p/11462673.html