コード例 #1
0
 /**
  * 使用DataSource初始化SqlSessionFactory
  *
  * @param ds
  */
 public static void initialize(DataSource ds) {
   TransactionFactory transactionFactory = new MybatisTransactionFactory();
   Environment environment = new Environment("snaker", transactionFactory, ds);
   Configuration configuration = new Configuration(environment);
   configuration.getTypeAliasRegistry().registerAliases(SCAN_PACKAGE, Object.class);
   if (log.isInfoEnabled()) {
     Map<String, Class<?>> typeAliases = configuration.getTypeAliasRegistry().getTypeAliases();
     for (Entry<String, Class<?>> entry : typeAliases.entrySet()) {
       log.info(
           "Scanned class:[name=" + entry.getKey() + ",class=" + entry.getValue().getName() + "]");
     }
   }
   try {
     for (String resource : resources) {
       InputStream in = Resources.getResourceAsStream(resource);
       XMLMapperBuilder xmlMapperBuilder =
           new XMLMapperBuilder(in, configuration, resource, configuration.getSqlFragments());
       xmlMapperBuilder.parse();
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     ErrorContext.instance().reset();
   }
   sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
 }
コード例 #2
0
  /** {@inheritDoc} */
  public Configuration get() {
    final Configuration configuration = new Configuration(environment);
    configuration.setLazyLoadingEnabled(lazyLoadingEnabled);
    configuration.setAggressiveLazyLoading(aggressiveLazyLoading);
    configuration.setMultipleResultSetsEnabled(multipleResultSetsEnabled);
    configuration.setUseGeneratedKeys(useGeneratedKeys);
    configuration.setUseColumnLabel(useColumnLabel);
    configuration.setCacheEnabled(cacheEnabled);
    configuration.setDefaultExecutorType(defaultExecutorType);
    configuration.setAutoMappingBehavior(autoMappingBehavior);
    configuration.setObjectFactory(objectFactory);
    configuration.setObjectWrapperFactory(objectWrapperFactory);
    configuration.setDefaultScriptingLanguage(defaultScriptingLanguageType);
    configuration.setMapUnderscoreToCamelCase(mapUnderscoreToCamelCase);
    configuration.setCallSettersOnNulls(callSettersOnNulls);
    configuration.setDefaultStatementTimeout(defaultStatementTimeout);

    try {
      if (databaseIdProvider != null) {
        configuration.setDatabaseId(databaseIdProvider.getDatabaseId(dataSource));
      }

      for (Map.Entry<String, Class<?>> alias : typeAliases.entrySet()) {
        configuration.getTypeAliasRegistry().registerAlias(alias.getKey(), alias.getValue());
      }

      for (Map.Entry<Class<?>, TypeHandler<?>> typeHandler : typeHandlers.entrySet()) {
        registerTypeHandler(configuration, typeHandler.getKey(), typeHandler.getValue());
      }

      for (TypeHandler<?> typeHandler : mappingTypeHandlers) {
        configuration.getTypeHandlerRegistry().register(typeHandler);
      }

      for (Class<?> mapperClass : mapperClasses) {
        if (!configuration.hasMapper(mapperClass)) {
          configuration.addMapper(mapperClass);
        }
      }

      for (Interceptor interceptor : plugins) {
        configuration.addInterceptor(interceptor);
      }

      if (failFast) {
        configuration.getMappedStatementNames();
      }
    } catch (Throwable cause) {
      throw new ProvisionException(
          "An error occurred while building the org.apache.ibatis.session.Configuration", cause);
    } finally {
      ErrorContext.instance().reset();
    }

    return configuration;
  }
コード例 #3
0
ファイル: PcmUtil.java プロジェクト: pabi2/pb2_pabiweb
  public static SqlSession openSession(DataSource dataSource) {

    SqlSessionFactory sqlSessionFactory = DbConnectionFactory.getSqlSessionFactory(dataSource);

    Configuration config = sqlSessionFactory.getConfiguration();
    if (!config.hasMapper(PcmReqDAO.class)) {
      config.getTypeAliasRegistry().registerAlias("pcmReqModel", PcmReqModel.class);
      config.getTypeAliasRegistry().registerAlias("pcmReqDtlModel", PcmReqDtlModel.class);
      config.getTypeAliasRegistry().registerAlias("pcmReqMethodModel", PcmReqMethodModel.class);
      config
          .getTypeAliasRegistry()
          .registerAlias("pcmReqMethodCommitteeModel", PcmReqMethodCommitteeModel.class);
      config.getTypeAliasRegistry().registerAlias("pcmReqCmtHdrModel", PcmReqCmtHdrModel.class);
      config.getTypeAliasRegistry().registerAlias("pcmReqCmtDtlModel", PcmReqCmtDtlModel.class);

      config.addMapper(PcmReqDAO.class);
      config.addMapper(PcmReqDtlDAO.class);
      config.addMapper(PcmReqMethodDAO.class);
      config.addMapper(PcmReqMethodCommitteeDAO.class);
      config.addMapper(PcmReqCmtHdrDAO.class);
      config.addMapper(PcmReqCmtDtlDAO.class);
    }

    if (!config.hasMapper(PcmFunctionDAO.class)) {
      // config.getTypeAliasRegistry().registerAlias("memoWorkflowModel", MemoWorkflowModel.class);

      config.addMapper(PcmFunctionDAO.class);
    }

    if (!config.hasMapper(PcmOrdDAO.class)) {
      config.getTypeAliasRegistry().registerAlias("pcmOrdModel", PcmOrdModel.class);

      config.addMapper(PcmOrdDAO.class);
    }

    return sqlSessionFactory.openSession();
  }
コード例 #4
0
 public InlineParameterMapParser(Configuration configuration) {
   this.configuration = configuration;
   this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
   this.typeAliasRegistry = configuration.getTypeAliasRegistry();
 }
  /**
   * Build a {@code SqlSessionFactory} instance.
   *
   * <p>The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
   * {@code SqlSessionFactory} instance based on an Reader.
   *
   * @return SqlSessionFactory
   * @throws IOException if loading the config file failed
   */
  protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

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

    if (this.objectFactory != null) {
      configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
      configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (hasLength(this.typeAliasesPackage)) {
      String[] typeAliasPackageArray =
          tokenizeToStringArray(
              this.typeAliasesPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
      for (String packageToScan : typeAliasPackageArray) {
        configuration
            .getTypeAliasRegistry()
            .registerAliases(
                packageToScan, typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
        if (this.logger.isDebugEnabled()) {
          this.logger.debug("Scanned package: '" + packageToScan + "' for aliases");
        }
      }
    }

    if (!isEmpty(this.typeAliases)) {
      for (Class<?> typeAlias : this.typeAliases) {
        configuration.getTypeAliasRegistry().registerAlias(typeAlias);
        if (this.logger.isDebugEnabled()) {
          this.logger.debug("Registered type alias: '" + typeAlias + "'");
        }
      }
    }

    if (!isEmpty(this.plugins)) {
      for (Interceptor plugin : this.plugins) {
        configuration.addInterceptor(plugin);
        if (this.logger.isDebugEnabled()) {
          this.logger.debug("Registered plugin: '" + plugin + "'");
        }
      }
    }

    if (hasLength(this.typeHandlersPackage)) {
      String[] typeHandlersPackageArray =
          tokenizeToStringArray(
              this.typeHandlersPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
      for (String packageToScan : typeHandlersPackageArray) {
        configuration.getTypeHandlerRegistry().register(packageToScan);
        if (this.logger.isDebugEnabled()) {
          this.logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
        }
      }
    }

    if (!isEmpty(this.typeHandlers)) {
      for (TypeHandler<?> typeHandler : this.typeHandlers) {
        configuration.getTypeHandlerRegistry().register(typeHandler);
        if (this.logger.isDebugEnabled()) {
          this.logger.debug("Registered type handler: '" + typeHandler + "'");
        }
      }
    }

    if (xmlConfigBuilder != null) {
      try {
        xmlConfigBuilder.parse();

        if (this.logger.isDebugEnabled()) {
          this.logger.debug("Parsed configuration file: '" + this.configLocation + "'");
        }
      } catch (Exception ex) {
        throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
      } finally {
        ErrorContext.instance().reset();
      }
    }

    if (this.transactionFactory == null) {
      this.transactionFactory = new SpringManagedTransactionFactory();
    }

    Environment environment =
        new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);

    if (this.databaseIdProvider != null) {
      try {
        configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
      } catch (SQLException e) {
        throw new NestedIOException("Failed getting a databaseId", e);
      }
    }

    ArrayList<Resource> listado_resources = new ArrayList<Resource>();

    if (!isEmpty(this.mapperLocations1)) {
      for (Resource resource : this.mapperLocations1) {
        listado_resources.add(resource);
      }
    }

    if (!isEmpty(this.mapperLocations2)) {
      for (Resource resource : this.mapperLocations2) {
        listado_resources.add(resource);
      }
    }

    if (!isEmpty(this.mapperLocations3)) {
      for (Resource resource : this.mapperLocations3) {
        listado_resources.add(resource);
      }
    }

    if (!isEmpty(this.mapperLocations4)) {
      for (Resource resource : this.mapperLocations4) {
        listado_resources.add(resource);
      }
    }

    this.mapperLocations = new Resource[listado_resources.size()];
    int index = 0;
    for (Resource resource : listado_resources) {
      this.mapperLocations[index] = resource;
      index++;
    }

    if (!isEmpty(this.mapperLocations)) {
      for (Resource mapperLocation : this.mapperLocations) {
        if (mapperLocation == null) {
          continue;
        }

        //				//log.info("Mapa cargado: " + mapperLocation);

        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 (this.logger.isDebugEnabled()) {
          this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
        }
      }
    } else {
      if (this.logger.isDebugEnabled()) {
        this.logger.debug(
            "Property 'mapperLocations' was not specified or no matching resources found");
      }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
  }
コード例 #6
0
ファイル: MyBatis.java プロジェクト: eSh3m4/sonarqube
 private void loadAlias(Configuration conf, String alias, Class dtoClass) {
   conf.getTypeAliasRegistry().registerAlias(alias, dtoClass);
 }