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

How to understand the Mybatis source code

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to understand the Mybatis source code". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Why bother?

Because during the interview, the interviewer likes to ask: what framework source code have you seen? JDK source code is also fine.

At this time, if the answer has not been read, although you are not asked to go back immediately and wait for notice. But maybe your impression on the interviewer will be greatly reduced.

If you have read the answer and can speak it fluently, the interviewer may say in his heart: "the young man can do it!" (PS: many interviewers have not seen it themselves, or have forgotten many of them).

In short, if you can say 123, it will give the interviewer the impression of leverage. Why worry about work? Why worry about a raise?

Why bother?

In the case of most people: the source code is not unseen, but only the next part at a time. Why can you only watch the next part? There are usually three reasons:

Lack of technical support. Look at the source code needs technical support, not any rookie can also understand. Without some technical support, you can look at it for a short period at most, and then you can't watch it, so you give up.

Lack of the right state of mind. Look at the source code is really boring, if we do not put a good state of mind, do it as a task, must feel bored, and it is easy to give up.

Lack of enough time. Really busy, often read a part of it, work overtime every day, no time to read, and then forget the source code that I was looking at before.

Personal suggestion

For the average java programmer, what skills do you need before reading the source code?

Personally, I suggest that some of the following basic knowledge must be learned:

Can design patterns: including singleton pattern, factory pattern, agent pattern, decorator pattern, responsibility chain pattern, template method pattern, etc.

Will guess: bold guess, when looking at the source code, stand at a high level to think, if you are the designer of Mybatis, how will you design?

Will pick the key point to see: don't be stuck there by unwritten parameter check, type check and other problems.

Can draw pictures: flow charts, class diagrams, etc., find some online drawing tools, the media will save it after painting, if you do not look at it for a period of time, and then look back at these drawings, you can quickly continue.

Mybatis source code analysis

Today, let's take a look at the reading of the Mybatis source code. Let's give an example:

Cases and questions

Starting with our original demo:

Public static void main (String [] args) {String resource = "mybatis-config.xml"; InputStream inputStream = null; SqlSession sqlSession = null; try {inputStream = Resources.getResourceAsStream (resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (inputStream); sqlSession = sqlSessionFactory.openSession (); User user = sqlSession.selectOne ("com.tian.mybatis.mapper.UserMapper.selectById", 1) System.out.println (user);} catch (Exception e) {e.printStackTrace ();} finally {try {inputStream.close ();} catch (IOException e) {e.printStackTrace ();} sqlSession.close ();}}

Let's not discuss this when it comes to obtaining the data stream inputStream, we will focus on it.

For the above demo, we can divide it into five steps:

The first step is to create a factory class sqlSessionFactory.

The parsing of the configuration file is done here. Includes mybatis-config.xml and our Mapper.xml mapper files. What we care about in this step is: what do you do when parsing? What objects are produced and where the parsing results are put. Because this will mean where to get the content of this configuration item when we use it later.

The second step is to create a SqlSession through SqlSessionFactory.

So the problem is, SqlSession defines a variety of add, delete, modify and query API, which is called for the client, what implementation class is returned? Apart from SqlSession, what other objects and environments have we created?

Third, get a Mapper object.

The problem is that UserMapper.java is an interface and no implementation class is created for it, so how is it instantiated? What kind of object is this Mapper object we are using? Why get it from SqlSession? Why send it into an interface and then use an interface to receive it?

Step 4, call the interface method.

The question is that our interface does not have an implementation class, so why can we call its methods directly? So whose method is it calling? How do you relate SQL? How did you get the data?

Step 5, close the relevant resources.

Start the source code analysis process

Since there is a lot of content involved, here are a few pictures to show the whole process. We can quickly flip through the source code of Mybatis through these pictures.

The first step

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (inputStream)

The whole process of this code is as follows:

According to inputStream, parse the configuration file and create a default SqlSessionFactory implementation class for DefaultSqlSessionFactory. Build a factory class that is specifically used to create SqlSession objects.

Step two

SqlSession sqlSession = sqlSessionFactory.openSession ()

The whole process of obtaining SqlSession is as follows:

Step three

User user = sqlSession.selectOne ("com.tian.mybatis.mapper.UserMapper.selectById", 1)

The third and fourth steps are drawn by this line of code.

This step is to return a mapper proxy class, which is specifically used to bind the UserMapper interface and the UserMapper.xml proxy class. The created proxy class can be instantiated, and then the side of the UserMapper interface can be called.

Step 4: call the proxy object to execute the whole process of SQL.

Step five, close the resource.

"how to understand the Mybatis source code" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 278

*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

Database

Wechat

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

12
Report