Mybatis ResultMap復合映射使用以及源碼分析
阿新 • • 發佈:2018-05-07
thumbnail session bounds IE array pen lec _id hone 我們知道在mybatis中可以針對一列值作為入參進行嵌套查詢,那麽如果入參為多個時該如何處理呢? mybatis支持復合映射,下面通過示例代碼看看復合映射的使用
<resultMap id="postLiteMap2NestedWithSelect" type="org.apache.ibatis.domain.blog.BlogLite"> <id column="blog_id" property="id" /> <collection property="posts" ofType="org.apache.ibatis.domain.blog.PostLite"> <constructor> <arg javaType="org.apache.ibatis.domain.blog.PostLiteId" column="{id=id}" select="selectPostLiteId" /> <arg javaType="_int" column="blog_id"/> </constructor> </collection> </resultMap> <mapper namespace="org.apache.ibatis.domain.blog.mappers.PostMapper"> <resultMap id="postLiteIdMap" type="org.apache.ibatis.domain.blog.PostLiteId"> <constructor> <idArg javaType="_int" column="id"/> </constructor> </resultMap> <select id="selectPostLite2NestedWithSelect" resultMap="postLiteMap2NestedWithSelect"> select id, 1 as blog_id from post where blog_id is not null </select> <select id="selectPostLiteId" resultMap="postLiteIdMap"> select ${id} as id from (values(0)) as t </select>
查詢
List<BlogLite> posts = session.selectList("org.apache.ibatis.domain.blog.mappers.PostMapper.selectPostLite2NestedWithSelect");
這是怎樣的一個處理過程呢?下面看看時序圖 下面通過代碼看看Mybatis是如何處理的
public ResultMapping buildResultMapping( Class<?> resultType, String property, String column, Class<?> javaType, JdbcType jdbcType, String nestedSelect, String nestedResultMap, String notNullColumn, String columnPrefix, Class<? extends TypeHandler<?>> typeHandler, List<ResultFlag> flags, String resultSet, String foreignColumn, boolean lazy) { // Class<?> javaTypeClass = resolveResultJavaType(resultType, property, javaType); //類型處理器 TypeHandler<?> typeHandlerInstance = resolveTypeHandler(javaTypeClass, typeHandler); //解析混合列 List<ResultMapping> composites = parseCompositeColumnName(column); //構建ResultMapping return new ResultMapping.Builder(configuration, property, column, javaTypeClass) .jdbcType(jdbcType) //對嵌套查詢ID進行namespace處理 .nestedQueryId(applyCurrentNamespace(nestedSelect, true)) //對嵌套ResultMap進行namespace處理 .nestedResultMapId(applyCurrentNamespace(nestedResultMap, true)) .resultSet(resultSet) .typeHandler(typeHandlerInstance) .flags(flags == null ? new ArrayList<ResultFlag>() : flags) .composites(composites) .notNullColumns(parseMultipleColumnNames(notNullColumn)) .columnPrefix(columnPrefix) .foreignColumn(foreignColumn) .lazy(lazy) .build(); } private List<ResultMapping> parseCompositeColumnName(String columnName) { List<ResultMapping> composites = new ArrayList<ResultMapping>(); //如果columnName不為null 同時colunmnName中含有"=" 或者含有","號 if (columnName != null && (columnName.indexOf(‘=‘) > -1 || columnName.indexOf(‘,‘) > -1)) { //分割字符串 StringTokenizer parser = new StringTokenizer(columnName, "{}=, ", false); while (parser.hasMoreTokens()) { //獲取屬性 String property = parser.nextToken(); //獲取列 String column = parser.nextToken(); //構建復合的ResultMapping ResultMapping complexResultMapping = new ResultMapping.Builder( configuration, property, column, configuration.getTypeHandlerRegistry().getUnknownTypeHandler()).build(); composites.add(complexResultMapping); } } return composites; }
這樣得到的結果是: 至此可以發現{id=id}被解析為了一個復合resultMapping那麽在使用的時候又是如何處理的呢? 在DefaultResultSetHandler中對於構造方法中的嵌套查詢處理如下,如果配置的是復合映射在處理復合映射的內部映射
//獲取嵌套查詢構造參數的值 private Object getNestedQueryConstructorValue(ResultSet rs, ResultMapping constructorMapping, String columnPrefix) throws SQLException { //嵌套查詢ID final String nestedQueryId = constructorMapping.getNestedQueryId(); //嵌套查詢MappedStatement final MappedStatement nestedQuery = configuration.getMappedStatement(nestedQueryId); //嵌套查詢參數類型 final Class nestedQueryParameterType = nestedQuery.getParameterMap().getType(); //獲取嵌套查詢入參值 final Object nestedQueryParameterObject = prepareParameterForNestedQuery(rs, constructorMapping, nestedQueryParameterType, columnPrefix); Object value = null; if (nestedQueryParameterObject != null) { final BoundSql nestedBoundSql = nestedQuery.getBoundSql(nestedQueryParameterObject); final CacheKey key = executor.createCacheKey(nestedQuery, nestedQueryParameterObject, RowBounds.DEFAULT, nestedBoundSql); final Class targetType = constructorMapping.getJavaType(); final ResultLoader resultLoader = new ResultLoader(configuration, executor, nestedQuery, nestedQueryParameterObject, targetType, key, nestedBoundSql); value = resultLoader.loadResult(); } return value; } private Object prepareParameterForNestedQuery(ResultSet rs, ResultMapping resultMapping, Class parameterType, String columnPrefix) throws SQLException { //如果是復合映射 if (resultMapping.isCompositeResult()) { return prepareCompositeKeyParameter(rs, resultMapping, parameterType, columnPrefix); } else { return prepareSimpleKeyParameter(rs, resultMapping, parameterType, columnPrefix); } } private Object prepareCompositeKeyParameter(ResultSet rs, ResultMapping resultMapping, Class parameterType, String columnPrefix) throws SQLException { //創建參數對象 final Object parameterObject = instantiateParameterObject(parameterType); final MetaObject metaObject = configuration.newMetaObject(parameterObject); boolean foundValues = false; //遍歷復合結果映射 for (ResultMapping innerResultMapping : resultMapping.getComposites()) { //獲取參數類型 final Class propType = metaObject.getSetterType(innerResultMapping.getProperty()); //獲取類型處理器 final TypeHandler typeHandler = typeHandlerRegistry.getTypeHandler(propType); //獲取值 final Object propValue = typeHandler.getResult(rs, prependPrefix(innerResultMapping.getColumn(), columnPrefix)); // 如果參數值不為null則設置到參數對象 if (propValue != null) { metaObject.setValue(innerResultMapping.getProperty(), propValue); foundValues = true; } } return foundValues ? parameterObject : null; }
此時獲取到的入參 id值為1 ,同樣在其它嵌套查詢中也可以使用復合映射
<resultMap id="addressMapper" type="org.apache.ibatis.submitted.column_prefix.Address"> <constructor> <idArg column="id" javaType="int" /> <arg column="state" javaType="string" /> </constructor> <result property="city" column="city" /> <result property="hasPhone" column="has_phone" /> <association property="stateBird" select="selectStateBird" column="state" /> <association property="zip" select="selectZip" column="{state=state,city=city}" /> <association property="phone1" select="selectPhone" column="phone1_id" /> <association property="phone2" select="selectPhone" column="phone2_id" /> <discriminator column="addr_type" javaType="int"> <case value="1" resultType="org.apache.ibatis.submitted.column_prefix.AddressWithCaution"> <result property="caution" column="caution" /> </case> </discriminator> </resultMap>
Mybatis ResultMap復合映射使用以及源碼分析