Mybatis-ParameterHandler源码解析

Mybatis3.5.1源码分析

  1. Mybatis-SqlSessionFactoryBuilder,XMLConfigBuilder,XPathParser源码解析
  2. Mybatis-Configuration源码解析
  3. Mybatis-事务对象源码解析
  4. Mybatis-数据源源码解析
  5. Mybatis缓存策略源码解析
  6. Mybatis-DatabaseIdProvider源码解析
  7. Mybatis-TypeHandler源码解析
  8. Mybatis-Reflector源码解析
  9. Mybatis-ObjectFactory,ObjectWrapperFactory源码分析
  10. Mybatis-Mapper各类标签封装类源码解析
  11. Mybatis-XMLMapperBuilder,XMLStatmentBuilder源码分析
  12. Mybatis-MapperAnnotationBuilder源码分析
  13. [Mybatis-MetaObject,MetaClass源码解析]https://www.jianshu.com/p/f51fa552f30a)
  14. Mybatis-LanguageDriver源码解析
  15. Mybatis-SqlSource源码解析
  16. Mybatis-SqlNode源码解析
  17. Mybatis-KeyGenerator源码解析
  18. Mybatis-Executor源码解析
  19. Mybatis-ParameterHandler源码解析
  20. Mybatis-StatementHandler源码解析
  21. Mybatis-DefaultResultSetHandler(一)源码解析
  22. Mybatis-DefaultResultSetHandler(二)源码解析
  23. Mybatis-ResultHandler,Cursor,RowBounds 源码分析
  24. Mybatis-MapperProxy源码解析
  25. Mybatis-SqlSession源码解析
  26. Mybatis-Interceptor源码解析

ParameterHandler

/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.executor.parameter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * A parameter handler sets the parameters of the {@code PreparedStatement}.
 * 参数处理器,设置参数到 {@code PreparedStatement}.
 * @author Clinton Begin
 */
public interface ParameterHandler {

  /**
   * 获取参数对象
   * @return
   */
  Object getParameterObject();

  /**
   * 设置参数对象到 {@code ps}
   */
  void setParameters(PreparedStatement ps)
      throws SQLException;

}

DefaultParameterHandler

/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.scripting.defaults;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeException;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

/**
 * 默认的参数处理器
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public class DefaultParameterHandler implements ParameterHandler {

  /**
   * 类型处理接收器
   */
  private final TypeHandlerRegistry typeHandlerRegistry;

  /**
   *  Mapper.xml文件的select,delete,update,insert这些DML标签的封装类
   */
  private final MappedStatement mappedStatement;
  /**
   * 参数对象
   */
  private final Object parameterObject;
  /**
   * 可执行SQL封装
   */
  private final BoundSql boundSql;
  /**
   * mybatis全局配置信息
   */
  private final Configuration configuration;

  /**
   *
   * @param mappedStatement  Mapper.xml文件的select,delete,update,insert这些DML标签的封装类
   * @param parameterObject 参数对象
   * @param boundSql 可执行SQL封装
   */
  public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
    this.mappedStatement = mappedStatement;
    this.configuration = mappedStatement.getConfiguration();
    this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
    this.parameterObject = parameterObject;
    this.boundSql = boundSql;
  }

  /**
   * 获取参数对象
   */
  @Override
  public Object getParameterObject() {
    return parameterObject;
  }

  /**
   * 设置参数对象到 {@code ps}
   * @param ps 预编译得SQL语句的对象,也就是说SQL语句被预编译并存储在PreparedStatement对象中,然后可以使用此对象多次高效地执行改语句。
   */
  @Override
  public void setParameters(PreparedStatement ps) {
    //设置错误日志上下文
    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
    //获取参数映射列表
    List parameterMappings = boundSql.getParameterMappings();
    //如果参数映射列表不为null
    if (parameterMappings != null) {
      //变量参数映射
      for (int i = 0; i < parameterMappings.size(); i++) {
        //获取参数映射
        ParameterMapping parameterMapping = parameterMappings.get(i);
        //如果参数映射配置的模式不是输出模式
        if (parameterMapping.getMode() != ParameterMode.OUT) {
          //新建一个参数对象
          Object value;
          //获取参数映射配置的属性
          String propertyName = parameterMapping.getProperty();
          //在boundSql中存在额外的参数
          if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
            //获取额外参数
            value = boundSql.getAdditionalParameter(propertyName);
            //参数对象为null
          } else if (parameterObject == null) {
            value = null;
            //存在类型接收处理器
          } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
            value = parameterObject;
          } else {
            //从元对象中获取
            MetaObject metaObject = configuration.newMetaObject(parameterObject);
            value = metaObject.getValue(propertyName);
          }
          //获取参数映射配置的TypeHandler
          TypeHandler typeHandler = parameterMapping.getTypeHandler();
          //获取参数映射配置的jdbc类型
          JdbcType jdbcType = parameterMapping.getJdbcType();
          //如果值为null 且 jdbcType也为null
          if (value == null && jdbcType == null) {
            //获取null对应的jdbcType
            jdbcType = configuration.getJdbcTypeForNull();
          }
          try {
            //将参数对应交给typeHandler赋值进ps
            typeHandler.setParameter(ps, i + 1, value, jdbcType);
          } catch (TypeException | SQLException e) {
            throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
          }
        }
      }
    }
  }

}

你可能感兴趣的:(Mybatis-ParameterHandler源码解析)