java?executor包參數(shù)處理功能?
sql語句中的參數(shù)賦值是有由executor包中的parameter子包完成的。
parameter子包其實只有一個parameterHandler接口,它定義了2個方法:
public interface ParameterHandler {
? Object getParameterObject();
? void setParameters(PreparedStatement ps)
? ? ? throws SQLException;
}ParameterHandler接口有一個默認的實現(xiàn)類DefaultParameterHandler,它在scripting包的子包中。
mybatis中支持進行參數(shù)設置的語句類型是PreparedStatement接口及其子接口CallableStatement, 所以setParameters的輸入?yún)?shù)是PreparedStatement類型。
setParameters方法的實現(xiàn)邏輯就是依次取出每個參數(shù)的值,然后根據(jù)參數(shù)類型調用PreparedStatement中的賦值方法進行賦值。
public class DefaultParameterHandler implements ParameterHandler {
? // 類型處理器注冊表
? private final TypeHandlerRegistry typeHandlerRegistry;
? // MappedStatement對象(包含完整的增刪改查節(jié)點信息)
? private final MappedStatement mappedStatement;
? // 參數(shù)對象
? private final Object parameterObject;
? // BoundSql對象(包含SQL語句、參數(shù)、實參信息)
? 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;
? }
? /**
? ?* 為語句設置參數(shù)
? ?* @param ps 語句
? ?*/
? @Override
? public void setParameters(PreparedStatement ps) {
? ? ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
? ? // 取出參數(shù)列表
? ? List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
? ? if (parameterMappings != null) {
? ? ? for (int i = 0; i < parameterMappings.size(); i++) {
? ? ? ? ParameterMapping parameterMapping = parameterMappings.get(i);
? ? ? ? // ParameterMode.OUT是CallableStatement的輸出參數(shù),已經單獨注冊。故忽略
? ? ? ? if (parameterMapping.getMode() != ParameterMode.OUT) {
? ? ? ? ? Object value;
? ? ? ? ? // 取出屬性名稱
? ? ? ? ? String propertyName = parameterMapping.getProperty();
? ? ? ? ? if (boundSql.hasAdditionalParameter(propertyName)) {
? ? ? ? ? ? // 從附加參數(shù)中讀取屬性值
? ? ? ? ? ? value = boundSql.getAdditionalParameter(propertyName);
? ? ? ? ? } else if (parameterObject == null) {
? ? ? ? ? ? value = null;
? ? ? ? ? } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
? ? ? ? ? ? // 參數(shù)對象是基本類型,則參數(shù)對象即為參數(shù)值
? ? ? ? ? ? value = parameterObject;
? ? ? ? ? } else {
? ? ? ? ? ? // 參數(shù)對象是復雜類型,取出參數(shù)對象的該屬性值
? ? ? ? ? ? MetaObject metaObject = configuration.newMetaObject(parameterObject);
? ? ? ? ? ? value = metaObject.getValue(propertyName);
? ? ? ? ? }
? ? ? ? ? // 確定該參數(shù)的處理器
? ? ? ? ? TypeHandler typeHandler = parameterMapping.getTypeHandler();
? ? ? ? ? JdbcType jdbcType = parameterMapping.getJdbcType();
? ? ? ? ? if (value == null && jdbcType == null) {
? ? ? ? ? ? jdbcType = configuration.getJdbcTypeForNull();
? ? ? ? ? }
? ? ? ? ? try {
? ? ? ? ? ? // 此方法最終根據(jù)參數(shù)類型,調用java.sql.PreparedStatement類中的參數(shù)賦值方法,對SQL語句中的參數(shù)賦值
? ? ? ? ? ? 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包參數(shù)處理功能 的文章就介紹到這了,更多相關executor包參數(shù)處理功能 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring mvc常用注解_動力節(jié)點Java學院整理
這篇文章主要介紹了spring mvc常用注解,詳細的介紹了@RequestMapping, @RequestParam, @ModelAttribute等等這樣類似的注解,有興趣的可以了解一下2017-08-08
java 域對象共享數(shù)據(jù)的實現(xiàn)
本文主要介紹了java 域對象共享數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03
Java中使用ConcurrentHashMap實現(xiàn)線程安全的Map
在Java中,ConcurrentHashMap是一種線程安全的哈希表,可用于實現(xiàn)多線程環(huán)境下的Map操作。它支持高并發(fā)的讀寫操作,通過分段鎖的方式實現(xiàn)線程安全,同時提供了一些高級功能,比如迭代器弱一致性和批量操作等。ConcurrentHashMap在高并發(fā)場景中具有重要的應用價值2023-04-04

