My official account is MarkerHub and my website is Markerhub.com

For more selected articles, please click: Java Notes Complete.md

Small Hub read:

Understand the relationship between each component of Mybatis, and then gradually build components, make a framework!


  • Author: My name is Liu Banxian
  • My.oschina.net/liughDevelo…

Source code address: github.com/qq53182347/…

Following my last handwritten SpringMVC post, I recently struck while the iron was hot and took a look at Mybatis. The core function of MyBatis framework is not difficult, nothing more than dynamic proxy and JDBC operations, difficult is to write extensible, high cohesion, low coupling standard code. Mybatis function completed in this article is relatively simple, the code still has many places to improve, we can combine Mybatis source code to begin to improve.

I. Brief introduction of Mybatis framework process

Before writing my Mybatis framework, we will first understand Mybatis, its source code used a large number of design patterns, read the source code and observe the application of design patterns in which, to be able to understand the source code more deeply (REF: Mybatis source code interpretation – design pattern summary). Let’s summarize the above analysis:

Mybatis configuration files have 2 types

  • Mybatisconfig. XML, the name of the configuration file is not fixed, and the configuration of global parameters can only have one configuration file globally.
  • Mybatis framework can have multiple Mappe. XML configuration files.

SqlSessionFactory = SqlSessionFactory = SqlSessionFactory

SqlSessionFactory SqlSession, SqlSession can be used to manipulate data.

SqlSession is implemented through the underlying Executor, which has two classes:

  • Basic implementation

  • Implementation with caching capabilities

5. MappedStatement is an object generated by defining statement in mapper. XML.

6. Parameter input executes and outputs the result set, without manually judging the parameter type and parameter subscript position, and automatically maps the result set to Java objects

  • HashMap, data type in KV format
  • Java basic data types
  • Pojos, Java objects

Second, sort out the design ideas of Mybatis

According to the Mybatis process above, I have simplified it and divided it into the following steps:

1. Read the XML file and establish a connection

As you can see from the figure, MyConfiguration is responsible for interacting with people. After the XML is read, the properties and connection to the database are encapsulated in a MyConfiguration object for later components to call. This article will use DOM4J to read XML files, which has excellent performance and is very easy to use.

2. Create an SqlSession to bridge the Configuration and Executor

We often see sessions when we’re using frameworks, but what exactly is a Session? A Session has only one corresponding database connection. Similar to a front-segment Request Request, it can directly call exec(SQL) to execute SQL statements. As you can see from the arrows in the flow chart, the MySqlSession member variables must have MyExecutor and MyConfiguration for centralized deployment. The arrows act as an association. Our own MySqlSession will have a getMapper method, and after using the dynamic proxy to generate objects, we can do database operations.

3. Create Executor to encapsulate the JDBC operation database

Executor is an Executor that is responsible for generating SQL statements and maintaining the query cache (the cache is not yet complete), which is where the JDBC code will be done, but this article only implements a single table, so you can try to complete multiple tables if you are interested.

4. Create MapperProxy and use dynamic proxy to generate Mapper objects

We just want to generate an object for the specified interface that can run an SQL statement when executing it. The interface can’t call a method directly, so we use a dynamic proxy to generate the object, call the query back to MySqlSession at execution time, and eventually MyExecutor does the JDBC query. It is designed for a single responsibility and is more scalable.

Three, implement their own Mybatis

Project Documents and Contents:

First, create a new Maven project and import the following dependencies in POM.xml:

To create our database XML configuration file:

Create the test library in the database and execute the following SQL statement:

Create the User entity class with the UserMapper interface and the corresponding XML file:

With the basic operations configured, let’s implement MyConfiguration:

After reading the XML configuration with an object-oriented design:

The Function object includes the type of the SQL, method name, SQL statement, return type, and parameter type.

To implement our MySqlSession, the first member variable has to be Excutor and MyConfiguration. The essence of the code is in the getMapper method.

Next create Excutor and the implementation class:

MyExcutor encapsulates JDBC operations:



The MyMapperProxy proxy class completes the mapping between XML methods and real methods, and performs the query:

Here, you have completed your Mybatis framework, let’s test it:

Execution Result:

Query a user that does not exist

This is where we’re done!

I am an ordinary programmer, the level is limited, the article inevitably has mistakes, welcome to sacrifice their precious time readers, on the content of this article, my purpose is just hope to help readers.

Recommended reading

Java Notes Complete.md

Great, this Java site, everything! https://markerhub.com

The UP master of this B station, speaks Java really good!

Too great! The latest edition of Java programming ideas can be viewed online!