java?executor包參數處理功能?
sql語句中的參數賦值是有由executor
包中的parameter
子包完成的。
parameter
子包其實只有一個parameterHandler
接口,它定義了2個方法:
public interface ParameterHandler { ? Object getParameterObject(); ? void setParameters(PreparedStatement ps) ? ? ? throws SQLException; }
ParameterHandler
接口有一個默認的實現類DefaultParameterHandler
,它在scripting
包的子包中。
mybatis
中支持進行參數設置的語句類型是PreparedStatement
接口及其子接口CallableStatement
, 所以setParameters
的輸入參數是PreparedStatement
類型。
setParameters
方法的實現邏輯就是依次取出每個參數的值,然后根據參數類型調用PreparedStatement
中的賦值方法進行賦值。
public class DefaultParameterHandler implements ParameterHandler { ? // 類型處理器注冊表 ? private final TypeHandlerRegistry typeHandlerRegistry; ? // MappedStatement對象(包含完整的增刪改查節(jié)點信息) ? private final MappedStatement mappedStatement; ? // 參數對象 ? 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; ? } ? /** ? ?* 為語句設置參數 ? ?* @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) { ? ? ? ? ? ? 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); ? ? ? ? ? } ? ? ? ? } ? ? ? } ? ? } ? } }
到此這篇關于java executor包參數處理功能 的文章就介紹到這了,更多相關executor包參數處理功能 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring mvc常用注解_動力節(jié)點Java學院整理
這篇文章主要介紹了spring mvc常用注解,詳細的介紹了@RequestMapping, @RequestParam, @ModelAttribute等等這樣類似的注解,有興趣的可以了解一下2017-08-08Java中使用ConcurrentHashMap實現線程安全的Map
在Java中,ConcurrentHashMap是一種線程安全的哈希表,可用于實現多線程環(huán)境下的Map操作。它支持高并發(fā)的讀寫操作,通過分段鎖的方式實現線程安全,同時提供了一些高級功能,比如迭代器弱一致性和批量操作等。ConcurrentHashMap在高并發(fā)場景中具有重要的應用價值2023-04-04