MyBatis Source parsing "6" sqlsession run

Source: Internet
Author: User
Tags throw exception

Objective

This classification is more continuous, if you do not understand, or the first time to see, please review the previous blog

Http://www.cnblogs.com/linkstar/category/1027239.html

After studying, we know how the factory was built and how the products were produced.

So today it's important to get into the focus. That's how we actually use the product.

That's how sqlseesion actually works and what's inside.

This part is very difficult, need to use to our previous basic equipment OH.

Approximate steps for product operation

We still have to look at how it works from the outside.

sqlsession session = Sqlsessionfactory.opensession (); Demo demo = (demo) session.selectone ("Com.xex.dao.mapper.DemoMapper.selectDemo"); session.close (); from the outside, the simplest case is this, and it looks relatively simple. 1, obtain products from the factory (get the product has been mentioned in the last section, there is not much to say)2, the implementation of the required SQL method (Focus)3, close the product (recycling products)

Internal processes for product operation

from the general steps we can see that the focus is on the second step. How can you execute SQL by invoking a method arbitrarily? The process is complicated, I try to speak carefully. Let's take this query as an example. First we enter into the SelectOne method inside to see. (Idea uses the shortcut key ctrl+alt+b to see the implementation of the method on the cursor)

What we need to look at is the implementation of defaultsqlsession, as to why we've talked about it before.

Notice a place where we pass in the parameter statement= "Com.xex.dao.mapper.DemoMapper.selectDemo"

In the general direction, the passed parameter is "Com.xex.dao.mapper.DemoMapper.selectDemo" and the returned parameter is a class mappedstatement

Mappedstatements Brief Introduction

Here is the text to describe the construction of this mappedstatements idea. Because the specific details too much, do not explain, the focus on the part, I hope you can keep up and follow the idea of the source code to find.

But some of the details in this class are worth learning and studying, so it's worth seeing.

1. First of all, this mappedstatements is a variable in the configuration class we are familiar with.

2. This is new: New Strictmap

3, Strictmap is an internal class in the configuration inherits from the HashMap, and it is different yes, this map has a name of the variable, and then the main difference is the map of the put and get method.

4, put the method:

Here are a few words are used to describe the put method, after reading the careful thinking, how the three is summed up.

First, this map will com.xex.dao.mapper.DemoMapper.selectDemo this package name according to "." Remove the last element after splitting, this is Selectdemo. (Just for example)

Second, the value of this map is a string, if the put is found to have the same key, then do not immediately throw an exception, but the duplicate key is overwritten, the value of the duplicate key is modified to the ambiguity type.

Third, ambiguity this class is actually a string.

First of all, because the final selectdemo after splitting is the id,mybatis of the SQL statements we wrote, this ID is the only way to find the database statement that needs to be run. And the storage of the time only need the last Selectdemo on it.

Then it's strange why MyBatis encounters the same key, which means that there are two SQL statements with the same ID when there is no direct option to throw an exception when loading the configuration, but to store a strange ambiguity type? (ambiguity means vague, ambiguous)

Let's see how the Get method should be understood.

One, call the parent class get is also HashMap get method.

Two, the empty value throws the anomaly

Three, ambiguity throw exception

Smart you should have understood.

For you to explain to the stupid, empty value throw exception is through this ID can not find the corresponding need to execute SQL statement is of course abnormal; When ambiguity throws an exception on the surface there are two SQL statements with the same ID, MyBatis does not know which one to execute, so it is unusual.

What's left is when this map was created? Of course, the beginning of the construction of the factory, I have said before, do not understand can be turned over the front, review a bit.

Specifically in the mappedstatement inside how SQL is stored, interested can directly into this class to see. This is where we are going to continue the main process.

Execution of SQL

Let's go ahead and see what happens after we get the SQL.

Return Executor.query (MS, wrapcollection (parameter), rowbounds, Executor.no_result_handler);

Who's going to do it first? A thing called executor.

We call it the performer. It is an interface. It is then assigned when the product is produced.

Then there is the query method that it executes, and the return value is a list of generic constraints.

Then let's look at the parameters:

1, MS is the execution statement we just got.

2, Wrapcollection is the current parameter, that is, the database query when the parameters, such as query conditions, if the query condition is a set (Collection) or array form of the conversion to map, if not directly returned.

We're not in the argument now, so we're going to return a null.

3, Rowbounds MyBatis paging function, we don't care about his

4, Executor.no_result_handler

is a null, called a processor with no results

And then we'll take a look at execution

Here is the key!!! Watch it.

We don't look at it from the cache, it's simple, the cache is actually a map.

We're just looking. Query from Database

Down this way.

See here Excute No, here is the real execution of SQL, inside it is the legendary JDBC, like you go inside to see, do not like us to continue to see how the final result set is mapped.

Handling of returned results

At this point the entire SQL execution process in MyBatis is finally complete.

The whole route, if you look really long, has to admire the designer for such meticulous design.

Summarize

As we walk through the entire route, we have identified all the processes that SQL performs in MyBatis.

I believe you have something to gain, in fact, we should think more about why designers such design, some code why this writing. What are the benefits and design ideas of this writing? What kind of optimization or merit is worth studying.

But don't be happy too early.

It's not over yet!

Our equipment is useless now! The next article explains why it is necessary to define an interface that works in MyBatis and does not need to implement classes.

The application of proxy mode and the powerful reflection.

Hope that the above ideas for you to see the source of help. Please be grateful if you have any questions.

Reprint Please specify source: http://www.cnblogs.com/linkstar/category/1027239.html

Linkinstar

MyBatis Source parsing "6" sqlsession run

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.