1. package org.apache.ibatis.session;
  2. /**
  3. * @author Clinton Begin
  4. */
  5. public class Configuration {
  6. protected Environment environment;
  7. protected boolean safeRowBoundsEnabled;
  8. protected boolean safeResultHandlerEnabled = true;
  9. protected boolean mapUnderscoreToCamelCase;
  10. protected boolean aggressiveLazyLoading;
  11. protected boolean multipleResultSetsEnabled = true;
  12. protected boolean useGeneratedKeys;
  13. protected boolean useColumnLabel = true;
  14. protected boolean cacheEnabled = true;
  15. protected boolean callSettersOnNulls;
  16. protected boolean useActualParamName = true;
  17. protected boolean returnInstanceForEmptyRow;
  18. protected boolean shrinkWhitespacesInSql;
  19. protected boolean nullableOnForEach;
  20. protected boolean argNameBasedConstructorAutoMapping;
  21. protected String logPrefix;
  22. protected Class<? extends Log> logImpl;
  23. protected Class<? extends VFS> vfsImpl;
  24. protected Class<?> defaultSqlProviderType;
  25. protected LocalCacheScope localCacheScope = LocalCacheScope.SESSION;
  26. protected JdbcType jdbcTypeForNull = JdbcType.OTHER;
  27. protected Set<String> lazyLoadTriggerMethods = new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString"));
  28. protected Integer defaultStatementTimeout;
  29. protected Integer defaultFetchSize;
  30. protected ResultSetType defaultResultSetType;
  31. protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
  32. protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;
  33. protected AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior = AutoMappingUnknownColumnBehavior.NONE;
  34. protected Properties variables = new Properties();
  35. protected ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
  36. protected ObjectFactory objectFactory = new DefaultObjectFactory();
  37. protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
  38. protected boolean lazyLoadingEnabled = false;
  39. protected ProxyFactory proxyFactory = new JavassistProxyFactory(); // #224 Using internal Javassist instead of OGNL
  40. protected String databaseId;
  41. /**
  42. * Configuration factory class.
  43. * Used to create Configuration for loading deserialized unread properties.
  44. *
  45. * @see <a href='https://github.com/mybatis/old-google-code-issues/issues/300'>Issue 300 (google code)</a>
  46. */
  47. protected Class<?> configurationFactory;
  48. protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
  49. protected final InterceptorChain interceptorChain = new InterceptorChain();
  50. protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry(this);
  51. protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
  52. protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry();
  53. protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection")
  54. .conflictMessageProducer((savedValue, targetValue) ->
  55. ". please check " + savedValue.getResource() + " and " + targetValue.getResource());
  56. protected final Map<String, Cache> caches = new StrictMap<>("Caches collection");
  57. protected final Map<String, ResultMap> resultMaps = new StrictMap<>("Result Maps collection");
  58. protected final Map<String, ParameterMap> parameterMaps = new StrictMap<>("Parameter Maps collection");
  59. protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<>("Key Generators collection");
  60. protected final Set<String> loadedResources = new HashSet<>();
  61. protected final Map<String, XNode> sqlFragments = new StrictMap<>("XML fragments parsed from previous mappers");
  62. protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<>();
  63. protected final Collection<CacheRefResolver> incompleteCacheRefs = new LinkedList<>();
  64. protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<>();
  65. protected final Collection<MethodResolver> incompleteMethods = new LinkedList<>();
  66. /*
  67. * A map holds cache-ref relationship. The key is the namespace that
  68. * references a cache bound to another namespace and the value is the
  69. * namespace which the actual cache is bound to.
  70. */
  71. protected final Map<String, String> cacheRefMap = new HashMap<>();
  72. public Configuration(Environment environment) {
  73. this();
  74. this.environment = environment;
  75. }
  76. public Configuration() {
  77. typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
  78. typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
  79. typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class);
  80. typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class);
  81. typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class);
  82. typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class);
  83. typeAliasRegistry.registerAlias("FIFO", FifoCache.class);
  84. typeAliasRegistry.registerAlias("LRU", LruCache.class);
  85. typeAliasRegistry.registerAlias("SOFT", SoftCache.class);
  86. typeAliasRegistry.registerAlias("WEAK", WeakCache.class);
  87. typeAliasRegistry.registerAlias("DB_VENDOR", VendorDatabaseIdProvider.class);
  88. typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class);
  89. typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class);
  90. typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class);
  91. typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class);
  92. typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class);
  93. typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class);
  94. typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class);
  95. typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class);
  96. typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class);
  97. typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class);
  98. typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class);
  99. languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class);
  100. languageRegistry.register(RawLanguageDriver.class);
  101. }
  102. ...
  103. ...
  104. ...
  105. public Executor newExecutor(Transaction transaction) {
  106. return newExecutor(transaction, defaultExecutorType);
  107. }
  108. public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
  109. executorType = executorType == null ? defaultExecutorType : executorType;
  110. Executor executor;
  111. if (ExecutorType.BATCH == executorType) {
  112. executor = new BatchExecutor(this, transaction);
  113. } else if (ExecutorType.REUSE == executorType) {
  114. executor = new ReuseExecutor(this, transaction);
  115. } else {
  116. executor = new SimpleExecutor(this, transaction);
  117. }
  118. if (cacheEnabled) {
  119. executor = new CachingExecutor(executor);
  120. }
  121. executor = (Executor) interceptorChain.pluginAll(executor);
  122. return executor;
  123. }
  124. ...
  125. ...
  126. ...
  127. }

CustomizedSettingsMapperConfig.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!--
  3. Copyright 2009-2021 the original author or authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. -->
  14. <configuration xmlns="http://mybatis.org/schema/mybatis-config"
  15. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16. xsi:schemaLocation="http://mybatis.org/schema/mybatis-config http://mybatis.org/schema/mybatis-config.xsd">
  17. <properties resource="org/apache/ibatis/builder/jdbc.properties">
  18. <property name="prop1" value="aaaa"/>
  19. <property name="jdbcTypeForNull" value="NULL" />
  20. </properties>
  21. <settings>
  22. <setting name="autoMappingBehavior" value="NONE"/>
  23. <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
  24. <setting name="cacheEnabled" value="false"/>
  25. <setting name="proxyFactory" value="CGLIB"/>
  26. <setting name="lazyLoadingEnabled" value="true"/>
  27. <setting name="aggressiveLazyLoading" value="true"/>
  28. <setting name="multipleResultSetsEnabled" value="false"/>
  29. <setting name="useColumnLabel" value="false"/>
  30. <setting name="useGeneratedKeys" value="true"/>
  31. <setting name="defaultExecutorType" value="BATCH"/>
  32. <setting name="defaultStatementTimeout" value="10"/>
  33. <setting name="defaultFetchSize" value="100"/>
  34. <setting name="mapUnderscoreToCamelCase" value="true"/>
  35. <setting name="safeRowBoundsEnabled" value="true"/>
  36. <setting name="localCacheScope" value="STATEMENT"/>
  37. <setting name="jdbcTypeForNull" value="${jdbcTypeForNull}"/>
  38. <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString,xxx"/>
  39. <setting name="safeResultHandlerEnabled" value="false"/>
  40. <setting name="defaultScriptingLanguage" value="org.apache.ibatis.scripting.defaults.RawLanguageDriver"/>
  41. <setting name="callSettersOnNulls" value="true"/>
  42. <setting name="logPrefix" value="mybatis_"/>
  43. <setting name="logImpl" value="SLF4J"/>
  44. <setting name="vfsImpl" value="org.apache.ibatis.io.JBoss6VFS"/>
  45. <setting name="configurationFactory" value="java.lang.String"/>
  46. <setting name="shrinkWhitespacesInSql" value="true"/>
  47. <setting name="argNameBasedConstructorAutoMapping" value="true"/>
  48. </settings>
  49. <typeAliases>
  50. <typeAlias alias="BlogAuthor" type="org.apache.ibatis.domain.blog.Author"/>
  51. <typeAlias type="org.apache.ibatis.domain.blog.Blog"/>
  52. <typeAlias type="org.apache.ibatis.domain.blog.Post"/>
  53. <package name="org.apache.ibatis.domain.jpetstore"/>
  54. </typeAliases>
  55. <typeHandlers>
  56. <typeHandler javaType="String" handler="org.apache.ibatis.builder.CustomStringTypeHandler"/>
  57. <typeHandler javaType="String" jdbcType="VARCHAR" handler="org.apache.ibatis.builder.CustomStringTypeHandler"/>
  58. <typeHandler handler="org.apache.ibatis.builder.CustomLongTypeHandler"/>
  59. <package name="org.apache.ibatis.builder.typehandler"/>
  60. </typeHandlers>
  61. <objectFactory type="org.apache.ibatis.builder.ExampleObjectFactory">
  62. <property name="objectFactoryProperty" value="100"/>
  63. </objectFactory>
  64. <objectWrapperFactory type="org.apache.ibatis.builder.CustomObjectWrapperFactory" />
  65. <reflectorFactory type="org.apache.ibatis.builder.CustomReflectorFactory"/>
  66. <plugins>
  67. <plugin interceptor="org.apache.ibatis.builder.ExamplePlugin">
  68. <property name="pluginProperty" value="100"/>
  69. </plugin>
  70. </plugins>
  71. 默认环境,default设置是默认的环境,可以设置为development或者dev01
  72. id:数据库唯一标识不能重复
  73. <environments default="development">
  74. <environment id="development">
  75. <transactionManager type="JDBC">
  76. <property name="" value=""/>
  77. </transactionManager>
  78. <dataSource type="UNPOOLED">// 不带连接池
  79. <property name="driver" value="${driver}"/>
  80. <property name="url" value="${url}"/>
  81. <property name="username" value="${username}"/>
  82. <property name="password" value="${password}"/>
  83. </dataSource>
  84. </environment>
  85. <environment id="dev01">
  86. <transactionManager type="MANAGED">
  87. <property name="" value=""/>
  88. </transactionManager>
  89. <dataSource type="POOLED"> // 带有连接池
  90. <property name="driver" value="${driver}"/>
  91. <property name="url" value="${url}"/>
  92. <property name="username" value="${username}"/>
  93. <property name="password" value="${password}"/>
  94. </dataSource>
  95. </environment>
  96. <environment id="dev02">
  97. <transactionManager type="MANAGED">
  98. <property name="" value=""/>
  99. </transactionManager>
  100. <dataSource type="JNDI"> // 使用上下文的连接
  101. <property name="driver" value="${driver}"/>
  102. <property name="url" value="${url}"/>
  103. <property name="username" value="${username}"/>
  104. <property name="password" value="${password}"/>
  105. </dataSource>
  106. </environment>
  107. </environments>
  108. <databaseIdProvider type="DB_VENDOR">
  109. <property name="Apache Derby" value="derby"/>
  110. </databaseIdProvider>
  111. <mappers>
  112. <mapper resource="org/apache/ibatis/builder/xsd/BlogMapper.xml"/>
  113. <mapper url="file:./src/test/java/org/apache/ibatis/builder/xsd/NestedBlogMapper.xml"/>
  114. <mapper class="org.apache.ibatis.builder.xsd.CachedAuthorMapper"/>
  115. <package name="org.apache.ibatis.builder.mapper"/>
  116. </mappers>
  117. </configuration>