Configuration 的重要性

Mybatis 启动初始化的核心就是将所有 xml 配置文件信息加载到 Configuration 对象中, Configuration是单例的,生命周期是应用级的。
重要属性
protected Environment environment;
//下面2个是反射模块要用到的
/*MyBatis每次创建结果对象的新实例时,它都会使用对象工厂(ObjectFactory)去构建POJO*/
protected ObjectFactory objectFactory = new DefaultObjectFactory();
/*ObjectWrapper的工厂类,用于创建ObjectWrapper*/
protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
/*mapper接口的动态代理注册中心*/
protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
/*mapper文件中增删改查操作的注册中心*/
protected final Map<String, MappedStatement> mappedStatements = new StrictMap<>("Mapped Statements collection");
/*mapper文件中配置cache节点的 二级缓存*/
protected final Map<String, Cache> caches = new StrictMap<>("Caches collection");
/*mapper文件中配置的所有resultMap对象 key为命名空间+ID*/
protected final Map<String, ResultMap> resultMaps = new StrictMap<>("Result Maps collection");
protected final Map<String, ParameterMap> parameterMaps = new StrictMap<>("Parameter Maps collection");
/*mapper文件中配置KeyGenerator的insert和update节点,key为命名空间+ID*/
protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<>("Key Generators collection");
/*加载到的所有*mapper.xml文件*/
protected final Set<String> loadedResources = new HashSet<>();
/*mapper文件中配置的sql元素,key为命名空间+ID*/
protected final Map<String, XNode> sqlFragments = new StrictMap<>("XML fragments parsed from previous mappers");

本文深入探讨了MyBatis框架中Configuration对象的关键作用及其实现细节,包括其单例特性、生命周期,以及如何通过Configuration对象加载并管理XML配置文件信息。文章详细列举了Configuration的重要属性,如Environment、ObjectFactory、ObjectWrapperFactory、MapperRegistry等,揭示了这些属性在MyBatis运行过程中的核心地位。
1万+

被折叠的 条评论
为什么被折叠?



