Mybatis XML parsing source code analysis, mybatisxml source code

Source: Internet
Author: User

Mybatis XML parsing source code analysis, mybatisxml source code

I always want to know How mybatis parses the xml file. Today I carefully read the source code and record it here.

Here we use SqlSessionFactoryBean of mybatis-spring as the entry point. mybatis-spring is actually very simple, and the source code is just a few points to understand, proxy is nothing.

1.AnalysisSpringConfiguration

However, many parameters are processed in spring. Therefore, the configuration file is loaded in mybatis-spring instead of parse.

Is

  1. TypeAliasesPackage
  2. TypeAliases
  3. Plugins
  4. TypeHandlersPackage
  5. TypeHandlers

TypeAliases

It is equivalent to loading typeAliases, plugins, typeHandlers

Type conversion is then called when typeAliases is put to TYPE_ALIASES for query.

Plugins

The Interceptor is also private final List <Interceptor> interceptors = new ArrayList <Interceptor> ();

The ArrayList used here is executed from top to bottom in order.

TypeHandlers

Typehandler is also put to private final Map <Class <?>, Map <JdbcType, TypeHandler> TYPE_HANDLER_MAP = new

HashMap <Class <?>, Map <JdbcType, TypeHandler> ();

 

2.Parse our own configurationConfigLocation. Xml

 

The content to be parsed is similar to the content mentioned above.

3.Generate Environment Variables,Seemingly ineffective-Because both areSpringTakeover

4.AnalysisSQLFile!Important

Sequential namespace, cache, parameterMap seems useless,ResultMapKey Points,Reuse SQL and execute SQL

The namespace will not be mentioned.

4.1Cache Creation

The cache is based on the namespace. Each namespace has a new cache.

In the cache, mybatis is also relatively simple to implement a PerpetualCache with map first, and then use the decorator mode to get a bunch of caches, including LRU, Fifo, Scheduled, and other caches with additional functions, in fact, the call is still PerpetualCache. The default value is LRU. Let's talk about the implementation of LRU and Its simplicity. It uses the LinkedHashMap to access the sorting function, which is easy to implement. Then the subsequent SQL statements will be called, the cacheKey generation is shown here.

Many conditions are used to ensure the uniqueness of the key.

4.2 resultMap

4.2.1Parse simple parameters

 

4.2.1ResultMapping Actually, we wroteResultOfJavaObject

Parse xml and configure the value to the object.

4.2.4These parameters andListCombined intoResultMap

4.3DuplicateSQL

This simple direct put

4.4 run SQL

First, set a bunch of properties.

Dynamic xml parsing through a series of hanlder

Then assembled

This object is important and basically contains all the information about an SQL statement. Then, you can use this information to reflect the set object.

 

Mybatis Initialization is complete.

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.