目錄
建造者模式
建造者模式(Builder Pattern)指的是將一個複雜的構建與其表示相分離,使得同樣的構建過程可以創建不同的表示。它使用多個簡單的對象一步一步構建成一個複雜的對象。這種類型的設計模式屬於創建型模式,它提供了一種創建對象的最佳方式。
結構
角色
- Product:要創建的複雜對象
- Builder:給出一個抽象接口,以規範產品對象的各個組成成分的建造。這個接口規定要實現複雜對象的哪些部分的創建,並不涉及具體的對象部件的創建
- ConcreteBuilder:實現 Builder 接口,針對不同的商業邏輯,具體化複雜對象的各部分的創建。 在建造過程完成後,提供產品的實例
- Director:調用具體建造者來創建複雜對象的各個部分,在指導者中不涉及具體產品的信息,只負責保證對象各部分完整創建或按某種順序創建
使用場景
- 需要生成的對象具有複雜的內部結構,實例化對象時要屏蔽掉對象內部的細節,讓上層代碼與複雜對象的實例化過程解耦,可以使用建造者模式;簡而言之,如果“遇到多個構造器參數時要考慮用構建器”
- 對象的實例化是依賴各個組件的產生以及裝配順序,關注的是一步一步地組裝出目標對象,可以使用建造器模式
與工廠模式的區別
建造者模式在Mybatis裏的應用
Mybatis 的初始化
- Configuration : Mybatis 啓動初始化的核心就是將所有 xml 配置文件信息加載到 Configuration 對象中, Configuration是單例的,生命週期是應用級的。
- XMLConfigBuilder: 主要負責解析 mybatis-config.xml
- XMLMapperBuilder: 主要負責解析映射配置文件(各個Mapper.xml)
- XMLStatementBuilder: 主要負責解析映射配置文件中的SQL節點
- MapperBuilderAssistant:輔助XMLMapperBuilder解析mapper.xml文件,完善屬性信息,並註冊到configuration對象(單一職責原則)
- BaseBuilder:所有解析器的父類,包含配置文件實例,爲解析文件提供的一些通用的方法
入口
當我們創建了一個 SqlSessionFactory,其實就完成了初始化,所以入口是 build 方法。
private SqlSessionFactory sqlSessionFactory;
//讀取mybatis配置文件創SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//讀取mybatis配置文件創SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
inputStream.close();
跟 build:
這就是一個建造者模式的簡單實現。屏蔽了創建對象的複雜過程,但並沒有流式編程的思想。並不是建造者模式的最佳實現。
public class SqlSessionFactoryBuilder {
public SqlSessionFactory build(InputStream inputStream) {
return build(inputStream, null, null);
}
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
//省略了一些代碼
XMLConfigBuilder
public class XMLConfigBuilder extends BaseBuilder {
//是否解析過mybatis-config.xml文件
private boolean parsed;
//xml文件的解析器
private final XPathParser parser;
//讀取默認的environment
private String environment;
//負責創建和緩存Reflector對象
private final ReflectorFactory localReflectorFactory = new DefaultReflectorFactory();
public Configuration parse() {
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
//解析<properties>節點
propertiesElement(root.evalNode("properties"));
//解析<settings>節點
Properties settings = settingsAsProperties(root.evalNode("settings"));
loadCustomVfs(settings);
//解析<typeAliases>節點
typeAliasesElement(root.evalNode("typeAliases"));
//解析<plugins>節點
pluginElement(root.evalNode("plugins"));
//解析<objectFactory>節點
objectFactoryElement(root.evalNode("objectFactory"));
//解析<objectWrapperFactory>節點
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析<reflectorFactory>節點
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);//將settings填充到configuration
// read it after objectFactory and objectWrapperFactory issue #631
//解析<environments>節點
environmentsElement(root.evalNode("environments"));
//解析<databaseIdProvider>節點
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
//解析<typeHandlers>節點
typeHandlerElement(root.evalNode("typeHandlers"));
//解析<mappers>節點
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
private void mapperElement(XNode parent) throws Exception {
if (parent != null) {
for (XNode child : parent.getChildren()) {//處理mapper子節點
if ("package".equals(child.getName())) {//package子節點
String mapperPackage = child.getStringAttribute("name");
configuration.addMappers(mapperPackage);
} else {//獲取<mapper>節點的resource、url或mapperClass屬性這三個屬性互斥
String resource = child.getStringAttribute("resource");
String url = child.getStringAttribute("url");
String mapperClass = child.getStringAttribute("class");
if (resource != null && url == null && mapperClass == null) {//如果resource不爲空
ErrorContext.instance().resource(resource);
InputStream inputStream = Resources.getResourceAsStream(resource);//加載mapper文件
//實例化XMLMapperBuilder解析mapper映射文件
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
mapperParser.parse();
} else if (resource == null && url != null && mapperClass == null) {//如果url不爲空
ErrorContext.instance().resource(url);
InputStream inputStream = Resources.getUrlAsStream(url);//加載mapper文件
//實例化XMLMapperBuilder解析mapper映射文件
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
mapperParser.parse();
} else if (resource == null && url == null && mapperClass != null) {//如果class不爲空
Class<?> mapperInterface = Resources.classForName(mapperClass);//加載class對象
configuration.addMapper(mapperInterface);//向代理中心註冊mapper
} else {
throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
}
}
}
}
}
//省略了一些方法
}
XMLMapperBuilder
public class XMLMapperBuilder extends BaseBuilder {
private final XPathParser parser;
private final MapperBuilderAssistant builderAssistant;
private final Map<String, XNode> sqlFragments;
private final String resource;
public void parse() {
//判斷是否已經加載該配置文件
if (!configuration.isResourceLoaded(resource)) {
configurationElement(parser.evalNode("/mapper"));//處理mapper節點
configuration.addLoadedResource(resource);//將mapper文件添加到configuration.loadedResources中
bindMapperForNamespace();//註冊mapper接口
}
//處理解析失敗的ResultMap節點
parsePendingResultMaps();
//處理解析失敗的CacheRef節點
parsePendingCacheRefs();
//處理解析失敗的Sql語句節點
parsePendingStatements();
}
private void configurationElement(XNode context) {
try {
//獲取mapper節點的namespace屬性
String namespace = context.getStringAttribute("namespace");
if (namespace == null || namespace.equals("")) {
throw new BuilderException("Mapper's namespace cannot be empty");
}
//設置builderAssistant的namespace屬性
builderAssistant.setCurrentNamespace(namespace);
//解析cache-ref節點
cacheRefElement(context.evalNode("cache-ref"));
//重點分析 :解析cache節點----------------1-------------------
cacheElement(context.evalNode("cache"));
//解析parameterMap節點(已廢棄)
parameterMapElement(context.evalNodes("/mapper/parameterMap"));
//重點分析 :解析resultMap節點(基於數據結果去理解)----------------2-------------------
resultMapElements(context.evalNodes("/mapper/resultMap"));
//解析sql節點
sqlElement(context.evalNodes("/mapper/sql"));
//重點分析 :解析select、insert、update、delete節點 ----------------3-------------------
buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
} catch (Exception e) {
throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
}
}
//重點分析 :解析cache節點----------------1-------------------
private void cacheElement(XNode context) throws Exception {
if (context != null) {
//獲取cache節點的type屬性,默認爲PERPETUAL
String type = context.getStringAttribute("type", "PERPETUAL");
//找到type對應的cache接口的實現
Class<? extends Cache> typeClass = typeAliasRegistry.resolveAlias(type);
//讀取eviction屬性,既緩存的淘汰策略,默認LRU
String eviction = context.getStringAttribute("eviction", "LRU");
//根據eviction屬性,找到裝飾器
Class<? extends Cache> evictionClass = typeAliasRegistry.resolveAlias(eviction);
//讀取flushInterval屬性,既緩存的刷新週期
Long flushInterval = context.getLongAttribute("flushInterval");
//讀取size屬性,既緩存的容量大小
Integer size = context.getIntAttribute("size");
//讀取readOnly屬性,既緩存的是否只讀
boolean readWrite = !context.getBooleanAttribute("readOnly", false);
//讀取blocking屬性,既緩存的是否阻塞
boolean blocking = context.getBooleanAttribute("blocking", false);
Properties props = context.getChildrenAsProperties();
//通過builderAssistant創建緩存對象,並添加至configuration
builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);
}
}
//重點分析 :解析resultMap節點(基於數據結果去理解)----------------2-------------------
//解析resultMap節點,實際就是解析sql查詢的字段與pojo屬性之間的轉化規則
private void resultMapElements(List<XNode> list) throws Exception {
//遍歷所有的resultmap節點
for (XNode resultMapNode : list) {
try {
//解析具體某一個resultMap節點
resultMapElement(resultMapNode);
} catch (IncompleteElementException e) {
// ignore, it will be retried
}
}
}
private ResultMap resultMapElement(XNode resultMapNode) throws Exception {
return resultMapElement(resultMapNode, Collections.<ResultMapping> emptyList());
}
private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception {
ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier());
//獲取resultmap節點的id屬性
String id = resultMapNode.getStringAttribute("id",
resultMapNode.getValueBasedIdentifier());
//獲取resultmap節點的type屬性
String type = resultMapNode.getStringAttribute("type",
resultMapNode.getStringAttribute("ofType",
resultMapNode.getStringAttribute("resultType",
resultMapNode.getStringAttribute("javaType"))));
//獲取resultmap節點的extends屬性,描述繼承關係
String extend = resultMapNode.getStringAttribute("extends");
//獲取resultmap節點的autoMapping屬性,是否開啓自動映射
Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping");
//從別名註冊中心獲取entity的class對象
Class<?> typeClass = resolveClass(type);
Discriminator discriminator = null;
//記錄子節點中的映射結果集合
List<ResultMapping> resultMappings = new ArrayList<>();
resultMappings.addAll(additionalResultMappings);
//從xml文件中獲取當前resultmap中的所有子節點,並開始遍歷
List<XNode> resultChildren = resultMapNode.getChildren();
for (XNode resultChild : resultChildren) {
if ("constructor".equals(resultChild.getName())) {//處理<constructor>節點
processConstructorElement(resultChild, typeClass, resultMappings);
} else if ("discriminator".equals(resultChild.getName())) {//處理<discriminator>節點
discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
} else {//處理<id> <result> <association> <collection>節點
List<ResultFlag> flags = new ArrayList<>();
if ("id".equals(resultChild.getName())) {
flags.add(ResultFlag.ID);//如果是id節點,向flags中添加元素
}
//重點看:創建ResultMapping對象並加入resultMappings集合中
resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
}
}
//實例化resultMap解析器
ResultMapResolver resultMapResolver = new ResultMapResolver(builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping);
try {
//通過resultMap解析器實例化resultMap並將其註冊到configuration對象(也是交給MapperBuilderAssistant去做)
//return assistant.addResultMap(this.id, this.type, this.extend, this.discriminator, this.resultMappings, this.autoMapping);
return resultMapResolver.resolve();
} catch (IncompleteElementException e) {
configuration.addIncompleteResultMap(resultMapResolver);
throw e;
}
}
//根據resultmap中的子節點信息,創建resultMapping對象
private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resultType, List<ResultFlag> flags) throws Exception {
String property;
if (flags.contains(ResultFlag.CONSTRUCTOR)) {
property = context.getStringAttribute("name");
} else {
property = context.getStringAttribute("property");
}
String column = context.getStringAttribute("column");
String javaType = context.getStringAttribute("javaType");
String jdbcType = context.getStringAttribute("jdbcType");
String nestedSelect = context.getStringAttribute("select");
String nestedResultMap = context.getStringAttribute("resultMap",
processNestedResultMappings(context, Collections.<ResultMapping> emptyList()));
String notNullColumn = context.getStringAttribute("notNullColumn");
String columnPrefix = context.getStringAttribute("columnPrefix");
String typeHandler = context.getStringAttribute("typeHandler");
String resultSet = context.getStringAttribute("resultSet");
String foreignColumn = context.getStringAttribute("foreignColumn");
boolean lazy = "lazy".equals(context.getStringAttribute("fetchType", configuration.isLazyLoadingEnabled() ? "lazy" : "eager"));
Class<?> javaTypeClass = resolveClass(javaType);
@SuppressWarnings("unchecked")
Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
//使用建造者模式創建resultMapping對象
return builderAssistant.buildResultMapping(resultType, property, column, javaTypeClass, jdbcTypeEnum, nestedSelect, nestedResultMap, notNullColumn, columnPrefix, typeHandlerClass, flags, resultSet, foreignColumn, lazy);
}
//重點分析 :解析select、insert、update、delete節點 ----------------3-------------------
private void buildStatementFromContext(List<XNode> list) {
if (configuration.getDatabaseId() != null) {
buildStatementFromContext(list, configuration.getDatabaseId());
}
buildStatementFromContext(list, null);
}
//處理所有的sql語句節點並註冊至configuration對象
private void buildStatementFromContext(List<XNode> list, String requiredDatabaseId) {
for (XNode context : list) {
//創建XMLStatementBuilder 專門用於解析sql語句節點
final XMLStatementBuilder statementParser = new XMLStatementBuilder(configuration, builderAssistant, context, requiredDatabaseId);
try {
//解析sql語句節點
statementParser.parseStatementNode();
} catch (IncompleteElementException e) {
configuration.addIncompleteStatement(statementParser);
}
}
}
//省略了一些方法
}
XMLStatementBuilder
public class XMLStatementBuilder extends BaseBuilder {
private final MapperBuilderAssistant builderAssistant;
private final XNode context;
private final String requiredDatabaseId;
public void parseStatementNode() {
//獲取sql節點的id
String id = context.getStringAttribute("id");
String databaseId = context.getStringAttribute("databaseId");
if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {
return;
}
/*獲取sql節點的各種屬性*/
Integer fetchSize = context.getIntAttribute("fetchSize");
Integer timeout = context.getIntAttribute("timeout");
String parameterMap = context.getStringAttribute("parameterMap");
String parameterType = context.getStringAttribute("parameterType");
Class<?> parameterTypeClass = resolveClass(parameterType);
String resultMap = context.getStringAttribute("resultMap");
String resultType = context.getStringAttribute("resultType");
String lang = context.getStringAttribute("lang");
LanguageDriver langDriver = getLanguageDriver(lang);
Class<?> resultTypeClass = resolveClass(resultType);
String resultSetType = context.getStringAttribute("resultSetType");
StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType", StatementType.PREPARED.toString()));
ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
//根據sql節點的名稱獲取SqlCommandType(INSERT, UPDATE, DELETE, SELECT)
String nodeName = context.getNode().getNodeName();
SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
boolean useCache = context.getBooleanAttribute("useCache", isSelect);
boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false);
// Include Fragments before parsing
//在解析sql語句之前先解析<include>節點
XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant);
includeParser.applyIncludes(context.getNode());
// Parse selectKey after includes and remove them.
//在解析sql語句之前,處理<selectKey>子節點,並在xml節點中刪除
processSelectKeyNodes(id, parameterTypeClass, langDriver);
// Parse the SQL (pre: <selectKey> and <include> were parsed and removed)
//解析sql語句是解析mapper.xml的核心,實例化sqlSource,使用sqlSource封裝sql語句
SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
String resultSets = context.getStringAttribute("resultSets");//獲取resultSets屬性
String keyProperty = context.getStringAttribute("keyProperty");//獲取主鍵信息keyProperty
String keyColumn = context.getStringAttribute("keyColumn");///獲取主鍵信息keyColumn
//根據<selectKey>獲取對應的SelectKeyGenerator的id
KeyGenerator keyGenerator;
String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX;
keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true);
//獲取keyGenerator對象,如果是insert類型的sql語句,會使用KeyGenerator接口獲取數據庫生產的id;
if (configuration.hasKeyGenerator(keyStatementId)) {
keyGenerator = configuration.getKeyGenerator(keyStatementId);
} else {
keyGenerator = context.getBooleanAttribute("useGeneratedKeys",
configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType))
? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
}
//通過builderAssistant實例化MappedStatement,並註冊至configuration對象
builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
resultSetTypeEnum, flushCache, useCache, resultOrdered,
keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
}
//省略了一些方法
}
MapperBuilderAssistant
public class MapperBuilderAssistant extends BaseBuilder {
private String currentNamespace;
private final String resource;
private Cache currentCache;
private boolean unresolvedCacheRef; // issue #676
//通過builderAssistant創建緩存對象,並添加至configuration
public Cache useNewCache(Class<? extends Cache> typeClass,
Class<? extends Cache> evictionClass,
Long flushInterval,
Integer size,
boolean readWrite,
boolean blocking,
Properties props) {
//經典的建造起模式,創建一個cache對象
Cache cache = new CacheBuilder(currentNamespace)
.implementation(valueOrDefault(typeClass, PerpetualCache.class))
.addDecorator(valueOrDefault(evictionClass, LruCache.class))
.clearInterval(flushInterval)
.size(size)
.readWrite(readWrite)
.blocking(blocking)
.properties(props)
.build();
//將緩存添加至configuration,注意二級緩存以命名空間爲單位進行劃分
configuration.addCache(cache);
currentCache = cache;
return cache;
}
public ResultMapping buildResultMapping(
Class<?> resultType,
String property,
String column,
Class<?> javaType,
JdbcType jdbcType,
String nestedSelect,
String nestedResultMap,
String notNullColumn,
String columnPrefix,
Class<? extends TypeHandler<?>> typeHandler,
List<ResultFlag> flags,
String resultSet,
String foreignColumn,
boolean lazy) {
Class<?> javaTypeClass = resolveResultJavaType(resultType, property, javaType);
TypeHandler<?> typeHandlerInstance = resolveTypeHandler(javaTypeClass, typeHandler);
List<ResultMapping> composites = parseCompositeColumnName(column);
return new ResultMapping.Builder(configuration, property, column, javaTypeClass)
.jdbcType(jdbcType)
.nestedQueryId(applyCurrentNamespace(nestedSelect, true))
.nestedResultMapId(applyCurrentNamespace(nestedResultMap, true))
.resultSet(resultSet)
.typeHandler(typeHandlerInstance)
.flags(flags == null ? new ArrayList<ResultFlag>() : flags)
.composites(composites)
.notNullColumns(parseMultipleColumnNames(notNullColumn))
.columnPrefix(columnPrefix)
.foreignColumn(foreignColumn)
.lazy(lazy)
.build();
}
//實例化ResultMap並將其註冊到Configuration對象
public ResultMap addResultMap(
String id,
Class<?> type,
String extend,
Discriminator discriminator,
List<ResultMapping> resultMappings,
Boolean autoMapping) {
//完善id,id的完整格式是"namespace.id"
id = applyCurrentNamespace(id, false);
//獲得父類resultMap的完整id
extend = applyCurrentNamespace(extend, true);
//針對extend屬性的處理
if (extend != null) {
if (!configuration.hasResultMap(extend)) {
throw new IncompleteElementException("Could not find a parent resultmap with id '" + extend + "'");
}
ResultMap resultMap = configuration.getResultMap(extend);
List<ResultMapping> extendedResultMappings = new ArrayList<>(resultMap.getResultMappings());
extendedResultMappings.removeAll(resultMappings);
// Remove parent constructor if this resultMap declares a constructor.
boolean declaresConstructor = false;
for (ResultMapping resultMapping : resultMappings) {
if (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) {
declaresConstructor = true;
break;
}
}
if (declaresConstructor) {
Iterator<ResultMapping> extendedResultMappingsIter = extendedResultMappings.iterator();
while (extendedResultMappingsIter.hasNext()) {
if (extendedResultMappingsIter.next().getFlags().contains(ResultFlag.CONSTRUCTOR)) {
extendedResultMappingsIter.remove();
}
}
}
//添加需要被繼承下來的resultMapping對象結合
resultMappings.addAll(extendedResultMappings);
}
//通過建造者模式實例化resultMap,並註冊到configuration.resultMaps中
ResultMap resultMap = new ResultMap.Builder(configuration, id, type, resultMappings, autoMapping)
.discriminator(discriminator)
.build();
configuration.addResultMap(resultMap);
return resultMap;
}
public MappedStatement addMappedStatement(
String id,
SqlSource sqlSource,
StatementType statementType,
SqlCommandType sqlCommandType,
Integer fetchSize,
Integer timeout,
String parameterMap,
Class<?> parameterType,
String resultMap,
Class<?> resultType,
ResultSetType resultSetType,
boolean flushCache,
boolean useCache,
boolean resultOrdered,
KeyGenerator keyGenerator,
String keyProperty,
String keyColumn,
String databaseId,
LanguageDriver lang,
String resultSets) {
if (unresolvedCacheRef) {
throw new IncompleteElementException("Cache-ref not yet resolved");
}
id = applyCurrentNamespace(id, false);
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType)
.resource(resource)
.fetchSize(fetchSize)
.timeout(timeout)
.statementType(statementType)
.keyGenerator(keyGenerator)
.keyProperty(keyProperty)
.keyColumn(keyColumn)
.databaseId(databaseId)
.lang(lang)
.resultOrdered(resultOrdered)
.resultSets(resultSets)
.resultMaps(getStatementResultMaps(resultMap, resultType, id))
.resultSetType(resultSetType)
.flushCacheRequired(valueOrDefault(flushCache, !isSelect))
.useCache(valueOrDefault(useCache, isSelect))
.cache(currentCache);
ParameterMap statementParameterMap = getStatementParameterMap(parameterMap, parameterType, id);
if (statementParameterMap != null) {
statementBuilder.parameterMap(statementParameterMap);
}
MappedStatement statement = statementBuilder.build();
configuration.addMappedStatement(statement);
return statement;
}
//省略了一些方法
}