`
blue2048
  • 浏览: 182725 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Mybatis深入剖析 - typeHandlerRegistry parameterHandler resultSetHandler

阅读更多

Statement在具体orm的过程总,主要靠这三个类进行参数装配,结果装配和相应的类型转换

  • parameterHandler
  • resultSetHandler
  • typeHandlerRegistry
  protected BaseStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) {
    this.configuration = mappedStatement.getConfiguration();
    this.executor = executor;
    this.mappedStatement = mappedStatement;
    this.rowBounds = rowBounds;

    this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
    this.objectFactory = configuration.getObjectFactory();

    this.boundSql = mappedStatement.getBoundSql(parameterObject);

    this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);
    this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql);
  }

 

 

typeHandlerRegistry
先介绍Typehandler,因为它是ParameterHandler和ResultsetHandler中,类型转化的工具,概括而说,typehandler就是把Java类型数据和JDBC类型的数据相互转换,mybatis给我们提供了一些常规类型的handler,我们可以自己实现一些特殊类型的handler


 

parameterHandler

    只有一个实现类,DefaultParameterHandler,它负责把代码传入的参数,装配到preparestatement中去,根据下面的代码可以看出,根据mapper中配置的解析出的parameterMapping规则从代码传入parameterObject的对象出反射取出属性值,在根据TypeHandler做java到jdbc类型的数据格式转换,最终装配到jdbc的statement中去

 

 

  MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
      for (int i = 0; i < parameterMappings.size(); i++) {
        ParameterMapping parameterMapping = parameterMappings.get(i);
        if (parameterMapping.getMode() != ParameterMode.OUT) {
          Object value;
          String propertyName = parameterMapping.getProperty();
          PropertyTokenizer prop = new PropertyTokenizer(propertyName);
          if (parameterObject == null) {
            value = null;
          } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
            value = parameterObject;
          } else if (boundSql.hasAdditionalParameter(propertyName)) {
            value = boundSql.getAdditionalParameter(propertyName);
          } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
              && boundSql.hasAdditionalParameter(prop.getName())) {
            value = boundSql.getAdditionalParameter(prop.getName());
            if (value != null) {
              value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
            }
          } else {
            value = metaObject == null ? null : metaObject.getValue(propertyName);
          }
          TypeHandler typeHandler = parameterMapping.getTypeHandler();
          if (typeHandler == null) {
            throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
          }
          typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
        }

 

 

resultSetHandler

 有两个实现,一般结果集处理由FastResultSetHandler完成,嵌套符合结果集的处理由NestedResultSetHandler完成,看下面的resultset的配置就能明白是啥意思了

ResultSetHandler resultSetHandler = mappedStatement.hasNestedResultMaps() ? new NestedResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql,
        rowBounds) : new FastResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);

 

<resultMap id="processDefinitionResultMap"  
        type="com.apusic.bpm.engine.definition.ProcessDefinition">  
        <constructor>  
            <idArg column="ID_" jdbcType="VARCHAR" javaType="java.lang.String" />  
            <arg column="KEY_" jdbcType="VARCHAR" javaType="java.lang.String" />  
            <arg column="NAME_" jdbcType="VARCHAR" javaType="java.lang.String" />  
            <arg column="VERSION_" jdbcType="INTEGER" javaType="java.lang.Long" />  
            <arg column="DESCRIPTION_" jdbcType="VARCHAR" javaType="java.lang.String" />  
            <arg javaType="com.apusic.bpm.engine.definition.ProcessDefinitionResourceMapping" resultMap="resourceMappingResultMap" />  
        </constructor>  
    </resultMap>  
      
    <resultMap id="resourceMappingResultMap"  
        type="com.apusic.bpm.engine.definition.ProcessDefinitionResourceMapping">  
        <constructor>  
            <arg column="PROCESS_DEFINITION_PATH_" jdbcType="VARCHAR" javaType="java.lang.String"/>  
            <arg column="RESOURCE_PATHS_" jdbcType="VARCHAR" javaType="java.util.Set" />  
        </constructor>  
    </resultMap>  

 FastResultSetHandler的核心代码如下,从jdbc resultset结果集中去除结果,根据TypeHandler将jdbc类型的数据转换成java类型的数据,塞回到结果集java对象中

public List handleResultSets(Statement stmt) throws SQLException {
    final List multipleResults = new ArrayList();
    final List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    int resultSetCount = 0;
    ResultSet rs = stmt.getResultSet();

    while (rs == null) {
      // move forward to get the first resultset in case the driver
      // doesn't return the resultset as the first result (HSQLDB 2.1)
      if (stmt.getMoreResults()) {
        rs = stmt.getResultSet();
      } else {
        if (stmt.getUpdateCount() == -1) {
          // no more results.  Must be no resultset
          break;
        }
      }
    }

    validateResultMapsCount(rs, resultMapCount);
    while (rs != null && resultMapCount > resultSetCount) {
      final ResultMap resultMap = resultMaps.get(resultSetCount);
      handleResultSet(rs, resultMap, multipleResults);
      rs = getNextResultSet(stmt);
      cleanUpAfterHandlingResultSet();
      resultSetCount++;
    }
    return collapseSingleResultList(multipleResults);
  }

 

  • 大小: 19.3 KB
分享到:
评论

相关推荐

    MyBatis-Plus 的官方示例(mybatis-plus-samples-master.zip)

    本工程为 MyBatis-Plus 的官方示例,项目结构如下: mybatis-plus-sample-quickstart: 快速开始示例 mybatis-plus-sample-quickstart-springmvc: 快速开始示例(Spring MVC版本) mybatis-plus-sample-reduce-...

    mybatis-3-config.dtd mybatis-3-mapper.dtd

    首先,让我们深入了解一下`mybatis-3-config.dtd`。这个文件是MyBatis配置文件的DTD,它规定了`mybatis-config.xml`文件中的元素和属性。`mybatis-config.xml`是MyBatis的核心配置文件,它包含了全局设置,如数据源...

    mybatis-plus-boot-starter-3.5.1-API文档-中文版.zip

    赠送jar包:mybatis-plus-boot-starter-3.5.1.jar; 赠送原API文档:mybatis-plus-boot-starter-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-boot-starter-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-...

    mybatis-plus-extension-3.5.1-API文档-中英对照版.zip

    赠送jar包:mybatis-plus-extension-3.5.1.jar; 赠送原API文档:mybatis-plus-extension-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-extension-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...

    mybatis-generator-config_1_0.dtd

    mybatis-generator-config_1_0.dtd文件存在于mybatis-generator-core-1.3.2.jar包中,路径如下org/mybatis/generator/config/xml/mybatis-generator-config_1_0.dtd 可以设置开发工具的dtd配置,配置...

    mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip

    mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip ...

    mybatis-spring-2.0.6-API文档-中文版.zip

    赠送jar包:mybatis-spring-2.0.6.jar; 赠送原API文档:mybatis-spring-2.0.6-javadoc.jar; 赠送源代码:mybatis-spring-2.0.6-sources.jar; 赠送Maven依赖信息文件:mybatis-spring-2.0.6.pom; 包含翻译后的API...

    mybatis-spring-boot-autoconfigure-1.3.2-API文档-中英对照版.zip

    赠送jar包:mybatis-spring-boot-autoconfigure-1.3.2.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-1.3.2-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-1.3.2-sources.jar; 赠送...

    mybatis-plus-boot-starter-3.1.0-API文档-中文版.zip

    赠送jar包:mybatis-plus-boot-starter-3.1.0.jar; 赠送原API文档:mybatis-plus-boot-starter-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-boot-starter-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-...

    mybatis-generator-core-1.3.7-API文档-中文版.zip

    赠送jar包:mybatis-generator-core-1.3.7.jar; 赠送原API文档:mybatis-generator-core-1.3.7-javadoc.jar; 赠送源代码:mybatis-generator-core-1.3.7-sources.jar; 赠送Maven依赖信息文件:mybatis-generator-...

    mybatis-3-config/mapper.dtd 解决mybatis头文件报错

    然后打开eclipse -&gt;Window-&gt;prefenrence-&gt;XML-&gt;XML Catalog-&gt;User Specifiled Entreis-&gt;Add-&gt;Location(此处是你放dtd文件的位置例如:‪D:\mybatis\mybatis-3-config.dtd)-&gt;Key(如果更改config,此处应该是:-//...

    mybatis-spring-2.0.0-API文档-中文版.zip

    赠送jar包:mybatis-spring-2.0.0.jar; 赠送原API文档:mybatis-spring-2.0.0-javadoc.jar; 赠送源代码:mybatis-spring-2.0.0-sources.jar; 赠送Maven依赖信息文件:mybatis-spring-2.0.0.pom; 包含翻译后的API...

    开发工具 框架JAR mybatis-spring-1.3.1

    开发工具 框架JAR mybatis-spring-1.3.1开发工具 框架JAR mybatis-spring-1.3.1开发工具 框架JAR mybatis-spring-1.3.1开发工具 框架JAR mybatis-spring-1.3.1开发工具 框架JAR mybatis-spring-1.3.1开发工具 框架...

    mybatis-plus-annotation-3.5.1-API文档-中文版.zip

    赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...

    mybatis-plus-boot-starter-3.4.3.3.jar

    mybatis-plus-boot-starter.jar 各个版本下载, SpringBoot 集成 MybatisPlus jar 包下载, Mybatis-Plus(简称MP)是一个基于MyBatis的增强工具库,它简化了与数据库的交互操作并提供了一系列增强功能,使开发者...

Global site tag (gtag.js) - Google Analytics