Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Source Code Analysis of Mybatis execution process

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the source code analysis of the Mybatis implementation process, many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

One: Mybatis source code analysis process public static void main (String [] args) {try {/ / basic mybatis environment / / 1. Define the mybatis_config file address String resources = "mybatis_config.xml"; / / 2. Get InputStreamReaderIo stream Reader reader = Resources.getResourceAsReader (resources); / / 3. Get SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (reader); / / 4. Get Session SqlSession sqlSession = sqlSessionFactory.openSession (); / / 5. Operate Mapper interface UserMapper mapper = sqlSession.getMapper (UserMapper.class); UserEntity user = mapper.getUser (2); System.out.println (user.getName ());} catch (Exception e) {e.printStackTrace ();}} summary:

1. Read Mybatis configuration file information

2. Obtain SqlSessionFactory

a. Use XMLMappperBuilder to parse the Mybatis configuration file, encapsulate it into an Environment object, and then set the Environment object to the Configuration object

b. Call the ConfigurationElement function, and finally execute the addMappedStatement method, encapsulating each SQL statement in the mapper configuration file into a mappedStatement object, which is saved as a value in the HashMap collection

c. Enter the addLoaderResource method and use the HashSet collection to store the path address of the mapper.xml mapping file of mybatis

d. Enter the bindMapperForNamespace () method, find the mapper interface through namespace using the Java reflection mechanism, and then call the addMapper () method to determine whether it is an interface type and whether it has been registered (if registered, an exception is thrown) where mapperRegistry saves the mapper interface through HashMap, [key: interface; value:MapperProxyFactory]

3. Obtain session

a. Enter the openSession () method and execute the newExecutor () method to create the executor

b. First create a SimpleExecutor simple executor, and then determine whether the secondary cache is enabled. If it is enabled by default, the CacheExecutor cache executor will be created.

c. Execute the interceptorChain.pluginAll () method, the responsibility chain design pattern, and the bottom layer uses dynamic proxy technology, so that the developer can customize the plug-in development by implementing the Interceptor interface and specifying the signature of the method you want to intercept, and finally return to the executor.

4. Operate the mapper interface

a. Call the getMapper () method, and finally execute the mapperProxyFactory.newInstance (sqlSession) method to create the proxy class MapperProxy

b. When we call the mapper,getUser () method, we execute the invoke () method of the MapperProxy proxy class

c. To determine whether the mapper interface has an implementation class, obviously we do not have an implementation class, then call the cacheMapperMethod () method to cache the method method to be proxied

d. Enter the cacheMapperMethod () method to find out if there is any in the cache. If not, associate the SQL statement configured in the mapper configuration file with the corresponding mapper interface method and put it in the map cache, then go to the cache directly later, and finally execute the execute () method.

e. Execute the execute () method and finally call the selectOne () method

f. If you enter the selectOne () method, the bottom layer will still query all, but if you take the first one and query more than one, an exception will be thrown.

g. Enter the selectList () method and call the getMapperStatement () method to get the corresponding SQL statement

h. Execute the query () method to query, and judge that if the second-level cache is turned on and the second-level cache storage medium (Redis,EhCache..) is configured, then query data in the second-level cache first, and the first query has no cache data, then refresh the cache configuration and clear the cache.

i. If no data is found in the second-level cache (sessionFactory), go back to execute BaseExecutor to query whether there is cache data in the first-level cache (sqlSession) of HashMap, and the first-level cache (PerpetualCache) is stored in memory, which is also not available. Finally, query the database DB.

j. Cache the data queried from the database into the first level cache, then synchronize the data from the first level cache to the second level cache, and temporarily cache it in the map collection of getTransactionalCache's entritiesToAddOnCommit before adding it to the second level cache.

k. Call the executor.close () method to iterate through the TransactionCache, and finally commit the temporary map cache data to the secondary cache, which will be cleared if the transaction is rolled back.

After reading the above, do you have any further understanding of the source code analysis of the Mybatis execution process? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report