java architecture Road - (source) mybatis execution flow source parsing

  The source Mybatis us to say, just say here that the process execution, too many internal details, there can only teach him to fish up. Or the most recent part of the code, let's recap.

package mybatis;

import mybatis.bean.StudentBean;
import mybatis.dao.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class Test1 {

    public SqlSession session;
    public SqlSessionFactory sqlSessionFactory;

    @Before
    public void init() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        session = sqlSessionFactory.openSession();
    }

    @Test
    public void studentTest() {
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        StudentBean result = mapper.selectUser(1);
        System.out.println(result);
        session.commit();
    }

}

It is to get stream file, but also our main configuration file, streaming file parsing, passed into the build, to construct a sqlSessionFactory, then by sqlSessionFactory get session, get mapper, execute sql, complete.

Simple process should be like, let's make a little bit more professional diagram.

 Into our code is probably three steps to build the Configuration object, build our sqlsessionfactory, get our session, to give the corresponding statement (processing parameters and results) to obtain the result set.

So we start with a step by step look at us, look at our Configura tion objects

Configuration Analysis:

  We open the Configuration class can be seen, many of which property settings, including cache, mapper, plug-ins and so on, in fact, our xml tags into objects, and should be noted here is that this process is to resolve all the the xml Configuration object are converted, including config.xml and mapper.xml. Source too much, I will not paste. I'll give you a look at my conversion completed.

 There is nothing mysterious, is a process xml parsing, parse method XMLConfigBuilder in class, generate a Configuration object to complete, are interested can make a break a look.

 

 Here we should note that there is a lot of elements corresponding to the plurality of map and many properties are set. Incidentally close look which is constructed by a plurality of constructors.

 MapperAnnotationBuilder to comment constructor method, the rest are for the Configuration Service. That Configuration After creating a lot of things have been identified (except cache). So how is it generated sqlsession. Back to the Configuration object we have created a source phrase up.

Click the build methods into account, we see a new DefaultSqlSessionFactory then pass into our Configuration object, that is to say with the Configuration object will generate our DefaultSqlSessionFactory object. DefaultSqlSessionFactory achieved our SqlSessionFactory, we will get a SqlSessionFactory. Next up is our sqlsession

sessionsql Analysis:

  Or the old rules, the previous diagram Look at the source code, is better understood.

 

 

 Implementation process something like this.

 That is, what does that mean? DefaultSqlSessionFactory generated by an actuator Executor, the following is supported by the BaseExecutor, which contains some of our configuration and cache (is not more in-depth know why a cache short life), but also a similar decorator CachingExecutor he also needs BaseExecutor below to support, priority check cache you need to query, the query can not return to our BaseExecutor to perform. Go hug a source, sqlSessionFactory.openSession () method also got our sqlsession, we have to look into what is written.

Inside the 95-line tx mainly to get from some of our configuration to configure data sources, which we figure to draw support BaseExecutor configuration, came to 96 lines, create Executor, incoming data source configuration and implementation of a type, here simply mention a mouth, there are three main types: sIMPLE (simple), rEUSE (reusable), bATCH (batch conduct), we generally use the sIMPLE. We then into the creation of Executor way we look, what they have done.

 First determine the type actuators, simple, repeatable, batch, 613 cache line is our actuators, and out of that judgment is whether you configure the secondary cache, so our cache is configured actuators, bottom or Executor, can click to check it in.

That we generate a configuration file config.xml, mapper.xml our Configuration object, Configuration placed in DefaultSqlSessionFactory, generate our Executor executor, ready to execute SQL. Back to the topic we continue to look at part of the last MappedStatement

MappedStatement Analysis:

  Remember two issues, CRUD, it can be classified as two types, one is the original data changes - additions and deletions, and the other is the original data has not changed - the query. Our mybatis the same way to deal with, mainly two kinds of categories of query and update methods.

  MappedStatement by former calling Executor executor, by the configuration object to build. Source in the select *** method DefaultSqlSession, where not explained in detail. Interested can go to find out. Our main process is executed.

  Generally execution is 1. get sql;. 2 splicing parameters;. 3 execute sql;. 4 encapsulation result set. We look at the specific source processes.

  We learned above, the real implementation of the actuator is performed in BaseExecutor where we select the method DefaultSqlSession call, last performed executor.query, just go our priority CachingExecutor actuator, if not only to our BaseExecutor actuators inside, our sql query, do not change the data, then we hit the breakpoint query method our BaseExecutor class.

147 line is to clean up our cache, if mapper.xml configured to clean up the tab, there will first clean up the cache. queryStack indicates whether the actuator is being used.

152 lines begin our query cache, if the air conditioning is 156 lines, we began a formal inquiry, which is accounted for position into the cache, and then execute the query, clear the cache of cache footprint repositioning, said there is a level cache.

 

It boils down to this:

1. config.xml file stream and get mapper.xml;

2. Create with our two or more stream files a Configuration object (single responsibility principle to build, many a constructor to build, for example XMLConfigBuilder)

3. Configuration object class SqlSessionFactoryBuilder superimpose build method, construct a SqlSessionFactory.

4.sqlSessionFactory.openSession () to get our session object.

The session object and the Configuration object encapsulates parameters and results set mapping, to generate a corresponding actuator, the actuator and the secondary cache BaseExecutor actuators.

6 by the secondary cache to the actuator CachingExecutor two cache has priority search, query or update method performed BaseExecutor actuator does not exist.

7. get result set conversion ResultMap, session close, the write cache, return ends.

 

Guess you like

Origin www.cnblogs.com/cxiaocai/p/11534137.html