mybatis源码解析11---ParameterHandler解析

时间:2024-01-06 15:36:20

ParameterHandler接口是参数处理器,位于mybatis包的org.apache.ibatis.executor.parameter下,源码如下:

 public interface ParameterHandler {

 Object getParameterObject();//获取参数

 void setParameters(PreparedStatement ps)//设置参数
throws SQLException; }

可见ParameterHandler接口只有简单的两个方法,一个是获取参数一个是设置参数。ParameterHandler接口默认实现类是DefaultParameterHandler,主要源码如下:

 public class DefaultParameterHandler implements ParameterHandler {

 private final TypeHandlerRegistry typeHandlerRegistry;

 private final MappedStatement mappedStatement;
private final Object parameterObject;
private BoundSql boundSql;
private 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;//返回参数
} @Override
public void setParameters(PreparedStatement ps) {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();//获取所有参数,ParameterMapping是jdbc和java类型之间的对应关系
if (parameterMappings != null) {
//遍历所有参数,将java 类型设置成jdbc类型
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
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) {
jdbcType = configuration.getJdbcTypeForNull();
}
try {
typeHandler.setParameter(ps, i + 1, value, jdbcType);
} catch (TypeException e) {
throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
} catch (SQLException e) {
throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
}
}
}
}
} }

而ParameterHandler的初始化同样也是在Configuration中实现的,代码如下:

   public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}