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
- TypeAliasesPackage
- TypeAliases
- Plugins
- TypeHandlersPackage
- 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.