~~~
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.mapping;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.Configuration;
/**
* @author Clinton Begin
*/
public final class MappedStatement {
private String resource;
private Configuration configuration;
private String id;
private Integer fetchSize;
private Integer timeout;
private StatementType statementType;
private ResultSetType resultSetType;
private SqlSource sqlSource;
private Cache cache;
private ParameterMap parameterMap;
private List<ResultMap> resultMaps;
private boolean flushCacheRequired;
private boolean useCache;
private boolean resultOrdered;
private SqlCommandType sqlCommandType;
private KeyGenerator keyGenerator;
private String[] keyProperties;
private String[] keyColumns;
private boolean hasNestedResultMaps;
private String databaseId;
private Log statementLog;
private LanguageDriver lang;
private String[] resultSets;
MappedStatement() {
// constructor disabled
}
public static class Builder {
private MappedStatement mappedStatement = new MappedStatement();
public Builder(Configuration configuration, String id, SqlSource sqlSource, SqlCommandType sqlCommandType) {
mappedStatement.configuration = configuration;
mappedStatement.id = id;
mappedStatement.sqlSource = sqlSource;
mappedStatement.statementType = StatementType.PREPARED;
mappedStatement.resultSetType = ResultSetType.DEFAULT;
mappedStatement.parameterMap = new ParameterMap.Builder(configuration, "defaultParameterMap", null, new ArrayList<>()).build();
mappedStatement.resultMaps = new ArrayList<>();
mappedStatement.sqlCommandType = sqlCommandType;
mappedStatement.keyGenerator = configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType) ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
String logId = id;
if (configuration.getLogPrefix() != null) {
logId = configuration.getLogPrefix() + id;
}
mappedStatement.statementLog = LogFactory.getLog(logId);
mappedStatement.lang = configuration.getDefaultScriptingLanguageInstance();
}
public Builder resource(String resource) {
mappedStatement.resource = resource;
return this;
}
public String id() {
return mappedStatement.id;
}
public Builder parameterMap(ParameterMap parameterMap) {
mappedStatement.parameterMap = parameterMap;
return this;
}
public Builder resultMaps(List<ResultMap> resultMaps) {
mappedStatement.resultMaps = resultMaps;
for (ResultMap resultMap : resultMaps) {
mappedStatement.hasNestedResultMaps = mappedStatement.hasNestedResultMaps || resultMap.hasNestedResultMaps();
}
return this;
}
public Builder fetchSize(Integer fetchSize) {
mappedStatement.fetchSize = fetchSize;
return this;
}
public Builder timeout(Integer timeout) {
mappedStatement.timeout = timeout;
return this;
}
public Builder statementType(StatementType statementType) {
mappedStatement.statementType = statementType;
return this;
}
public Builder resultSetType(ResultSetType resultSetType) {
mappedStatement.resultSetType = resultSetType == null ? ResultSetType.DEFAULT : resultSetType;
return this;
}
public Builder cache(Cache cache) {
mappedStatement.cache = cache;
return this;
}
public Builder flushCacheRequired(boolean flushCacheRequired) {
mappedStatement.flushCacheRequired = flushCacheRequired;
return this;
}
public Builder useCache(boolean useCache) {
mappedStatement.useCache = useCache;
return this;
}
public Builder resultOrdered(boolean resultOrdered) {
mappedStatement.resultOrdered = resultOrdered;
return this;
}
public Builder keyGenerator(KeyGenerator keyGenerator) {
mappedStatement.keyGenerator = keyGenerator;
return this;
}
public Builder keyProperty(String keyProperty) {
mappedStatement.keyProperties = delimitedStringToArray(keyProperty);
return this;
}
public Builder keyColumn(String keyColumn) {
mappedStatement.keyColumns = delimitedStringToArray(keyColumn);
return this;
}
public Builder databaseId(String databaseId) {
mappedStatement.databaseId = databaseId;
return this;
}
public Builder lang(LanguageDriver driver) {
mappedStatement.lang = driver;
return this;
}
public Builder resultSets(String resultSet) {
mappedStatement.resultSets = delimitedStringToArray(resultSet);
return this;
}
/**
* @deprecated Use {@link #resultSets}
*/
@Deprecated
public Builder resulSets(String resultSet) {
mappedStatement.resultSets = delimitedStringToArray(resultSet);
return this;
}
public MappedStatement build() {
assert mappedStatement.configuration != null;
assert mappedStatement.id != null;
assert mappedStatement.sqlSource != null;
assert mappedStatement.lang != null;
mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);
return mappedStatement;
}
}
public KeyGenerator getKeyGenerator() {
return keyGenerator;
}
public SqlCommandType getSqlCommandType() {
return sqlCommandType;
}
public String getResource() {
return resource;
}
public Configuration getConfiguration() {
return configuration;
}
public String getId() {
return id;
}
public boolean hasNestedResultMaps() {
return hasNestedResultMaps;
}
public Integer getFetchSize() {
return fetchSize;
}
public Integer getTimeout() {
return timeout;
}
public StatementType getStatementType() {
return statementType;
}
public ResultSetType getResultSetType() {
return resultSetType;
}
public SqlSource getSqlSource() {
return sqlSource;
}
public ParameterMap getParameterMap() {
return parameterMap;
}
public List<ResultMap> getResultMaps() {
return resultMaps;
}
public Cache getCache() {
return cache;
}
public boolean isFlushCacheRequired() {
return flushCacheRequired;
}
public boolean isUseCache() {
return useCache;
}
public boolean isResultOrdered() {
return resultOrdered;
}
public String getDatabaseId() {
return databaseId;
}
public String[] getKeyProperties() {
return keyProperties;
}
public String[] getKeyColumns() {
return keyColumns;
}
public Log getStatementLog() {
return statementLog;
}
public LanguageDriver getLang() {
return lang;
}
public String[] getResultSets() {
return resultSets;
}
/**
* @deprecated Use {@link #getResultSets()}
*/
@Deprecated
public String[] getResulSets() {
return resultSets;
}
public BoundSql getBoundSql(Object parameterObject) {
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings == null || parameterMappings.isEmpty()) {
boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
}
// check for nested result maps in parameter mappings (issue #30)
for (ParameterMapping pm : boundSql.getParameterMappings()) {
String rmId = pm.getResultMapId();
if (rmId != null) {
ResultMap rm = configuration.getResultMap(rmId);
if (rm != null) {
hasNestedResultMaps |= rm.hasNestedResultMaps();
}
}
}
return boundSql;
}
private static String[] delimitedStringToArray(String in) {
if (in == null || in.trim().length() == 0) {
return null;
} else {
return in.split(",");
}
}
}
~~~
- 1.annotations-注解相关
- Arg
- AutomapConstructor
- CacheNamespace
- CacheNamespaceRef
- Case
- ConstructorArgs
- Delete
- DeleteProvider
- Flush
- Insert
- InsertProvider
- Lang
- Many
- MapKey
- Mapper
- One
- Options
- Param
- Property
- Result
- ResultMap
- Results
- ResultType
- Select
- SelectKey
- SelectProvider
- TypeDiscriminator
- Update
- UpdateProvider
- 2.binding-动态代理相关
- BindingException
- MapperMethod
- MapperProxy
- MapperProxyFactory
- MapperRegistry
- 3.builder-sql构建
- annotation
- MapperAnnotationBuilder
- MethodResolver
- ProviderContext
- ProviderMethodResolver
- ProviderSqlSource
- xml
- XMLConfigBuilder
- XMLIncludeTransformer
- XMLMapperBuilder
- XMLMapperEntityResolver
- XMLStatementBuilder
- BaseBuilder
- BuilderException
- CacheRefResolver
- IncompleteElementException
- InitializingObject
- MapperBuilderAssistant
- ParameterExpression
- ResultMapResolver
- SqlSourceBuilder
- StaticSqlSource
- 4.cache-缓存相关
- decorators
- BlockingCache
- FifoCache
- LoggingCache
- LruCache
- ScheduledCache
- SerializedCache
- SoftCache
- SynchronizedCache
- TransactionalCache
- WeakCache
- impl
- PerpetualCache
- Cache
- CacheException
- CacheKey
- TransactionalCacheManager
- 5.cursor-结果集
- defaults
- DefaultCursor
- Cursor
- 6.datasource-数据源
- 7.exceptions-异常处理
- 8.executor-入参出参sql执行
- 9.io-资源加载
- 10.javassist-java助手工具
- 11.jdbc-jdbc相关
- AbstractSQL
- Null
- RuntimeSqlException
- ScriptRunner
- SQL
- SqlRunner
- 12.lang-jdk版本
- UsesJava7
- UsesJava8
- 13.logging-日志处理
- 14.mapping-映射处理
- BoundSql
- CacheBuilder
- DatabaseIdProvider
- Discriminator
- Environment
- FetchType
- MappedStatement
- ParameterMap
- ParameterMapping
- ParameterMode
- ResultFlag
- ResultMap
- ResultMapping
- ResultSetType
- SqlCommandType
- SqlSource
- StatementType
- VendorDatabaseIdProvider
- 15.ognl-#%$符号处理
- 16.parsing-解析相关
- GenericTokenParser
- ParsingException
- PropertyParser
- TokenHandler
- XNode
- XPathParser
- 17.plugin-插件相关
- Interceptor
- InterceptorChain
- Intercepts
- Invocation
- Plugin
- PluginException
- Signature
- 18.reflection-反射处理
- 19.scripting-动态Sql相关
- defaults
- DefaultParameterHandler
- RawLanguageDriver
- RawSqlSource
- xmltags
- ChooseSqlNode
- DynamicContext
- DynamicSqlSource
- ExpressionEvaluator
- ForEachSqlNode
- IfSqlNode
- MixedSqlNode
- OgnlCache
- OgnlClassResolver
- OgnlMemberAccess
- SetSqlNode
- SqlNode
- StaticTextSqlNode
- TextSqlNode
- TrimSqlNode
- VarDeclSqlNode
- WhereSqlNode
- XMLLanguageDriver
- XMLScriptBuilder
- LanguageDriver
- LanguageDriverRegistry
- ScriptingException
- 20.session-会话工具
- defaults
- DefaultSqlSession
- DefaultSqlSessionFactory
- AutoMappingBehavior
- AutoMappingUnknownColumnBehavior
- Configuration
- ExecutorType
- LocalCacheScope
- ResultContext
- ResultHandler
- RowBounds
- SqlSession
- SqlSessionException
- SqlSessionFactory
- SqlSessionFactoryBuilder
- SqlSessionManager
- TransactionIsolationLevel
- 21.transaction-事务相关
- jdbc
- JdbcTransaction
- JdbcTransactionFactory
- managed
- ManagedTransaction
- ManagedTransactionFactory
- Transaction
- TransactionException
- TransactionFactory
- 22.type-类型转换