In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The resultMap element is the most important and powerful element in MyBatis. It's the thing that keeps you away from the 90% JDBC code that needs to pull data from the result set, and in some cases allows you to do things that JDBC doesn't support. In fact, writing equivalent code similar to joint mapping of complex statements may span thousands of lines of code.
ResultMap is designed so that simple statements do not require explicit result mapping, and many complex statements do need to describe their relationships.
You have seen an example of a simple mapping statement, but there is no explicit resultMap. For example:
Select id, username, hashedPasswordfrom some_tablewhere id = # {id}
Such a statement simply acts on keys where all columns are automatically mapped to HashMap, as specified by the resultType attribute. This is useful in many cases, but HashMap does not describe a domain model very well. Then your application will use JavaBeans or POJOs as the domain model. MyBatis supports both. Take a look at this JavaBean:
Package com.someapp.model;public class User {private int id; private String username; private String hashedPassword; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getHashedPassword () {return hashedPassword } public void setHashedPassword (String hashedPassword) {this.hashedPassword = hashedPassword;}}
Based on the JavaBean specification, the above class has three properties: id,username and hashedPassword. These match exactly to the column names in the select statement. Such a JavaBean can be mapped to the result set as easily as to HashMap.
Select id, username, hashedPasswordfrom some_tablewhere id = # {id}
Remember that the type alias is your partner. With them, you don't have to enter the full path of the class. For example:
Select id, username, hashedPasswordfrom some_tablewhere id = # {id}
In these cases, MyBatis automatically creates a ResultMap behind the scenes to map the column to the attribute of JavaBean based on the property name. If the column names do not match exactly, you can use the alias of the select clause (a standard SQL feature) on the column name to match the tag. For example:
Selectuser_id as "id", user_name as "userName", hashed_password as "hashedPassword" from some_tablewhere id = # {id}
So how to try an external resultMap:
Statements that reference it just use the resultMap attribute (notice that we removed the resultType attribute). For example:
Select user_id, user_name, hashed_passwordfrom some_tablewhere id = # {id}
ReulstMap is only a collection of mapping relations between fields and attributes, so the real mapping relationship between fields and attributes is described by ResultMapping (org.apache.ibatis.mapping.ResultMapping) attribute mapping.
The ResultMap data structure is as follows:
Package org.apache.ibatis.mapping;import java.util.ArrayList;import java.util.Collections;import java.util.HashSet;import java.util.List;import java.util.Locale;import java.util.Set;import org.apache.ibatis.session.Configuration;/** * result mapping, which holds the mapping relationship between table and object * * / public class ResultMap {/ / corresponding id attribute private String id; / / corresponding type attribute private Class type / / corresponding to all attribute mapping relations except elements private List resultMappings; / / corresponding to all attribute mapping relations with ID flag, including elements and child elements private List idResultMappings; / / corresponding to all attribute mapping relations with Constructor flag, including all child element private List constructorResultMappings / / corresponds to the mapping relationship without Constructor flag in all attribute maps private List propertyResultMappings; / / corresponds to the set private Set mappedColumns; / / discriminator of column attributes in all attribute maps, and corresponds to whether the element private Discriminator discriminator; / / contains a nested result mapping. / / if there is a resultMap attribute in an attribute map and there is no resultSet attribute, it is true private boolean hasNestedResultMaps. / / whether there is a nested query, / / if there is a select attribute in an attribute mapping, true private boolean hasNestedQueries; / / whether to enable automatic mapping private Boolean autoMapping;}
And there is a static construction class inside the ResultMap class, as follows:
/ static inner class, builder mode public static class Builder {private ResultMapping resultMapping = new ResultMapping (); public Builder (Configuration configuration, String property, String column, TypeHandler typeHandler) {this (configuration, property); resultMapping.column = column; resultMapping.typeHandler = typeHandler;} public Builder (Configuration configuration, String property, String column, Class javaType) {this (configuration, property); resultMapping.column = column; resultMapping.javaType = javaType } public Builder (Configuration configuration, String property) {resultMapping.configuration = configuration; resultMapping.property = property; resultMapping.flags = new ArrayList (); resultMapping.composites = new ArrayList (); resultMapping.lazy = configuration.isLazyLoadingEnabled ();} public Builder javaType (Class javaType) {resultMapping.javaType = javaType; return this;} public Builder jdbcType (JdbcType jdbcType) {resultMapping.jdbcType = jdbcType; return this } public Builder nestedResultMapId (String nestedResultMapId) {resultMapping.nestedResultMapId = nestedResultMapId; return this;} public Builder nestedQueryId (String nestedQueryId) {resultMapping.nestedQueryId = nestedQueryId; return this;} public Builder resultSet (String resultSet) {resultMapping.resultSet = resultSet; return this;} public Builder foreignColumn (String foreignColumn) {resultMapping.foreignColumn = foreignColumn; return this } public Builder notNullColumns (Set notNullColumns) {resultMapping.notNullColumns = notNullColumns; return this;} public Builder columnPrefix (String columnPrefix) {resultMapping.columnPrefix = columnPrefix; return this;} public Builder flags (List flags) {resultMapping.flags = flags; return this;} public Builder typeHandler (TypeHandler typeHandler) {resultMapping.typeHandler = typeHandler; return this } public Builder composites (List composites) {resultMapping.composites = composites; return this;} public Builder lazy (boolean lazy) {resultMapping.lazy = lazy; return this;} public ResultMapping build () {/ / lock down collections resultMapping.flags = Collections.unmodifiableList (resultMapping.flags); resultMapping.composites = Collections.unmodifiableList (resultMapping.composites); resolveTypeHandler (); validate (); return resultMapping } / / some verification logic to verify that result map has written private void validate () {/ / Issue # 697: cannot define both nestedQueryId and nestedResultMapId if (resultMapping.nestedQueryId! = null & & resultMapping.nestedResultMapId! = null) {throw new IllegalStateException ("Cannot define both nestedQueryId and nestedResultMapId in property" + resultMapping.property) } / / Issue # 5: there should be no mappings without typehandler if (resultMapping.nestedQueryId = = null & & resultMapping.nestedResultMapId = = null & & resultMapping.typeHandler = = null) {throw new IllegalStateException ("No typehandler found for property" + resultMapping.property) } / / Issue # 4 and GH # 39: column is optional only in nested resultmaps but not in the rest if (resultMapping.nestedResultMapId = = null & & resultMapping.column = = null & & resultMapping.composites.isEmpty ()) {throw new IllegalStateException ("Mapping is missing column attribute for property" + resultMapping.property);} if (resultMapping.getResultSet ()! = null) {int numColums = 0 If (resultMapping.column! = null) {numColums = resultMapping.column.split (",") .length;} int numForeignColumns = 0; if (resultMapping.foreignColumn! = null) {numForeignColumns = resultMapping.foreignColumn.split (",") .length;} if (numColums! = numForeignColumns) {throw new IllegalStateException ("There should be the same number of columns and foreignColumns in property" + resultMapping.property) }} private void resolveTypeHandler () {if (resultMapping.typeHandler = = null & & resultMapping.javaType! = null) {Configuration configuration = resultMapping.configuration; TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry (); resultMapping.typeHandler = typeHandlerRegistry.getTypeHandler (resultMapping.javaType, resultMapping.jdbcType);}} public Builder column (String column) {resultMapping.column = column; return this;}}
The ResultMapping data structure is as follows:
Package org.apache.ibatis.mapping;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Set;import org.apache.ibatis.session.Configuration;import org.apache.ibatis.type.JdbcType;import org.apache.ibatis.type.TypeHandler;import org.apache.ibatis.type.TypeHandlerRegistry / * Field Mapping A ResultMapping instance corresponds to a mapping relationship between a field in ResultSet and an attribute in javaBean * * in the sql element, all elements except child elements generate instances of this type, * including: * contains Builder classes internally, which are responsible for data integration and verification Not responsible for parsing * @ author Administrator * * / public class ResultMapping {/ / Core configuration object private Configuration configuration for incoming parameters / / attribute name, corresponding element property attribute private String property; / / field name, corresponding element column attribute private String column; / / attribute java type, corresponding element javaType attribute private Class javaType; / / field jdbc type, corresponding element jdbcType attribute private JdbcType jdbcType; / / type handler, corresponding element typeHandler attribute private TypeHandler typeHandler / / the result of the resultMap attribute of the corresponding element after the namespace is applied private String nestedResultMapId; / / the select attribute of the corresponding element applies the namespace, the result private String nestedQueryId; / / the result of the split of the notNullColumn attribute of the corresponding element private Set notNullColumns; / / the columnPrefix attribute of the corresponding element private String columnPrefix; / / the processed flag, there are two flags: id and constructor private List flags / / the result generated after the column attribute of the corresponding element is split. When composites.size () > 0, column is null private List composites; / / resultSet attribute of the corresponding element private String resultSet; / / whether the foreignColumn attribute private String foreignColumn; / / of the corresponding element is delayed loading, the value of the fetchType attribute of the corresponding element, lazy is true, otherwise false / / also reads private boolean lazy; directly from the configuration object}
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.