In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "the use of ResultSetHandler in Mybatis". Many people will encounter such a dilemma in the operation of actual cases, 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!
Mybatis-3.4.6.release.
ResultSetHandler is an interface, such as List-1
List-1
Public interface ResultSetHandler {/ / convert the result to List List handleResultSets (Statement stmt) throws SQLException; / / convert the result to cursor Cursor Cursor handleCursorResultSets (Statement stmt) throws SQLException; void handleOutputParameters (CallableStatement cs) throws SQLException;}
The implementation class is only DefaultResultSetHandler, and the implementation is a bit complicated because there are a lot of situations to consider.
Principle of result encapsulation
List-2
For example, List-2, after defining such a ResultMap, use ObjectFactory to create a Person object
Person.setId (resultSet.getInt ("id"))
Person.setUsername (resultSet.getString ("username"))
Person.setPassword (resultSet.getString ("password"))
Person.setFltNum (resultSet.getString ("flt_num"))
However, the implementation of this transformation process is very complex, in which TypeHandler is used.
Before we start, let's take a look at ResultSetWrapper, as shown in List-3. Get the column attributes through ResultSet, iterate through the column, and get the column name, column type, and corresponding JdbcType.
List-3
Public ResultSetWrapper (ResultSet rs, Configuration configuration) throws SQLException {super (); this.typeHandlerRegistry = configuration.getTypeHandlerRegistry (); this.resultSet = rs; final ResultSetMetaData metaData = rs.getMetaData (); final int columnCount = metaData.getColumnCount (); for (int I = 1; I resultSetCount) {ResultMap resultMap = resultMaps.get (resultSetCount); / 4 handleResultSet (rsw, resultMap, multipleResults, null); / / 5 rsw = getNextResultSet (stmt); cleanUpAfterHandlingResultSet () ResultSetCount++;} String [] resultSets = mappedStatement.getResultSets (); if (resultSets! = null) {while (rsw! = null & & resultSetCount)
< resultSets.length) { ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]); if (parentMapping != null) { String nestedResultMapId = parentMapping.getNestedResultMapId(); ResultMap resultMap = configuration.getResultMap(nestedResultMapId); handleResultSet(rsw, resultMap, null, parentMapping); } rsw = getNextResultSet(stmt); cleanUpAfterHandlingResultSet(); resultSetCount++; } } return collapseSingleResultList(multipleResults);} 1处通过ResultSetMetadata获取列的属性 2处获取我们定义的resultMap,如List-2 3处验证resultMap个数,如果小于1则会报错 4处获取resultMap,从List中 5处是核心,调用handleResultSet->HandleRowValues- > handleRowValuesForSimpleResultMap
List-5
Private void handleRowValuesForSimpleResultMap (ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {DefaultResultContext resultContext = new DefaultResultContext (); skipRows (rsw.getResultSet (), rowBounds); while (shouldProcessMoreRows (resultContext, rowBounds) & & rsw.getResultSet (). Next () {ResultMap discriminatedResultMap = resolveDiscriminatedResultMap (rsw.getResultSet (), resultMap, null); Object rowValue = getRowValue (rsw, discriminatedResultMap); / / 1 storeObject (resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet ()) }}
In the handleRowValuesForSimpleResultMap method, the constant call to the resultSet.next () method fetches all the data in the resultSet.
At 1 of List-5, call the getRowValue method, which takes a row of data in resultSet and encapsulates the data bit object
List-6
Private Object getRowValue (ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {final ResultLoaderMap lazyLoader = new ResultLoaderMap (); Object rowValue = createResultObject (rsw, resultMap, lazyLoader, null); / / 1 if (rowValue! = null & &! hasTypeHandlerForResultObject (rsw, resultMap.getType ()) {final MetaObject metaObject = configuration.newMetaObject (rowValue); boolean foundValues = this.useConstructorMappings; if (shouldApplyAutomaticMappings (resultMap, false)) {foundValues = applyAutomaticMappings (rsw, resultMap, metaObject, null) | foundValues / / 2} foundValues = applyPropertyMappings (rsw, resultMap, metaObject, lazyLoader, null) | | foundValues; foundValues = lazyLoader.size () > 0 | foundValues; rowValue = foundValues | | configuration.isReturnInstanceForEmptyRow ()? RowValue: null;} return rowValue;}
At 1 in List-6, a Person object (such as List-7) is created by reflection, followed by 2 in List-6
List-7
Select * from person where username=# {username}
List-8
Private boolean applyAutomaticMappings (ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {List autoMapping = createAutomaticMappings (rsw, resultMap, metaObject, columnPrefix); boolean foundValues = false; if (! autoMapping.isEmpty ()) {for (UnMappedColumnAutoMapping mapping: autoMapping) {final Object value = mapping.typeHandler.getResult (rsw.getResultSet (), mapping.column); if (value! = null) {foundValues = true } if (value! = null | | (configuration.isCallSettersOnNulls () & &! mapping.primitive)) {/ / gcode issue # 377, call setter on nulls (value is not 'found') metaObject.setValue (mapping.property, value);}} return foundValues;}
In List-8, the createAutomaticMappings method returns the column properties of the sql query-- obtained from ResultSetWrapper, and then iterates through the columns, calling the typeHandler.getResult method to get the value for each column, and then using metaObject.setValue to set the value internally through reflection. After the execution of the createAutomaticMappings method, a row of data from the resultSet is obtained and encapsulated in the object.
Back in List-5, the getRowValue method returns the value, and the storeObject method puts the value into the List.
The description is more general, it is recommended that individuals read the source code. In addition, lazy loading is implemented in ResultSetHandler, for example.
Reference
Https://github.com/mybatis/mybatis-3/tree/3.4.x
This is the end of the introduction to "the use of ResultSetHandler in Mybatis". 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.