Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Mybatis source code

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the Mybatis source code". In the daily operation, I believe that many people have doubts about what is the Mybatis source code. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what is the Mybatis source code?" Next, please follow the editor to study!

Overall architecture

This is just a logical partition architecture for MySql.

Interface layer: the SqlSession class provides access to the database, hiding the subsequent complex processing logic.

Core processing layer: mainly responsible for executing SQL and returning results.

Basic support layer: encapsulates some basic functions to provide services for the core processing layer.

Code structure

Mybatis's code structure is very neat, can be called the perfect java programming specification textbook, when we delve into the source code, we will find that the amount of Mybatis comments is very small, that is because basically we can understand the meaning of it by name.

Design patterns in Mybatis

If you want to learn the application of design patterns in your code, it is also a good choice to read the Mybatis source code, such as:

SqlSession uses facade mode

The log module uses the adapter mode

The data source module uses factory mode

Data connection pool usage policy mode

The cache module uses decorator mode

The Executor module uses the template method mode

The Builder module uses the builder mode

The Mapper interface uses proxy mode

The plug-in module uses the responsibility chain mode

Mybatis Quick start public class MybatisTest extends BaseTest {private SqlSessionFactory sqlSessionFactory; @ Before public void init () throws IOException {String resource = "config/mybatis-config.xml"; try (InputStream inputStream = Resources.getResourceAsStream (resource)) {/ / 1. Read the mybatis configuration file to create SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream);} @ Test / / Test automatic mapping and underscore automatic conversion hump public void quickStart () throws Exception {/ / 2. Get sqlSession try (SqlSession sqlSession = sqlSessionFactory.openSession ()) {initH2dbMybatis (sqlSession); / / 3. Get the corresponding mapper PersonMapper mapper = sqlSession.getMapper (PersonMapper.class); / / 4. Execute the query statement and return the result Person person = mapper.selectByPrimaryKey (1L); System.out.println (person.toString ());} Mybatis execution process

New SqlSessionFactoryBuilder () .build (inputStream);: read the mybatis configuration file to build SqlSessionFactory.

SqlSessionFactory.openSession ();: get sqlSession resources

SqlSession.getMapper (PersonMapper.class);: get the corresponding mapper

Mapper.selectByPrimaryKey (1L);: execute the query and return the result

Close the resource

The above figure shows the execution process of Mybatis, from which we can see that there are four core classes of Mybatis, namely SqlSessionFactoryBuilder, SqlSessionFactory, SqlSession, and SQL Mapper.

SqlSessionFactoryBuilde: read configuration information (XML file), create SqlSessionFactory, builder mode, method-level lifecycle

SqlSessionFactory: create Sqlsession, factory singleton pattern, that exists throughout the application lifecycle of the program

SqlSession: represents a database connection, which can be executed by sending SQL directly or accessing the database by calling Mapper. Thread is not safe, so it is necessary to ensure thread exclusive and method-level life cycle.

SQL Mapper: consists of a Java interface and a XML file that contains the SQL statements to be executed and the result set mapping rules. Method level lifecycle

Three stages of Mybatis core process

As can be seen from the above execution process, the core process of Mybatis is mainly divided into the following three phases:

Initialization phase: read the configuration information in the XML configuration file and comments, create configuration objects, and complete the initialization of each module

Agent phase: encapsulates the programming model of iBatis and initializes the development using the mapper interface

Data reading and writing phase: the parsing of SQL, the mapping of parameters, the execution of SQL, and the parsing of results are completed through SqlSession.

Sample source code

Https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases

Spring-boot-student-mybatis project

Mybatis Source Code Chinese Notes

Https://github.com/xiaolyuh/mybatis

At this point, the study on "what is the Mybatis source code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report