Understand the relationship between sqlsession and sqlsession template in mybatis

Time:2020-8-1

My handsome big gray wolf is back. Today’s message to you is:

Be patient, always young, always in tears.

preface

Let me cry for a while. Wuwuwuwu ~ I wrote half of my article last night. I covered it before I saved it. I pulled off the power to pack the computer into my schoolbag and took it home. When I finished, I was lazy and stopped. Finally, I didn’t take it home. So, I was punished. When I came here this morning, the computer just turned off without powerI can’t find the article I wrote(I’m sorry for the bad Mac. I wrote a special article to praise the delicious Mac

Thread.sleep(5000);//crying…

Back to the subject, began to write with tears.

When learning mabatis or looking at the company’s mybatis project, many beginners always don’t understand the relationship between sqlsessionsql, sqlsessiontemplate, sessionfactory and sqlsessionfactorybean. Especially when we look at other people’s code, different projects are completed by different people with different styles. Some use spring configuration and others use springboot to hard compile Code form, more complex projects, the use of read-write separation and so on, it is easy to confuse people. The purpose of this article is to clarify the relationship between them in an easy to understand way. If you find that some parts of the article are difficult to understand or have errors, I would like to accept them with an open mind and feel grateful. If you find it helpful,Like to supportThank you~

Come on, cheer up.

text

Sqlsession and sqlsessiontemplate

First of all, we will bring you the relationship between sqlsession and sqlsessiontemplate. First, look at the diagram and then the description.

Understand the relationship between sqlsession and sqlsession template in mybatis

Sqlsession implements the closeable interface, which means that sqlsession can be closed. That is to say, sqlsession represents a connection that can be closed. Just like his name, session represents a session, which is used to maintain the state information between stateless requests. Here in the database, sqlsession represents a session between the database client and the database server, and maintains the state information between them.

Understand the relationship between sqlsession and sqlsession template in mybatis

We can see that sqlsession is an interface, in which we are familiar with the methods of select, insert and update to operate the database and execute SQL statements. Is it very familiar.

Understand the relationship between sqlsession and sqlsession template in mybatis

Sqlsession has three implementation classes. Of course, you can implement it yourself. The default sqlsession is its default implementation class, and of course, the familiar sqlsessiontemplate implementation class. If not, I suggest you open the source code and follow my ideas.

Understand the relationship between sqlsession and sqlsession template in mybatis

Understand the relationship between sqlsession and sqlsession template in mybatis

Smart, have you already discovered that there is a big difference between the defaultsqlsession and the sqlsessiontemplate, but the focus of our discussion this time is the sqlsessiontemplate, so we won’t talk about the differences between them. In fact, if you can see the end carefully, you will naturally know the difference.

Understand the relationship between sqlsession and sqlsession template in mybatis

It is worth mentioning that, in addition to the sqlsession interface, sqlsessiontemplate also implements the disposablebean interface. This means that after the instance of sqlsessiontemplate is found by the bean factory, it will be included in the whole spring During the management process of bean life cycle, when beanfactory tries to destroy it, the bean manager will call the destroy() method of sqlsessiontemplate by callback.

Understand the relationship between sqlsession and sqlsession template in mybatis

The default implementation is an empty method. You can rewrite it yourself.

To sum up, sqlsessiontemplate is the implementation class of sqlsession. Its name is sqlsession template. With sqlsessiontemplate, we can execute SQL statements of Dao layer. Having said so much, the key point is that sqlsessiontmplate is the implementation class of sqlsession, and one of the key classes in this implementation class is sqlsessionfactory.

Sqlsessionfactory and sqlsessionfactorybean

Understand the relationship between sqlsession and sqlsession template in mybatis

Sqlsessionfactory is also an interface. It is a sqlsession factory. Its ability is to open a sqlsession session and overload many different parameters. You can change these parameters to customize some default behaviors during the session. For example, you can set the auto commit transaction or turn off the auto commit; you can set the type of thread that gets the database connection (reuse, each new generation, etc.); you can also get the configuration object instance of the whole mybatis configuration information.

Understand the relationship between sqlsession and sqlsession template in mybatis

Sqlsessionfactory also has two implementation classes by default. Of course, you can also customize the implementation classes. The default implementation is defaultsqlsessionfactory.

In short, sqlsessionfactory is the factory that produces sqlsession objects. That is to say, in the whole mybatis, if there is only one database server to connect to, then only one factory is needed (there is only one instance object of sqlsessionfactory), and sqlsession can be closed freely, which means that sqlsession needs to be created repeatedly. As mentioned above, sqlsession is associated with a specific database connection. However, if you directly operate the physical connection every time you create and destroy it, this resource waste is very high and the efficiency is very low. See the method of defaultsqlsessionfactory:

Understand the relationship between sqlsession and sqlsession template in mybatis

The above figure is based on the database connection pool. That is to say, when a connection is exhausted and the sqlsession instance is closed, the database connection object is only put back into the object pool without direct destruction. The use of pool technology greatly improves the utilization of material resources, shortens the connection time, and reduces the resource utilization.

At this point, careful partners may have a question: how does the sqlsessionfactory create a sqlsession, or more specifically, how to create a sqlsessiontemplate? This has to be said about dynamic proxy. This part is implemented in sqlsessiontemplate. I will share the details with you from the perspective of source code in the next issue.

There’s not much left, so stick to it and read it.

The following is the old rule of sqlsessionfactorybean. The class diagram is as follows:

Understand the relationship between sqlsession and sqlsession template in mybatis

The application listener interface is implemented, which represents that sqlsessionfactorybean has the ability to monitor some event notifications sent by application.

The factorybean interface is implemented. The instance representing sqlsessionfactorybean is no longer an ordinary bean object, but a factory that can generate its own bean. The generated bean will be included in the spring life cycle. The bean generated here refers to the sqlsessionfactory.

The initializingbean interface is implemented, which represents that the afterpropertieset() method in the sqlsessionfactorybean will be called immediately after the bean initialization property is completed.

As its name suggests, the sqlsessionfactorybean is the factory bean that produces the sqlsessionfactory.

in summary

Sqlsessionfactorybean is a factory bean that produces sqlsessionfactory.

Sqlsessionfactory is a factory to open a sqlsession. It is an interface that can be implemented according to the requirements. Its default implementation class, defaultsqlsessionfactory, uses the database connection pool technology.

Sqlsession is the session information between the client and the database server. There are many methods to operate the database.

Sqlsessiontemplate is a concrete implementation of sqlsession.

Write it at the end

I don’t know if my friends have figured out the relationship between them. In fact, the above-mentioned is relatively simple, mainly to sort out the relationship between them from the structure, and the role of each of them, but this is far from enough. As mentioned above, what is the entanglement between sqlsessiontemplate, sqlsession and sqlsessionfactory? How do datasource and connection work? And how does our code generate SQL statements and send them to the database server?

If you think I write well, clear and understandable, you can pay attention to my official account “grey wolf learning java”, and share the original technical articles regularly, and encourage him.

Recommended Today

ASP.NET Example of core MVC getting the parameters of the request

preface An HTTP request is a standard IO operation. The request is I, which is the input; the responsive o is the output. Any web development framework is actually doing these two things Accept the request and parse to get the parameters Render according to the parameters and output the response content So let’s learn […]