In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "detailed introduction of Java persistence layer framework Mybatis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. the architecture design of Mybaits
II. The implementation process of Mybatis-Spring
1. Initialize SqlSessionFactoryBean
SqlSessionFactoryBean implements the FactoryBean and InitializingBean interfaces of Spring. Spring will create a SqlSessionFactory object for you when the application starts.
Load all MyBatis mapper XML files in the mappers package and its subpackages from the classpath.
two。 Get MapperProxy to act as a proxy to execute Mapper interface.
Proxy Mapper interface (top priority, Mybatis's dynamic proxy MapperPoxy)
1.mapperRegistry.getMapper (type, sqlSession)
2.MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) this.knownMappers.get (type)
3.mapperProxyFactory.newInstance (sqlSession)
4. Agent executes query
3. Call SqlsessionApi for database operation
4.SqlSessionTemplate initializes the sqlSessionProxy proxy class for open session
5.openSession
6.openSession re-execution: DefaultSqlSession calls selectList
7. First query from the cache: first query the secondary cache here, and then check the cache of session
The CachingExecutor cache executor first queries the cache, and then uses delegate to execute SIMPLEExecutor to query the database.
8. If there is no cache, query the database: queryFromDatabase
9. Add plug-ins to RoutingStatementHandler
1.newStatementHandler
2.SatementHandler statementHandler = new RoutingStatementHandler ()
3.this.interceptorChain.pluginAll (statementHandler)
10. Finally, the real call JDBC-- > PreparedStatement.execute () for database operation
11. Query result processing: resultSetHandler:-- > DefaultResultSetHandler
twelve。 Finally sqlSession.commit (true) and close Sqlsession
III. The core principle of Mybatis needs to focus on debug
Sql parsing process
Mybaitis's sql parsing work is initialized by XMLMapperBuilder. When Mybaiti initializes Config, it processes all Mapper files and finally gets SqlSource, which will be put into Configuration. With SqlSource, you can get BoundSql, and BoundSql can get the final sql. The process of parsing is quite tedious, so you can debug it.
Recommend a blog post, Mybatis-Sql dynamic parsing principle:
Http://www.cnblogs.com/fangjian0423/p/mybaits-dynamic-sql-analysis.html
Transaction Management of MyBatis
First, using the transaction management mechanism of JDBC: that is, using the java.sql.Connection object to complete the transaction commit (commit ()), rollback (rollback ()), close (close ()) and so on.
Second, use MANAGED's transaction management mechanism: this mechanism MyBatis itself does not implement transaction management, but allows the program's container, such as (JBOSS,Weblogic), to manage transactions.
The transaction section has not been carefully debug. I would like to recommend you to take a look at the blog post:
Https://blog.csdn.net/luanlouis/article/details/37992171
Plug-in operation mechanism
By default, the method calls that MyBatis allows you to intercept with plug-ins include:
1. Method to intercept actuators: Executor
two。 Processing of intercept parameters: ParameterHandler
3. Handling of intercepted result sets: ResultSetHandler
4. Interception of Sql syntax build processing: StatementHandler
Mybatis instantiates the class represented by the interceptor attribute in the plugin node by reflection. Then call the addInterceptor method of the global configuration class Configuration. Plug-in source code needs to be parsed separately, and you can follow it yourself.
Caching of Mybatis
Level 1 caching: data caching at the session (Session) level. For the waste of resources caused by the same query in a short time, MyBatis will establish a simple cache in the SqlSession object to cache the results of each query. When the next query is made, if it is judged that there is an exact same query before, the result will be directly extracted from the cache and returned to the user. There is no need for another database query. First-level caching is on by default.
Secondary caching: Mybatis is turned off for secondary caching by default. You need to configure it yourself, and then you will get the configuration with cache enabled during initialization to deal with such element nodes. Later, we will summarize the implementation principle of cache in detail, although the second-level cache is not used much.
CacheElement (context.evalNode ("cache")); "A detailed introduction to the Java persistence layer framework Mybatis" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.