设置如果是PreparedStatement,则最终都是调用PreparedStatement的setXXX方法
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET AGE=? WHERE ID = ?");
pstmt.setInt(1, 20);
pstmt.setInt(2, 110592);
注意第1个参数是1(parameterIndex the first parameter is 1, the second is 2, ...)
PreparedStatement相关方法:
BoundSql
TypeHandler
这里只关注setParameter函数
public interface TypeHandler<T> {
void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
.......................
}
如果javaType=UnknownTypeHandler ,并且未指定JdbcType,会根据parameter的类型,推断出对应的TypeHandler,比如,参数是String类型的,会推断StringTypeHandler
public class UnknownTypeHandler extends BaseTypeHandler<Object> {
private static final ObjectTypeHandler OBJECT_TYPE_HANDLER = new ObjectTypeHandler();
private TypeHandlerRegistry typeHandlerRegistry;
public UnknownTypeHandler(TypeHandlerRegistry typeHandlerRegistry) {
this.typeHandlerRegistry = typeHandlerRegistry;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType)
throws SQLException {
TypeHandler handler = resolveTypeHandler(parameter, jdbcType); // 根据parameter的类型,找到匹配的TypeHandler
handler.setParameter(ps, i, parameter, jdbcType);
}
}
StringTypeHandler
public class StringTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, parameter);
}
}
DefaultParameterHandler
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); // 具体的第几个参数-详细信息
if (parameterMapping.getMode() != ParameterMode.OUT) { // IN or INOUT 才处理
Object value;
String propertyName = parameterMapping.getProperty(); //得到入参的属性名 比如username、a.id 等 #{id} #{a.id}
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())) { // 单个参数的类型,如果在typeHandlerRegistry里注册了,value就是=parameterObject
value = parameterObject; // 单个参数情况下,并且在typeHandlerRegistry里注册了parameterObject的类型,则value就是=parameterObject
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject); // 一般都是走这里的
value = metaObject.getValue(propertyName); // 通过metaObject得到具体要设置pos的值 first parameter is 1, the second is 2
}
TypeHandler typeHandler = parameterMapping.getTypeHandler(); // 未指定是UnknownTypeHandler
JdbcType jdbcType = parameterMapping.getJdbcType();
if (value == null && jdbcType == null) {
jdbcType = configuration.getJdbcTypeForNull();
}
try {
typeHandler.setParameter(ps, i + 1, value, jdbcType); // first parameter is 1, the second is 2
} catch (TypeException | SQLException e) {
throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
}
}
}
}
}