org.apache.ibatis.session.Configuration类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(16.3k)|赞(0)|评价(0)|浏览(167)

本文整理了Java中org.apache.ibatis.session.Configuration类的一些代码示例,展示了Configuration类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration类的具体详情如下:
包路径:org.apache.ibatis.session.Configuration
类名称:Configuration

Configuration介绍

暂无

代码示例

代码示例来源:origin: wuyouzhuguli/FEBS-Shiro

private static String showSql(Configuration configuration, BoundSql boundSql) {
  Object parameterObject = boundSql.getParameterObject();
  List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
  if (!CollectionUtils.isEmpty(parameterMappings) && parameterObject != null) {
    TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
    if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
      sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(parameterObject)));
    } else {
      MetaObject metaObject = configuration.newMetaObject(parameterObject);
      for (ParameterMapping parameterMapping : parameterMappings) {
        String propertyName = parameterMapping.getProperty();
        if (metaObject.hasGetter(propertyName)) {
          Object obj = metaObject.getValue(propertyName);
          sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(obj)));
        } else if (boundSql.hasAdditionalParameter(propertyName)) {

代码示例来源:origin: SonarSource/sonarqube

MyBatisConfBuilder(Database database) {
 this.conf = new Configuration();
 this.conf.setEnvironment(new Environment("production", createTransactionFactory(), database.getDataSource()));
 this.conf.setUseGeneratedKeys(true);
 this.conf.setLazyLoadingEnabled(false);
 this.conf.setJdbcTypeForNull(JdbcType.NULL);
 Dialect dialect = database.getDialect();
 this.conf.setDatabaseId(dialect.getId());
 this.conf.getVariables().setProperty("_true", dialect.getTrueSqlValue());
 this.conf.getVariables().setProperty("_false", dialect.getFalseSqlValue());
 this.conf.getVariables().setProperty("_from_dual", dialect.getSqlFromDual());
 this.conf.getVariables().setProperty("_scrollFetchSize", String.valueOf(dialect.getScrollDefaultFetchSize()));
 this.conf.setLocalCacheScope(LocalCacheScope.STATEMENT);
}

代码示例来源:origin: baomidou/mybatis-plus

private void mapperElement(XNode parent) throws Exception {
  /*
   * 定义集合 用来分类放置mybatis的Mapper与XML 按顺序依次遍历
   */
  if (parent != null) {
    //指定在classpath中的mapper文件
    Set<String> resources = new HashSet<>();
    //指向一个mapper接口
    Set<Class<?>> mapperClasses = new HashSet<>();
    setResource(parent, resources, mapperClasses);
    // 依次遍历 首先 resource 然后 mapper
    for (String resource : resources) {
      ErrorContext.instance().resource(resource);
      InputStream inputStream = Resources.getResourceAsStream(resource);
      //TODO
      XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,
        configuration.getSqlFragments());
      mapperParser.parse();
    }
    for (Class<?> mapper : mapperClasses) {
      //TODO
      configuration.addMapper(mapper);
    }
  }
}

代码示例来源:origin: SonarSource/sonarqube

private void retryBindMapperForNamespace() {
  if (!configuration.hasMapper(mapperType)) {
   // Spring may not know the real resource name so we set a flag
   // to prevent loading again this resource from the mapper interface
   // look at MapperAnnotationBuilder#loadXmlResource
   configuration.addLoadedResource("namespace:" + mapperType.getName());
   configuration.addMapper(mapperType);
  }
 }
}

代码示例来源:origin: baomidou/mybatis-plus

private <E> Object convertToDeclaredCollection(Configuration config, List<E> list) {
  Object collection = config.getObjectFactory().create(method.getReturnType());
  MetaObject metaObject = config.newMetaObject(collection);
  metaObject.addAll(list);
  return collection;
}

代码示例来源:origin: baomidou/mybatis-plus

@Override
@SuppressWarnings("unchecked")
public void setParameters(PreparedStatement ps) {
  ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
  List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  if (parameterMappings != null) {
    for (int i = 0; i < parameterMappings.size(); i++) {
        Object value;
        String propertyName = parameterMapping.getProperty();
        if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
          value = boundSql.getAdditionalParameter(propertyName);
        } else if (parameterObject == null) {
          value = null;
        } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
          value = parameterObject;
        } else {
          MetaObject metaObject = configuration.newMetaObject(parameterObject);
          value = metaObject.getValue(propertyName);
          jdbcType = configuration.getJdbcTypeForNull();

代码示例来源:origin: abel533/Mapper

String keyId = ms.getId() + SelectKeyGenerator.SELECT_KEY_SUFFIX;
if (ms.getConfiguration().hasKeyGenerator(keyId)) {
  return;
Configuration configuration = ms.getConfiguration();
KeyGenerator keyGenerator;
String IDENTITY = (column.getGenerator() == null || "".equals(column.getGenerator())) ? identity : column.getGenerator();
  statementBuilder.keyColumn(null);
  statementBuilder.databaseId(null);
  statementBuilder.lang(configuration.getDefaultScriptingLanuageInstance());
  statementBuilder.resultOrdered(false);
  statementBuilder.resulSets(null);
  statementBuilder.timeout(configuration.getDefaultStatementTimeout());
    configuration.addMappedStatement(statement);
  } catch (Exception e) {
  MappedStatement keyStatement = configuration.getMappedStatement(keyId, false);
    configuration.addKeyGenerator(keyId, keyGenerator);
  } catch (Exception e) {
  msObject.setValue("keyGenerator", keyGenerator);
  msObject.setValue("keyProperties", column.getTable().getKeyProperties());
  msObject.setValue("keyColumns", column.getTable().getKeyColumns());
} catch (Exception e) {

代码示例来源:origin: baomidou/mybatis-plus

private void settingsElement(Properties props) {
  configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
  configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE")));
  configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
  configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));
  configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
  configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), false));
  configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
  configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
  configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
  configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
  configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
  configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
  configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
  configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
  configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
  configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
  configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
  configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
  configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
  configuration.setDefaultEnumTypeHandler(resolveClass(props.getProperty("defaultEnumTypeHandler")));
  configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
  configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true));
  configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));
  configuration.setLogPrefix(props.getProperty("logPrefix"));
  configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
}

代码示例来源:origin: pagehelper/Mybatis-PageHelper

boolean hasTypeHandler = ms.getConfiguration().getTypeHandlerRegistry().hasTypeHandler(parameterObject.getClass());
MetaObject metaObject = MetaObjectUtil.forObject(parameterObject);
  for (String name : metaObject.getGetterNames()) {
    paramMap.put(name, metaObject.getValue(name));
if (boundSql.getParameterMappings() != null && boundSql.getParameterMappings().size() > 0) {
  for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) {
    String name = parameterMapping.getProperty();
    if (!name.equals(PAGEPARAMETER_FIRST)

代码示例来源:origin: abel533/Mapper

private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
  try {
    if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) {
      String[] keyProperties = keyStatement.getKeyProperties();
      final Configuration configuration = ms.getConfiguration();
      final MetaObject metaParam = configuration.newMetaObject(parameter);
      if (keyProperties != null) {
        Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
        List<Object> values = keyExecutor.query(keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        if (values.size() == 0) {
          throw new ExecutorException("SelectKey returned more than one value.");
        } else {
          MetaObject metaResult = configuration.newMetaObject(values.get(0));
          if (keyProperties.length == 1) {
            if (metaResult.hasGetter(keyProperties[0])) {
              setValue(metaParam, keyProperties[0], metaResult.getValue(keyProperties[0]));
            } else {

代码示例来源:origin: camunda/camunda-bpm-platform

private void handleLocallyCachedOutputParameters(MappedStatement ms, CacheKey key, Object parameter, BoundSql boundSql) {
 if (ms.getStatementType() == StatementType.CALLABLE) {
  final Object cachedParameter = localOutputParameterCache.getObject(key);
  if (cachedParameter != null && parameter != null) {
   final MetaObject metaCachedParameter = configuration.newMetaObject(cachedParameter);
   final MetaObject metaParameter = configuration.newMetaObject(parameter);
   for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) {
    if (parameterMapping.getMode() != ParameterMode.IN) {
     final String parameterName = parameterMapping.getProperty();
     final Object cachedValue = metaCachedParameter.getValue(parameterName);
     metaParameter.setValue(parameterName, cachedValue);
    }
   }
  }
 }
}

代码示例来源:origin: baomidou/mybatis-plus

MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
  Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
      metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
    } else if (tableInfo.getIdType() == IdType.ID_WORKER_STR) {
      metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getIdStr());
    } else if (tableInfo.getIdType() == IdType.UUID) {
      metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());

代码示例来源:origin: camunda/camunda-bpm-platform

@Override
public int executeNonEmptyUpdateStmt(String updateStmt, Object parameter) {
 updateStmt = dbSqlSessionFactory.mapStatement(updateStmt);
 //if mapped statement is empty, which can happens for some databases, we have no need to execute it
 MappedStatement mappedStatement = sqlSession.getConfiguration().getMappedStatement(updateStmt);
 if (mappedStatement.getBoundSql(parameter).getSql().isEmpty())
  return 0;
 return sqlSession.update(updateStmt, parameter);
}

代码示例来源:origin: baomidou/mybatis-plus

public MybatisDefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
  super(mappedStatement, processBatch(mappedStatement, parameterObject), boundSql);
  this.mappedStatement = mappedStatement;
  this.configuration = mappedStatement.getConfiguration();
  this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
  this.parameterObject = parameterObject;
  this.boundSql = boundSql;
}

代码示例来源:origin: baomidou/mybatis-plus

/**
 * MybatisPlus 加载 SQL 顺序:
 * <p>1、加载XML中的SQL</p>
 * <p>2、加载sqlProvider中的SQL</p>
 * <p>3、xmlSql 与 sqlProvider不能包含相同的SQL</p>
 * <p>调整后的SQL优先级:xmlSql > sqlProvider > curdSql</p>
 */
@Override
public void addMappedStatement(MappedStatement ms) {
  logger.debug("addMappedStatement: " + ms.getId());
  if (GlobalConfigUtils.isRefresh(ms.getConfiguration())) {
    /*
     * 支持是否自动刷新 XML 变更内容,开发环境使用【 注:生产环境勿用!】
     */
    mappedStatements.remove(ms.getId());
  } else {
    if (mappedStatements.containsKey(ms.getId())) {
      /*
       * 说明已加载了xml中的节点; 忽略mapper中的SqlProvider数据
       */
      logger.error("mapper[" + ms.getId() + "] is ignored, because it exists, maybe from xml file");
      return;
    }
  }
  super.addMappedStatement(ms);
}

代码示例来源:origin: camunda/camunda-bpm-platform

@Override
public void handleOutputParameters(CallableStatement cs) throws SQLException {
 final Object parameterObject = parameterHandler.getParameterObject();
 final MetaObject metaParam = configuration.newMetaObject(parameterObject);
 final List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
 for (int i = 0; i < parameterMappings.size(); i++) {
  final ParameterMapping parameterMapping = parameterMappings.get(i);
  if (parameterMapping.getMode() == ParameterMode.OUT || parameterMapping.getMode() == ParameterMode.INOUT) {
   if (ResultSet.class.equals(parameterMapping.getJavaType())) {
    handleRefCursorOutputParameter((ResultSet) cs.getObject(i + 1), parameterMapping, metaParam);
   } else {
    final TypeHandler<?> typeHandler = parameterMapping.getTypeHandler();
    metaParam.setValue(parameterMapping.getProperty(), typeHandler.getResult(cs, i + 1));
   }
  }
 }
}

代码示例来源:origin: hs-web/hsweb-framework

private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
  try {
    boolean autoCommit;
    try {
      autoCommit = connection.getAutoCommit();
    } catch (SQLException e) {
      // Failover to true, as most poor drivers
      // or databases won't support transactions
      autoCommit = true;
    }
    final Environment environment = configuration.getEnvironment();
    final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
    final Transaction tx = transactionFactory.newTransaction(connection);
    final Executor executor = configuration.newExecutor(tx, execType);
    return new DefaultSqlSession(configuration, executor, autoCommit);
  } catch (Exception e) {
    throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  } finally {
    ErrorContext.instance().reset();
  }
}

代码示例来源:origin: jneat/mybatis-types

static synchronized void setupSessionFactoryBuilder(DataSource ds) {
  TransactionFactory transactionFactory = new JdbcTransactionFactory();
  Environment environment = new Environment("jneat", transactionFactory, ds);
  Configuration configuration = new Configuration(environment);
  configuration.getTypeHandlerRegistry().register("com.github.jneat.mybatis");
  configuration.setMapUnderscoreToCamelCase(true);
  // Add Mappers
  configuration.addMapper(TypesMapper.class);
  configuration.addMapper(ArraysMapper.class);
  configuration.addMapper(TimeMapper.class);
  ssf = new SqlSessionFactoryBuilder().build(configuration);
}

代码示例来源:origin: abel533/Mapper

/**
 * {@inheritDoc}
 */
@Override
protected void checkDaoConfig() {
  super.checkDaoConfig();
  notNull(this.mapperInterface, "Property 'mapperInterface' is required");
  Configuration configuration = getSqlSession().getConfiguration();
  if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {
    try {
      configuration.addMapper(this.mapperInterface);
    } catch (Exception e) {
      logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
      throw new IllegalArgumentException(e);
    } finally {
      ErrorContext.instance().reset();
    }
  }
  //直接针对接口处理通用接口方法对应的 MappedStatement 是安全的,通用方法不会出现 IncompleteElementException 的情况
  if (configuration.hasMapper(this.mapperInterface) && mapperHelper != null && mapperHelper.isExtendCommonMapper(this.mapperInterface)) {
    mapperHelper.processConfiguration(getSqlSession().getConfiguration(), this.mapperInterface);
  }
}

代码示例来源:origin: camunda/camunda-bpm-platform

private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
 super(new Configuration());
 ErrorContext.instance().resource("SQL Mapper Configuration");
 this.configuration.setVariables(props);
 this.parsed = false;
 this.environment = environment;
 this.parser = parser;
}

相关文章

微信公众号

最新文章

更多

Configuration类方法