博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis中的resultType和resultMap 区别
阅读量:7064 次
发布时间:2019-06-28

本文共 4235 字,大约阅读时间需要 14 分钟。

hot3.png

 是mybatis 中返回类型一定用到的,但不会同时出现。mybatis返回类型肯定是map结构,然后根据返回类型是map还是对象类型,再转换。

在给对象设置属性的时候,两个方法肯定会调用。

private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {    final ResultLoaderMap lazyLoader = new ResultLoaderMap();    Object resultObject = createResultObject(rsw, resultMap, lazyLoader, null);    if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {      final MetaObject metaObject = configuration.newMetaObject(resultObject);      boolean foundValues = resultMap.getConstructorResultMappings().size() > 0;      if (shouldApplyAutomaticMappings(resultMap, !AutoMappingBehavior.NONE.equals(configuration.getAutoMappingBehavior()))) {                foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;      }      foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;      foundValues = lazyLoader.size() > 0 || foundValues;      resultObject = foundValues ? resultObject : null;      return resultObject;    }    return resultObject;  }
private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {    final List
unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix);
//unmappedColumnNames是配置文件中未配的resultMap中的属性,下面是自动映射规则,去设值  boolean foundValues = false; for (String columnName : unmappedColumnNames) { String propertyName = columnName; if (columnPrefix != null && columnPrefix.length() > 0) { // When columnPrefix is specified, // ignore columns without the prefix. if (columnName.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) { propertyName = columnName.substring(columnPrefix.length()); } else { continue; } }
//这个自动映射属性的规则,很普通,根据映射出的属性,利用反射查找该对象,是否存在此属性,以便设值  final String property = metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase()); if (property != null && metaObject.hasSetter(property)) { final Class
propertyType = metaObject.getSetterType(property); if (typeHandlerRegistry.hasTypeHandler(propertyType)) { final TypeHandler
typeHandler = rsw.getTypeHandler(propertyType, columnName); final Object value = typeHandler.getResult(rsw.getResultSet(), columnName); if (value != null || configuration.isCallSettersOnNulls()) { // issue #377, call setter on nulls if (value != null || !propertyType.isPrimitive()) { metaObject.setValue(property, value); } foundValues = true; } } } } return foundValues; }
private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, ResultLoaderMap lazyLoader, String columnPrefix)      throws SQLException {    final List
mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
//mappedColumnNames 是配置文件中配置resultMap中对应的映射,如果不配,此方法体就不会执行,走上面那该自动映射规则去映射属性(非常智能,但这个智能属性对应,不怎么特殊,也没提供自动映射拓展点  boolean foundValues = false; final List
propertyMappings = resultMap.getPropertyResultMappings(); for (ResultMapping propertyMapping : propertyMappings) { final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix); if (propertyMapping.isCompositeResult() || (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) || propertyMapping.getResultSet() != null) { Object value = getPropertyMappingValue(rsw.getResultSet(), metaObject, propertyMapping, lazyLoader, columnPrefix); final String property = propertyMapping.getProperty(); // issue #541 make property optional if (value != NO_VALUE && property != null && (value != null || configuration.isCallSettersOnNulls())) { // issue #377, call setter on nulls if (value != null || !metaObject.getSetterType(property).isPrimitive()) { metaObject.setValue(property, value); } foundValues = true; } } } return foundValues; }

转载于:https://my.oschina.net/doctor2014/blog/385847

你可能感兴趣的文章
Promise源码解析-步步为营皆可及
查看>>
武汉区块链软件技术公司:区块链将如何优化产业链?
查看>>
python机器学习实战(二)
查看>>
[译] 2019 年了,为什么我还在用 jQuery?
查看>>
蕾丝 | 内外皆精致的女子
查看>>
人工智能与勒索病毒较量,你猜最后谁能赢了?
查看>>
前端面试每日3+1(周汇总2019.05.05)
查看>>
RPA:制造业的下一个改变者
查看>>
VSCode Python开发环境配置
查看>>
208道 java 高频面试题和答案
查看>>
nginx反向代理配置
查看>>
MySQL学习笔记 初学基础篇
查看>>
一步步教你用 CSS 为 SVG 添加过滤器
查看>>
TeeChart Pro VCL/FMX教程(一):入门——构建图表
查看>>
微服务架构 SpringCloud(二)Eureka(服务注册和服务发现基础篇)
查看>>
oracle RAC的客户端HA配置
查看>>
VsCode编辑器
查看>>
spring cloud开发、部署注意事项
查看>>
又一款基于BCH开发出来的社交软件BlockPress
查看>>
ttlsa教程系列之mongodb——(五)mongodb架构-复制原理&复制集
查看>>