[Spring Boot] Database persistence layer framework MyBatis — Introduction to MyBatis

Introduction to MyBatis

This section will first introduce what ORM is, what is MyBatis, the characteristics and core concepts of MyBatis, and finally how to start MyBatis and load the configuration file?

1.What is ORM

ORM (Object Relational Mapping) is a technology that solves the mismatch between object-oriented and relational databases. Simply put, an ORM automatically persists objects in a program to a relational database by using metadata that describes the mapping between the object and the database.

When we develop an application, we need to write a large amount of data access layer code to operate the data in the database. These codes are either a large amount of repeated code, or the operation is particularly cumbersome. To address these problems, ORM provides a complete solution, simplifying the operation of persisting objects into relational databases.

The essence of the ORM framework is to simplify the coding of operating databases in programming. With the development of the Java field, ORM frameworks have emerged in endlessly. However, Hibernate and Mybatis are basically the two most popular and widely used.

Hibernate: A fully automatic framework, powerful, complex, cumbersome, and costly to learn.

Mybatis: Semi-automatic framework (requires developers to understand the database), you must write SQL yourself.

Hibernate claims that there is no need to write a sentence of SQL, while MyBatis is good at dynamic SQL. Both have their own characteristics, and developers can use them flexibly according to their needs.

There is an interesting phenomenon: Most traditional companies like to use Hibernate, while the Internet industry usually uses MyBatis.

2.What is MyBatis

MyBatis is an excellent data persistence layer ORM framework that is widely used in application systems. It was originally iBatis, an open source project of Apache. In 2010, this project was moved to Google Code by the Apache Software Foundation and renamed MyBatis. In November 2013, it was moved to GitHub.

MyBatis supports customized SQL, stored procedures and advanced mapping, can implement dynamic SQL very flexibly, can use simple XML or annotations to configure and map native information, and can easily convert Java POJO (Plain Ordinary Java Object, ordinary Java objects) are mapped to tables and fields in the database.

As a widely used open source software, MyBatis has the following characteristics:

Easy to learn and use, without any third-party dependencies.

SQL is extracted uniformly to facilitate unified management and optimization.

Decoupling SQL from code separates business logic and data access logic, making the system design clearer, easier to maintain, and easier to perform unit testing.

Flexible and dynamic SQL supports various conditions to dynamically generate different SQL.

Provides mapping tags to support ORM relationship mapping between objects and databases.

Provides object-relational mapping tags to support object-relational component maintenance.

3.The core concept of MyBatis

MyBatis consists of Mapper configuration files, Mapper interfaces, executors, sessions and other components. These very important components and concepts are introduced below.

1) Mapper configuration file: It can be implemented using an XML-based Mapper configuration file, or using MyBatis annotations based on Java annotations, or even directly using the API provided by MyBatis.

2) Mapper interface: refers to a customized data operation interface, similar to the commonly known DAO interface. The early Mapper interface needed to be customized to implement. Now MyBatis will automatically create a dynamic proxy object for the Mapper interface. The methods of the Mapper interface usually correspond to the select, insert, update, delete and other XML nodes in the Mapper configuration file.

3) Executor (executor): All SQL statements in MyBatis are executed through Executor, which is a core interface of MyBatis.

4) SqlSession (session): The key object of MyBatis, similar to the Connection in JDBC. The SqlSession object completely contains all methods related to the database to perform SQL operations. Its bottom layer encapsulates the JDBC connection and can be directly used by the SqlSession instance. Execute the mapped SQL statement.

5) SqlSessionFactory (session factory): The key object of MyBatis, which is a compiled memory image of a single database mapping relationship. Instances of SqlSessionFactory objects can be obtained through the SqlSessionFactoryBuilder object class.

6) SqlSessionFactoryBuilder builder: used to parse configuration files, including property configuration, alias configuration, interceptor configuration, data source and transaction manager, etc., which can be built from an XML configuration file or a predefined configuration instance.

4.MyBatis startup process

Although MyBatis is simple to use, it is a highly encapsulated framework, so we must be familiar with the startup and execution process of MyBatis. The specific workflow is shown in the figure.

Insert image description here

1) Load the SQL mapping file configured by Mapper, or the relevant SQL content of the annotation.

2) Create a session factory. MyBatis constructs a session factory (SqlSessionFactory) by reading the data source information of the configuration file.

3) Create a session. MyBatis can create a session object (SqlSession) through the session factory. The session object is an interface that contains methods for adding, deleting, modifying, and querying database operations.

4) Create an executor. Because the session object itself cannot directly operate the database, it uses an interface called a database executor (Executor) to help it perform operations.

5) Encapsulate SQL objects. In this step, the executor encapsulates the SQL information to be processed into an object (MappedStatement), which includes SQL statements, input parameter mapping information (Java simple types, HashMap or POJO) and output result mapping information.

6) Operate the database. Once you have the executor and SQL information encapsulation object, you can use them to access the database, and finally return the operation result and end the process.

To sum up, MyBatis mainly has two core components: SqlSessionFactory and Mapper. SqlSessionFactory is responsible for creating database sessions, and Mapper mainly provides SQL mapping.

Guess you like

Origin blog.csdn.net/weixin_45627039/article/details/132467111