List of usage examples for org.apache.ibatis.session Configuration getSqlFragments
public Map<String, XNode> getSqlFragments()
From source file:org.sonar.core.persistence.MyBatis.java
License:Open Source License
private void loadMapper(Configuration configuration, String mapperName) { InputStream input = null;/*from ww w . jav a2 s . c o m*/ try { input = getClass().getResourceAsStream("/" + mapperName.replace('.', '/') + ".xml"); new XMLMapperBuilder(input, configuration, mapperName, configuration.getSqlFragments()).parse(); configuration.addLoadedResource(mapperName); } catch (Exception e) { throw new IllegalArgumentException("Unable to load mapper " + mapperName, e); } finally { Closeables.closeQuietly(input); } }
From source file:org.springframework.data.mybatis.repository.config.MybatisMappersRegister.java
License:Apache License
@Override public void afterPropertiesSet() throws Exception { Assert.notNull(sqlSessionFactory);//w ww. ja v a 2 s.c o m if (StringUtils.isEmpty(locations)) { return; } Configuration configuration = sqlSessionFactory.getConfiguration(); String[] split = locations.split(","); for (String s : split) { if (StringUtils.isEmpty(s)) { continue; } Resource[] resources = applicationContext.getResources(s); if (null == resources || resources.length == 0) { continue; } for (Resource r : resources) { InputStream inputStream = r.getInputStream(); String namespace = r.getFilename(); String rr = "after_" + namespace; XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(inputStream, configuration, rr, configuration.getSqlFragments()); try { xmlMapperBuilder.parse(); } catch (Exception e) { throw new MappingException("parse after mapping error for " + namespace, e); } finally { try { inputStream.close(); } catch (IOException e) { // ignore } } } } }
From source file:org.springframework.data.mybatis.repository.query.PartTreeMybatisQuery.java
License:Apache License
private void doCreateQueryStatement(MybatisQueryMethod method) { Configuration configuration = sqlSessionTemplate.getConfiguration(); String statementXML = ""; if (tree.isDelete()) { statementXML = doCreateDeleteQueryStatement(); } else if (tree.isCountProjection()) { statementXML = doCreateCountQueryStatement(getStatementName()); } else if (method.isPageQuery()) { statementXML = doCreatePageQueryStatement(true); } else if (method.isSliceQuery()) { statementXML = doCreatePageQueryStatement(false); } else if (method.isStreamQuery()) { } else if (method.isCollectionQuery()) { statementXML = doCreateCollectionQueryStatement(); } else if (method.isQueryForEntity()) { statementXML = doCreateSelectQueryStatement(getStatementName()); }/*from w w w .j a v a2 s. c om*/ StringBuilder builder = new StringBuilder(); builder.append(MAPPER_BEGIN); builder.append("<mapper namespace=\"" + getNamespace() + "\">"); builder.append(statementXML); builder.append(MAPPER_END); String xml = builder.toString(); if (logger.isDebugEnabled()) { logger.debug("\n******************* Auto Generate MyBatis Mapping XML (" + getStatementId() + ") *******************\n" + xml); } InputStream inputStream = null; try { inputStream = new ByteArrayInputStream(xml.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { // ignore } String namespace = getNamespace(); String resource = getStatementId() + "_auto_generate.xml"; try { XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments(), namespace); xmlMapperBuilder.parse(); } catch (Exception e) { throw new MappingException("create auto mapping error for " + namespace, e); } finally { try { inputStream.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } }
From source file:org.workspace7.osgi.mybatis.extender.impl.MyBatisMapperRegistry.java
License:Apache License
public void registerBundle(Bundle bundle) { Map<String, List<String>> dsMappers = ExtenderUtil.getMapperClauses(bundle); for (String dsName : dsMappers.keySet()) { Environment environment = registeredEnvironments.get(dsName); Configuration configuration = new Configuration(environment); if (configuration != null) { List<String> packageNames = dsMappers.get(dsName); for (String packageName : packageNames) { String cleanPackageName = packageName.trim(); logger.info("Adding mappers and xmls from package {} to Configuration {}", cleanPackageName, dsName);/*from ww w . jav a 2 s. c om*/ configuration.addMappers(cleanPackageName); Enumeration<URL> mapperXmls = ExtenderUtil.findFilesFromBundle(bundle, packageName, "*.xml"); if (mapperXmls != null) { while (mapperXmls.hasMoreElements()) { URL mapperxmlUrl = mapperXmls.nextElement(); try { String mappingFile = ExtenderUtil.mappingFileName(mapperxmlUrl.getFile()); logger.info("Adding Mapper XML {} to Config {} ", mappingFile, dsName); InputStream in = mapperxmlUrl.openStream(); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, mappingFile, configuration.getSqlFragments()); xmlMapperBuilder.parse(); in.close(); Collection<String> mappedStatements = configuration.getMappedStatementNames(); for (String mappedStmt : mappedStatements) { logger.info(" Added Mapped Statement: {}", mappedStmt); } unregisterSqlSessionFactory(dsName); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(configuration); Dictionary<String, String> props = new Hashtable<>(); props.put("dataSourceName", dsName); ServiceRegistration serviceRegistration = bundleContext.registerService( SqlSessionFactory.class.getName(), sqlSessionFactory, props); registeredSqlSessionFactories.put(dsName, serviceRegistration); } catch (IOException e) { logger.error("Unable to add mapper {} ", mapperxmlUrl.toString(), e); } } } } } } logger.info("Registering Mapper Bundle {} ", bundle.getSymbolicName()); activeBundles.add(bundle); }
From source file:paner.das.strong.SqlSessionFactoryBean.java
License:Apache License
/** * Build a {@code SqlSessionFactory} instance. * * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a * {@code SqlSessionFactory} instance based on an Reader. * * @return SqlSessionFactory// w ww. j a v a 2s. c o m * @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 (LOGGER.isDebugEnabled()) { 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) { // ThinkGem ??????? try { configuration.getTypeAliasRegistry().registerAliases(packageToScan, typeAliasesSuperType == null ? Object.class : typeAliasesSuperType); } catch (Exception ex) { LOGGER.error("Scanned package: '" + packageToScan + "' for aliases", ex); throw new NestedIOException("Scanned package: '" + packageToScan + "' for aliases", ex); } finally { ErrorContext.instance().reset(); } // ThinkGem end if (LOGGER.isDebugEnabled()) { LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases"); } } } if (!isEmpty(this.typeAliases)) { for (Class<?> typeAlias : this.typeAliases) { configuration.getTypeAliasRegistry().registerAlias(typeAlias); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Registered type alias: '" + typeAlias + "'"); } } } if (!isEmpty(this.plugins)) { for (Interceptor plugin : this.plugins) { configuration.addInterceptor(plugin); if (LOGGER.isDebugEnabled()) { 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 (LOGGER.isDebugEnabled()) { LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers"); } } } if (!isEmpty(this.typeHandlers)) { for (TypeHandler<?> typeHandler : this.typeHandlers) { configuration.getTypeHandlerRegistry().register(typeHandler); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Registered type handler: '" + typeHandler + "'"); } } } if (xmlConfigBuilder != null) { try { xmlConfigBuilder.parse(); if (LOGGER.isDebugEnabled()) { 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(); } configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource)); if (this.databaseIdProvider != null) { try { configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource)); } catch (SQLException e) { throw new NestedIOException("Failed getting a databaseId", e); } } 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) { // ThinkGem MapperXML???? LOGGER.error("Failed to parse mapping resource: '" + mapperLocation + "'", e); throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e); } finally { ErrorContext.instance().reset(); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'"); } } // ThinkGem ?MapperXML? //new MapperRefreshThread(this.mapperLocations, configuration).run(); this.mapperRefresh.setConfiguration(configuration); } else { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found"); } } return this.sqlSessionFactoryBuilder.build(configuration); }