java?executor包參數(shù)處理功能?
sql語(yǔ)句中的參數(shù)賦值是有由executor
包中的parameter
子包完成的。
parameter
子包其實(shí)只有一個(gè)parameterHandler
接口,它定義了2個(gè)方法:
public interface ParameterHandler { ? Object getParameterObject(); ? void setParameters(PreparedStatement ps) ? ? ? throws SQLException; }
ParameterHandler
接口有一個(gè)默認(rèn)的實(shí)現(xiàn)類DefaultParameterHandler
,它在scripting
包的子包中。
mybatis
中支持進(jìn)行參數(shù)設(shè)置的語(yǔ)句類型是PreparedStatement
接口及其子接口CallableStatement
, 所以setParameters
的輸入?yún)?shù)是PreparedStatement
類型。
setParameters
方法的實(shí)現(xiàn)邏輯就是依次取出每個(gè)參數(shù)的值,然后根據(jù)參數(shù)類型調(diào)用PreparedStatement
中的賦值方法進(jìn)行賦值。
public class DefaultParameterHandler implements ParameterHandler { ? // 類型處理器注冊(cè)表 ? private final TypeHandlerRegistry typeHandlerRegistry; ? // MappedStatement對(duì)象(包含完整的增刪改查節(jié)點(diǎn)信息) ? private final MappedStatement mappedStatement; ? // 參數(shù)對(duì)象 ? private final Object parameterObject; ? // BoundSql對(duì)象(包含SQL語(yǔ)句、參數(shù)、實(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; ? } ? /** ? ?* 為語(yǔ)句設(shè)置參數(shù) ? ?* @param ps 語(yǔ)句 ? ?*/ ? @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ù),已經(jīng)單獨(dú)注冊(cè)。故忽略 ? ? ? ? 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ù)對(duì)象是基本類型,則參數(shù)對(duì)象即為參數(shù)值 ? ? ? ? ? ? value = parameterObject; ? ? ? ? ? } else { ? ? ? ? ? ? // 參數(shù)對(duì)象是復(fù)雜類型,取出參數(shù)對(duì)象的該屬性值 ? ? ? ? ? ? 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ù)類型,調(diào)用java.sql.PreparedStatement類中的參數(shù)賦值方法,對(duì)SQL語(yǔ)句中的參數(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); ? ? ? ? ? } ? ? ? ? } ? ? ? } ? ? } ? } }
到此這篇關(guān)于java executor包參數(shù)處理功能 的文章就介紹到這了,更多相關(guān)executor包參數(shù)處理功能 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring mvc常用注解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了spring mvc常用注解,詳細(xì)的介紹了@RequestMapping, @RequestParam, @ModelAttribute等等這樣類似的注解,有興趣的可以了解一下2017-08-08java 域?qū)ο蠊蚕頂?shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了java 域?qū)ο蠊蚕頂?shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03使用Lombok @Builder注解導(dǎo)致默認(rèn)值無(wú)效的問(wèn)題
這篇文章主要介紹了使用Lombok @Builder注解導(dǎo)致默認(rèn)值無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Java中使用ConcurrentHashMap實(shí)現(xiàn)線程安全的Map
在Java中,ConcurrentHashMap是一種線程安全的哈希表,可用于實(shí)現(xiàn)多線程環(huán)境下的Map操作。它支持高并發(fā)的讀寫操作,通過(guò)分段鎖的方式實(shí)現(xiàn)線程安全,同時(shí)提供了一些高級(jí)功能,比如迭代器弱一致性和批量操作等。ConcurrentHashMap在高并發(fā)場(chǎng)景中具有重要的應(yīng)用價(jià)值2023-04-04SpringBoot解決jar包沖突的問(wèn)題,簡(jiǎn)單有效
這篇文章主要介紹了SpringBoot解決jar包沖突的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例
這篇文章主要介紹了java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-10-10