MyBatis implementation and plug-in development

Before analyzing the source code also need to download and install the source code to a local warehouse and development tools, making it easy to add comments to the code; installation process and the installation process mybatis source is the same, the description is not repeated here; Download: HTTPS: // github.com/mybatis/spring

  1, SqlSessionFactoryBean source code analysis

  2, MapperFactoryBean source code analysis

  3, MapperScannerConfigurer source code analysis

 

Plug-in Development Principle

Plug-ins are used to change or extend the existing functionality of mybatis, mybaits plug-in is through inheritance Interceptor interceptor implementation;

Prohibit the use of plug-mybaits be extended until the plug is not fully understood, it may cause serious problems;

mybatis plug-in can be used to intercept and interfaces as follows:

  Executor(update、query 、 flushStatment 、 commit 、 rollback 、 getTransaction 、 close 、 isClose)

  StatementHandler(prepare 、 paramterize 、 batch 、 update 、 query)

  ParameterHandler( getParameterObject 、 setParameters )

  ResultSetHandler( handleResultSets 、 handleCursorResultSets 、 handleOutputParameters )

Plug-in Development Quick Start

Define a threshold value, when the query running time exceeds the threshold value for logging operation and maintenance personnel positioning slow query, widget implementation steps:

  1, implement Interceptor Method

  2, determine the interception of signature

  3, configure the plug-in configuration file

  4, run the test case

Source code analysis of the responsibility chain mode

Chain of Responsibility pattern: each piece of work is to go through each node in the chain, so that these nodes in order to deal with this work; and the decorator pattern is different every node knows who the successor is; for to complete the same request requires multiple processing category scene;

 

 

 

Factor analysis

  Handler : defines a standard interface to a processing request;

  ConcreteHandler : specific handler, which is responsible for the processing section according to the service processing flow may be ended, a request may be forwarded to its successor;

  Client : sender, the client initiates the request terminal;  

Chain of Responsibility pattern advantages:

   Reduce the coupling. It requests the sender and receiver decoupling.

  Simplified object. Such that the object does not need to know the structure of the chain.

  Enhance the flexibility of assigning responsibilities to objects. By changing the members of the chain or the mobilization of their order, allows to dynamically add or delete liability.

  Adding a new class of easily processing a request.

 

mybatis plug-in module source code analysis

  Initialization plug (XMLConfigBuilder.pluginElement)

  Load plugin (Configuration.new * method to create four objects)

  Calling plug-in ( Plugin . Wrap, Plugin . The Invoke)

 

mybatis plug understanding:

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Interceptor.md

 

Mybatis pagination plug PageHelper

  

Use pagination plug-in;

  Chinese document: https: //github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md

  Manual: https: //github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

 

Notes pagination plug-in;

  Note: https: //github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Important.md

 

Source plug-page overview;

 

To realize his mybatis

MyBatis core processes three stages

 

 

Initialization phase

Read XML configuration files and information in the database configuration file is loaded into the configuration object;

 

 

SqlSession schematic

 

 

Saying SqlSession

Sqlsession means creating a database session, representing a connection to the database;

   MyBatis provide external data access is the main API ( try iBatis programmatically bar );

   In fact Sqlsession functions are implemented based on Excutor;

 

SqlSession query interface nested relationship:

 

 

Why should there be a proxy stage?

 

 

 

 
Executor component analysis

Executor is one of MyBaits core interface defines the basic database operation method; Executor operation of the database follow what specification?

 

 

A simplified version of MyBatis realization of ideas

 

 

 

Guess you like

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