DefaultParameterHandler 参数处理器

ParameterHandler接口类,定义设置参数的方法setParameters(PreparedStatement ps)

/**
 * A parameter handler sets the parameters of the {@code PreparedStatement}.
 * @author Clinton Begin
 */
public interface ParameterHandler {
  
  //获取参数信息
  Object getParameterObject();

   //为PreparedStatement设置具体实参
  void setParameters(PreparedStatement ps)
      throws SQLException;

}

参数处理类(ParameterHandler)默认实现类(DefaultParameterHandler),为java.sql.PreparedStatement 设置具体的值。交由typeHandler设置具体的

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;
    // MappedStatement对象(包含完整的增删改查节点信息)
    //在此处仅仅是用来打印错误日志
    private final MappedStatement mappedStatement;
    // 参数对象Map类型
    private final Object parameterObject;
    // BoundSql对象(包含SQL语句、参数、实参信息)
    private final BoundSql boundSql;
    // 配置信息
    private final Configuration configuration;
     //构造函数
    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;
    }
    /**
   * 为PreparedStatement设置参数
   * @param ps 语句
   */
    @Override
    public void setParameters(PreparedStatement ps) {
        ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
        // 取出参数列表
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        if (parameterMappings != null) {
            //遍历参数
            for (int i = 0; i < parameterMappings.size(); i++) {
                ParameterMapping parameterMapping = parameterMappings.get(i);
                // ParameterMode.OUT是CallableStatement的输出参数,已经单独注册。故忽略
                if (parameterMapping.getMode() != ParameterMode.OUT) {
                    Object value;
                    // 取出属性名称
                    String propertyName = parameterMapping.getProperty();
                    //是否是附加参数
                    if (boundSql.hasAdditionalParameter(propertyName)) {
                    // 从附加参数中读取属性值
                    value = boundSql.getAdditionalParameter(propertyName);
                } 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 = parameterMapping.getTypeHandler();
                    //获取数据库字段类型
                    JdbcType jdbcType = parameterMapping.getJdbcType();
                    if (value == null && jdbcType == null) {//处理值和jdbc类型都为空
                    jdbcType = configuration.getJdbcTypeForNull();
                }
                    try {
                    // ☆☆☆ 此方法最终根据参数类型,调用java.sql.PreparedStatement类中的参数赋值方法,对SQL语句中的参数赋值
                    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,开发语言,mybatis,java)