<返回更多

mybatis3 源码深度解析-Configuration 对象深入了解

2022-07-18    javabus
加入收藏
  1. 1是主配置文件 mybatis-config.xml
  2. 2是配置执行 sql 的 MApper文件(接口或者 xml)

Configuaration 类字段注释

public class Configration {
  protected Environment environment;
  // 允许 JDBC 支持自动生成主键,需要驱动兼容。
  // 这就是insert时获取MySQL自增主键/oracle sequence的开关。
  // 注:一般来说,这是希望的结果,应该默认值为true比较合适。
  protected boolean useGeneratedKeys;
  // 是否启用缓存
  protected boolean cacheEnabled = true;
// 指定 MyBatis 增加到日志名称的前缀。
protected String logPrefix;

// 指定 MyBatis 所用日志的具体实现,未指定时将自动查找。一般建议指定为slf4j或log4j
protected Class<? extends Log> logImpl;
// 当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型。 某些驱动需要指定列的 JDBC 类型,多数情况直接用一般类型即可,比如 NULL、VARCHAR 或 OTHER。
protected JdbcType jdbcTypeForNull = JdbcType.OTHER;
// settings下的properties属性
protected Properties variables = new Properties();


// 默认的反射器工厂,用于操作属性、构造器方便
protected ReflectorFactory reflectorFactory = new DefaultReflectorFactory();

// 对象工厂, 所有的类resultMap类都需要依赖于对象工厂来实例化
protected ObjectFactory objectFactory = new DefaultObjectFactory();

// 对象包装器工厂,主要用来在创建非原生对象,比如增加了某些监控或者特殊属性的代理类
protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();

// 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。特定关联关系中可通过设置fetchType属性来覆盖该项的开关状态。
protected boolean lazyLoadingEnabled = false;

// 指定 Mybatis 创建具有延迟加载能力的对象所用到的代理工具。MyBatis 3.3+使用JAVASSIST
protected ProxyFactory proxyFactory = new JavassistProxyFactory(); // #224 Using internal Javassist instead of OGNL
// MyBatis 可以根据不同的数据库厂商执行不同的语句,这种多厂商的支持是基于映射语句中的 databaseId 属性。
protected String databaseId;


/**
 * configuration 存储属性之外的数据: 类型处理 TypeHandler,类型别名typeAlias,Mapper接口
 * 以下字段信息会在启动的时候注册到 configuration 对象
 */
// 存储namespace与MapperProxyFactory<mapper接口> 对应关系    Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();
protected final MIniMapperRegistry mapperRegistry = new MIniMapperRegistry(this);

// mybatis插件列表  List<Interceptor> interceptors = new ArrayList<>();
protected final InterceptorChain interceptorChain = new InterceptorChain();
// 类型注册器,TypeAliasRegistry 构造器会添加默认值
// 用于在执行sql语句的出入参映射以及mybatis-config文件里的各种配置
// 比如<transactionManager type="JDBC"/><dataSource type="POOLED">时使用简写
// Map<String, Class<?>> typeAliases  存储key别名,value 类型
protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
//Java和jdbc类型互相转换
protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry();
protected ResultSetType defaultResultSetType;

//mapper.xml 中每条select|insert|update|delete 语句的映射
protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection")
  .conflictMessageProducer((savedValue, targetValue) ->
                           ". please check " + savedValue.getResource() + " and " + targetValue.getResource());
protected final Map<String, Cache> caches = new StrictMap<>("Caches collection");
//Mapper.xml 通过 <ResultMap> 配置的结果集映射关系信息
protected final Map<String, ResultMap> resultMaps = new StrictMap<>("Result Maps collection");
//Mapper.xml 通过 <ParameterMap> 配置的入参映射关系信息
protected final Map<String, ParameterMap> parameterMaps = new StrictMap<>("Parameter Maps collection");
//KeyGenerator 是mybatis主键生成器 : mybatis提供了 3 中主键生成器 1:jdbc3KeyGenerator(数据库自增主键) 2:SelectKeyGenerator (通过 select 语句查询,如 oracle sequence) 3:noKeyGenerator(无自增主键)
protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<>("Key Generators collection");

// 注册所有 Mapper.xml 的存放路径
protected final Set<String> loadedResources = new HashSet<>();
protected final Map<String, XNode> sqlFragments = new StrictMap<>("XML fragments parsed from previous mappers");

/**
 * 以下用于注册出现解析异常的相关对象
 */
protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<>();
protected final Collection<CacheRefResolver> incompleteCacheRefs = new LinkedList<>();
protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<>();
protected final Collection<MethodResolver> incompleteMethods = new LinkedList<>();

 

构造器注册别名预览

public MiniConfigration() {
  // 内置别名注册
  typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
  typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);

  typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class);
  typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class);
  typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class);

  typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class);
  typeAliasRegistry.registerAlias("FIFO", FifoCache.class);
  typeAliasRegistry.registerAlias("LRU", LruCache.class);
  typeAliasRegistry.registerAlias("SOFT", SoftCache.class);
  typeAliasRegistry.registerAlias("WEAK", WeakCache.class);

  typeAliasRegistry.registerAlias("DB_VENDOR", VendorDatabaseIdProvider.class);

  typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class);
  typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class);

  typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class);
  typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class);
  typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class);
  typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class);
  typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class);
  typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class);
  typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class);

  typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class);
  typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class);

  languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class);
  languageRegistry.register(RawLanguageDriver.class);
}

 

Configuration 作为其他组件的工厂类型,提供相关工厂方法

这些工厂的方法签名如下:

public MetaObject newMetaObject(Object object) {
    return MetaObject.forObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
}

public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
  ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
  parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
  return parameterHandler;
}

public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
                                            ResultHandler resultHandler, BoundSql boundSql) {
  ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
  resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
  return resultSetHandler;
}

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
  StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
  statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
  return statementHandler;
}

public Executor newExecutor(Transaction transaction) {
  //return newExecutor(transaction, defaultExecutorType);
}
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>