In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you the Mybatis query delay loading example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
Detailed explanation and example of delayed loading of Mybatis query
1.1 enable delayed loading
The delayed loading of Mybatis is for nested queries, which means that when querying, only the outermost SQL is queried first, and the inner SQL will be queried only when needed. The deferred loading of Mybatis is turned off by default, that is, all the nested SQL are checked together and all the information of the object is queried at once. There are two ways to turn on deferred loading.
The first is to specify the value of the fetchType attribute as "lazy" on the corresponding or tag. In the following example, we return BaseResultMap when querying a query whose id is selectByPrimaryKey. In BaseResultMap, we specify that the attribute "nodes" is of a collection type and needs to be queried for the selectNodes query through id. We specify that the fetchType of the query is lazy, that is, delayed loading.
Select from sys_wf_process where id=# {id,jdbcType=INTEGER} select id, process_id, node_code, node_name from sys_wf_node where process_id=# {id}
The second is to turn on global deferred loading. Global deferred loading can be enabled by adding the following configuration under the label of the Mybatis configuration file. When global deferred loading is enabled, we no longer need to configure deferred loading on each nested subquery. If there is a nested subquery that does not need deferred loading, we can set its fetchType= "eager". The fetchType set on the nested query can override the global deferred loading setting.
1.2 Analysis
The query results of Mybatis are handled by the handleResultSets () method of the ResultSetHandler interface. There is only one implementation of the ResultSetHandler interface, DefaultResultSetHandler. Interested friends can take a look at its source code and see how it handles the result set. For the topic of this article, one of the core methods related to delayed loading is the following method to create a return result object.
Private Object createResultObject (ResultSetWrapper rsw, ResultMap resultMap, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {final List > (); final List constructorArgs = new ArrayList (); final Object resultObject = createResultObject (rsw, resultMap, constructorArgTypes, constructorArgs, columnPrefix); if (resultObject! = null & &! typeHandlerRegistry.hasTypeHandler (resultMap.getType () {final List propertyMappings = resultMap.getPropertyResultMappings () For (ResultMapping propertyMapping: propertyMappings) {/ / issue gcode # 109 & & issue # 149if (propertyMapping.getNestedQueryId ()! = null & & propertyMapping.isLazy ()) {return configuration.getProxyFactory (). CreateProxy (resultObject, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);}} return resultObject;}
In the above method, we can see that Mybatis first creates an object corresponding to the return type based on the normal situation. When our ResultMap contains a subquery, it creates a corresponding proxy object based on the type object we normally return. Yes, you read it correctly, that is, our direct result is the proxy object, not the subquery. The corresponding property is the proxy object. The default is a proxy object created based on the JavassistProxyFactory class. It can be changed through the global configuration proxyFactory of Mybatis. The optional values are CGLIB and JAVASSIST, and the default is the latter. Pay attention to the packages that add CGLIB when you need to use the CGLIB agent.
Looking back at our previous deferred loading configuration, one of our queries returns an object of type SysWfProcess, which has a nodes property of type SysWfNode collection, and the nodes property is found through a subquery and is deferred loading. At this time, let's conduct the following tests.
@ Test public void testLazyLoad1 () {SysWfProcessMapper mapper = this.session.getMapper (SysWfProcessMapper.class); SysWfProcess process = mapper.selectByPrimaryKey (1); System.out.println (process.getClass ());}
At this point, you will find that the output of the above test code is a proxy class, not our own com.elim.learn.mybatis.model.SysWfProcess type. In addition, if you enable log output and print DEBUG logs, you will see that Mybatis sends two SQL queries.
2016-12-23 15 select id 43 BaseJdbcLogger.java:145 21131 DEBUG [main] (BaseJdbcLogger.java:145)-= > Preparing: select id, template_id, creator, create_time from sys_wf_process where id =? 2016-12-23 15 15 V 43 BaseJdbcLogger.java:145 21156 DEBUG [main] (BaseJdbcLogger.java:145)-= > Parameters: 1 (Integer) 2016-12-23 1543 Parameters [main] (BaseJdbcLogger.java:145)-Preparing: select id, process_id, node_code Node_name from sys_wf_node where process_id=?2016-12-23 15 node_name from sys_wf_node where process_id=?2016 43 BaseJdbcLogger.java:145 DEBUG [main] (BaseJdbcLogger.java:145)-= > Parameters: 1 (Integer) 2016-12-23 15 15 14 43 node_name from sys_wf_node where process_id=?2016 [main] (BaseJdbcLogger.java:145)-0) {return new JavassistSerialStateHolder (original, lazyLoader.getProperties (), objectFactory, constructorArgTypes, constructorArgs) } else {return original;}} else {if (lazyLoader.size () > 0 & &! FINALIZE_METHOD.equals (methodName)) {if (aggressive | | lazyLoadTriggerMethods.contains (methodName)) {lazyLoader.loadAll ();} else if (PropertyNamer.isProperty (methodName)) {final String property = PropertyNamer.methodToProperty (methodName) If (lazyLoader.hasLoader (property)) {lazyLoader.load (property);}} return methodProxy.invoke (enhanced, args);} catch (Throwable t) {throw ExceptionUtil.unwrapThrowable (t);} these are all the contents of the article "sample Analysis of delayed loading of Mybatis queries". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.