三、代码重构

1、先使用Eclipse把buildSqlSessionFactory()方法中众多的if换成小函数

protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

        Configuration configuration;

        XMLConfigBuilder xmlConfigBuilder = null;
        if (this.configuration != null) {
            configuration = this.configuration;
            if (configuration.getVariables() == null) {
                configuration.setVariables(this.configurationProperties);
            } else if (this.configurationProperties != null) {
                configuration.getVariables().putAll(this.configurationProperties);
            }
        } else if (this.configLocation != null) {
            xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null,
                    this.configurationProperties);
            configuration = xmlConfigBuilder.getConfiguration();
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(
                        "Property `configuration` or 'configLocation' not specified, using default MyBatis Configuration");
            }
            configuration = new Configuration();
            configuration.setVariables(this.configurationProperties);
        }

        doResolveObjectFactory(configuration);
        doResolveObjectWrapperFactory(configuration);
        doResolveVfs(configuration);
        doResolveTypeAliasesPackage(configuration);
        doResolveTypeAliases(configuration);
        doResolvePlugins(configuration);
        doResolveTypeHandlersPackage(configuration);
        doResolveTypeHandlers(configuration);
        doResolveDatabaseIdProvider(configuration);
        doResolveCache(configuration);
        doParseConfig(xmlConfigBuilder);
        doResolveTransactionFactory();
        doResolveEnvironment(configuration);
        doParseSqlMapper(configuration);

        return this.sqlSessionFactoryBuilder.build(configuration);
}

说明一下:

  • 这里的重构全部使用Eclipse完成,操作步骤是选定需要重构的代码,右键选择Refactor—>Extract Method,然后输入新的方法名,点击OK完成
  • 新方法名规则:全部使用do开头,表示实际做某件事,对于解析XML的,使用doParse(如doParseConfig、doParseSqlMapper),其它的则使用doResolve为前缀
  • 新方法一开始全部为private,但是为了后续扩展性,可以根据需要修改为protected
  • 第一段的if语句,由于有两个变量需要返回,直接使用Eclipse重构不成功,先保持不变

看其中一个重构提取的方法:

protected void doParseSqlMapper(Configuration configuration) throws NestedIOException {
        if (!isEmpty(this.mapperLocations)) {
            for (Resource mapperLocation : this.mapperLocations) {
                if (mapperLocation == null) {
                    continue;
                }

                try {
                    XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                            configuration, mapperLocation.toString(), configuration.getSqlFragments());
                    xmlMapperBuilder.parse();
                } catch (Exception e) {
                    throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
                } finally {
                    ErrorContext.instance().reset();
                }

                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
                }
            }
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
            }
        }
}
View Code

相关文章:

  • 2021-04-08
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-09-18
  • 2021-12-29
  • 2021-12-30
猜你喜欢
  • 2022-12-23
  • 2021-10-09
  • 2021-05-19
  • 2022-01-15
  • 2021-07-03
  • 2022-12-23
  • 2021-09-30
相关资源
相似解决方案