Mybatis3.5.1源码分析
- Mybatis-SqlSessionFactoryBuilder,XMLConfigBuilder,XPathParser源码解析
- Mybatis-Configuration源码解析
- Mybatis-事务对象源码解析
- Mybatis-数据源源码解析
- Mybatis缓存策略源码解析
- Mybatis-DatabaseIdProvider源码解析
- Mybatis-TypeHandler源码解析
- Mybatis-Reflector源码解析
- Mybatis-ObjectFactory,ObjectWrapperFactory源码分析
- Mybatis-Mapper各类标签封装类源码解析
- Mybatis-XMLMapperBuilder,XMLStatmentBuilder源码分析
- Mybatis-MapperAnnotationBuilder源码分析
- [Mybatis-MetaObject,MetaClass源码解析]https://www.jianshu.com/p/f51fa552f30a)
- Mybatis-LanguageDriver源码解析
- Mybatis-SqlSource源码解析
- Mybatis-SqlNode源码解析
- Mybatis-KeyGenerator源码解析
- Mybatis-Executor源码解析
- Mybatis-ParameterHandler源码解析
- Mybatis-StatementHandler源码解析
- Mybatis-DefaultResultSetHandler(一)源码解析
- Mybatis-DefaultResultSetHandler(二)源码解析
- Mybatis-ResultHandler,Cursor,RowBounds 源码分析
- Mybatis-MapperProxy源码解析
- Mybatis-SqlSession源码解析
- 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<ParameterMapping> 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);
}
}
}
}
}
}