List of usage examples for org.apache.ibatis.mapping Environment Environment
public Environment(String id, TransactionFactory transactionFactory, DataSource dataSource)
From source file:examples.paging.LimitAndOffsetTest.java
License:Apache License
@BeforeEach public void setup() throws Exception { Class.forName(JDBC_DRIVER);/* ww w. j a va2s.co m*/ InputStream is = getClass().getResourceAsStream("/examples/animal/data/CreateAnimalData.sql"); try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) { ScriptRunner sr = new ScriptRunner(connection); sr.setLogWriter(null); sr.runScript(new InputStreamReader(is)); } UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); Configuration config = new Configuration(environment); config.addMapper(LimitAndOffsetMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); }
From source file:mbg.test.mb3.dsql.AbstractTest.java
License:Apache License
@Before public void setUp() throws Exception { createDatabase();//from ww w. j a v a 2 s. c om UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); Configuration config = new Configuration(environment); config.addMapper(AwfulTableMapper.class); config.addMapper(FieldsblobsMapper.class); config.addMapper(FieldsonlyMapper.class); config.addMapper(PkblobsMapper.class); config.addMapper(PkfieldsblobsMapper.class); config.addMapper(PkfieldsMapper.class); config.addMapper(PkonlyMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); }
From source file:mbg.test.mb3.dsql.miscellaneous.AbstractAnnotatedMiscellaneousTest.java
License:Apache License
@Before public void setUp() throws Exception { createDatabase();//from w w w . ja v a2 s.c om UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); Configuration config = new Configuration(environment); config.addMapper(EnumtestMapper.class); config.addMapper(GeneratedalwaystestMapper.class); config.addMapper(GeneratedalwaystestnoupdatesMapper.class); config.addMapper(MyObjectMapper.class); config.addMapper(RegexrenameMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); }
From source file:net.butfly.albacore.spring.AutoSqlSessionFactoryBean.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 w w . ja v a2 s .c om * @throws IOException * if loading the config file failed */ protected SqlSessionFactory buildSqlSessionFactory() throws IOException { Configuration configuration; XMLConfigBuilder xmlConfigBuilder = null; if (this.configLocation == null) this.configLocation = Springs .searchResource(null == this.mybatisConfigLocationPattern ? DEFALUT_MYBATIS_CONF_PATTERN : this.mybatisConfigLocationPattern); if (this.configLocation != null) { xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties); configuration = xmlConfigBuilder.getConfiguration(); if (logger.isTraceEnabled()) logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Mybatis configuration loaded: " + this.configLocation); } else { logger.warn( "Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Property 'configLocation' not specified, using default MyBatis Configuration"); configuration = new Configuration(); configuration.setVariables(this.configurationProperties); } this.fillMybatisProperties(configuration); 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 (logger.isTraceEnabled()) logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Scanned package: '" + packageToScan + "' for aliases"); } } if (!isEmpty(this.typeAliases)) for (Class<?> typeAlias : this.typeAliases) { configuration.getTypeAliasRegistry().registerAlias(typeAlias); if (logger.isTraceEnabled()) logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Registered type alias: '" + typeAlias + "'"); } if (!isEmpty(this.plugins)) for (Interceptor plugin : this.plugins) { configuration.addInterceptor(plugin); if (logger.isTraceEnabled()) logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - 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.isTraceEnabled()) { logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Scanned package: '" + packageToScan + "' for type handlers"); } } } if (!isEmpty(this.typeHandlers)) { for (TypeHandler<?> typeHandler : this.typeHandlers) { configuration.getTypeHandlerRegistry().register(typeHandler); if (logger.isTraceEnabled()) { logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Registered type handler: '" + typeHandler + "'"); } } } if (xmlConfigBuilder != null) try { xmlConfigBuilder.parse(); if (logger.isTraceEnabled()) { logger.trace( "Albacore Impl for Mybatis AutoSqlSessionFactoryBean - 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); } if (!isEmpty(this.mapperLocations)) { for (Resource mapperLocation : this.mapperLocations) if (mapperLocation != null) this.registerMapper(configuration, mapperLocation); } else if (logger.isTraceEnabled()) logger.trace( "Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Property 'mapperLocations' was not specified or no matching resources found"); Resource mapperCache = Springs .searchResource(null == this.mybatisCacheConfigLocationPattern ? DEFAULT_MYBATIS_CACHE_CONF_PATTERN : this.mybatisCacheConfigLocationPattern); if (mapperCache != null) this.registerMapper(configuration, mapperCache); return this.sqlSessionFactoryBuilder.build(configuration); }
From source file:net.landora.video.data.AbstractDataManager.java
License:Open Source License
private synchronized SqlSessionFactory getFactory() { if (factory == null) { TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, DatabaseConnectionManager.getInstance().getDataSource()); Configuration configuration = new Configuration(environment); configuration.setCacheEnabled(true); configuration.setLazyLoadingEnabled(false); configuration.getTypeHandlerRegistry().register(Calendar.class, JdbcType.TIMESTAMP, new CalendarTypeHandler()); configuration.getTypeHandlerRegistry().register(Calendar.class, JdbcType.DATE, new DateCalendarTypeHandler()); for (Class<?> clazz : getAliasTypeClasses()) { String className = clazz.getSimpleName(); configuration.getTypeAliasRegistry().registerAlias(className, clazz); }// www . ja va2s . c om for (Class<?> clazz : getMapperClasses()) { configuration.addMapper(clazz); } factory = new SqlSessionFactoryBuilder().build(configuration); } return factory; }
From source file:och.comp.db.base.BaseDb.java
License:Apache License
public BaseDb(DataSource ds, Props props, String url) { this.ds = ds; this.props = props; this.dialect = props.getStrVal(PropKey.db_dialect); String mappersPackageName = getClass().getPackage().getName(); //mybatis//from w w w . j av a 2 s . c o m TransactionFactory txFactory = new JdbcTransactionFactory(); Environment environment = new Environment("prod", txFactory, ds); Configuration config = new Configuration(environment); config.addMappers(mappersPackageName, BaseMapper.class); mappers = config.getMapperRegistry().getMappers(); sessionFactory = new SqlSessionFactoryBuilder().build(config); universal = new UniversalQueries(ds, props, url); }
From source file:org.activiti.crystalball.simulator.impl.cfg.SimulationEngineConfigurationImpl.java
License:Apache License
protected void initSqlSessionFactory() { if (sqlSessionFactory == null) { InputStream inputStream = null; try {//w w w. jav a 2 s. com inputStream = getMyBatisXmlConfigurationSteam(); // update the jdbc parameters to the configured ones... Environment environment = new Environment("default", transactionFactory, dataSource); Reader reader = new InputStreamReader(inputStream); Properties properties = new Properties(); properties.put("prefix", databaseTablePrefix); if (databaseType != null) { properties.put("limitBefore", DbSimulatorSqlSessionFactory.databaseSpecificLimitBeforeStatements.get(databaseType)); properties.put("limitAfter", DbSimulatorSqlSessionFactory.databaseSpecificLimitAfterStatements.get(databaseType)); properties.put("limitBetween", DbSimulatorSqlSessionFactory.databaseSpecificLimitBetweenStatements.get(databaseType)); properties.put("orderBy", DbSimulatorSqlSessionFactory.databaseSpecificOrderByStatements.get(databaseType)); } XMLConfigBuilder parser = new XMLConfigBuilder(reader, "", properties); Configuration configuration = parser.getConfiguration(); configuration.setEnvironment(environment); configuration.getTypeHandlerRegistry().register(VariableType.class, JdbcType.VARCHAR, new IbatisVariableTypeHandler()); configuration = parser.parse(); sqlSessionFactory = new DefaultSqlSessionFactory(configuration); } catch (Exception e) { throw new ActivitiException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e); } finally { IoUtil.closeSilently(inputStream); } } }
From source file:org.activiti.DeprecateMultiTenantProcessEngineConfiguration.java
License:Apache License
protected void initSqlSessionFactoryForTenant(MultiTenantSqlSessionFactory multiTenantSqlSessionFactory, String tenantId) {// w ww . j a va 2s . c o m logger.info("Initializing sql session factory for tenant " + tenantId); // This could be optimized by a hack: http://www.jorambarrez.be/blog/2014/08/22/seriously-reducing-memory/ // For each database type, the Mybatis configuration could be cached and reused. // But not applied it, as it's a quite dirty bit of hacking with relfection. InputStream inputStream = null; try { inputStream = getMyBatisXmlConfigurationStream(); DataSource dataSource = datasources.get(tenantId); MultiTenantDataSourceConfiguration dataSourceConfiguration = null; for (MultiTenantDataSourceConfiguration d : dataSourceConfigurations) { if (d.getTenantId().equals(tenantId)) { dataSourceConfiguration = d; break; } } String databaseType = dataSourceConfiguration.getDatabaseType(); Environment environment = new Environment("default", transactionFactory, dataSource); Reader reader = new InputStreamReader(inputStream); Properties properties = new Properties(); properties.put("prefix", databaseTablePrefix); if (databaseType != null) { properties.put("limitBefore", DbSqlSessionFactory.databaseSpecificLimitBeforeStatements.get(databaseType)); properties.put("limitAfter", DbSqlSessionFactory.databaseSpecificLimitAfterStatements.get(databaseType)); properties.put("limitBetween", DbSqlSessionFactory.databaseSpecificLimitBetweenStatements.get(databaseType)); properties.put("limitOuterJoinBetween", DbSqlSessionFactory.databaseOuterJoinLimitBetweenStatements.get(databaseType)); properties.put("orderBy", DbSqlSessionFactory.databaseSpecificOrderByStatements.get(databaseType)); properties.put("limitBeforeNativeQuery", ObjectUtils.toString( DbSqlSessionFactory.databaseSpecificLimitBeforeNativeQueryStatements.get(databaseType))); } Configuration configuration = initMybatisConfiguration(environment, reader, properties); multiTenantSqlSessionFactory.addConfig(tenantId, configuration); } catch (Exception e) { throw new ActivitiException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e); } finally { IoUtil.closeSilently(inputStream); } }
From source file:org.activiti.engine.AbstractEngineConfiguration.java
License:Apache License
public void initSqlSessionFactory() { if (sqlSessionFactory == null) { InputStream inputStream = null; try {/*from w w w. j a v a 2 s. c o m*/ inputStream = getMyBatisXmlConfigurationStream(); Environment environment = new Environment("default", transactionFactory, dataSource); Reader reader = new InputStreamReader(inputStream); Properties properties = new Properties(); properties.put("prefix", databaseTablePrefix); String wildcardEscapeClause = ""; if ((databaseWildcardEscapeCharacter != null) && (databaseWildcardEscapeCharacter.length() != 0)) { wildcardEscapeClause = " escape '" + databaseWildcardEscapeCharacter + "'"; } properties.put("wildcardEscapeClause", wildcardEscapeClause); // set default properties properties.put("limitBefore", ""); properties.put("limitAfter", ""); properties.put("limitBetween", ""); properties.put("limitOuterJoinBetween", ""); properties.put("limitBeforeNativeQuery", ""); properties.put("orderBy", "order by ${orderByColumns}"); properties.put("blobType", "BLOB"); properties.put("boolValue", "TRUE"); if (databaseType != null) { properties.load(getResourceAsStream(pathToEngineDbProperties())); } Configuration configuration = initMybatisConfiguration(environment, reader, properties); sqlSessionFactory = new DefaultSqlSessionFactory(configuration); } catch (Exception e) { throw new ActivitiException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e); } finally { IoUtil.closeSilently(inputStream); } } }
From source file:org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.java
License:Apache License
protected void initSqlSessionFactory() { if (sqlSessionFactory == null) { InputStream inputStream = null; try {/*from w w w . ja v a 2s .c om*/ inputStream = getMyBatisXmlConfigurationSteam(); // update the jdbc parameters to the configured ones... Environment environment = new Environment("default", transactionFactory, dataSource); Reader reader = new InputStreamReader(inputStream); Properties properties = new Properties(); properties.put("prefix", databaseTablePrefix); if (databaseType != null) { properties.put("limitBefore", DbSqlSessionFactory.databaseSpecificLimitBeforeStatements.get(databaseType)); properties.put("limitAfter", DbSqlSessionFactory.databaseSpecificLimitAfterStatements.get(databaseType)); properties.put("limitBetween", DbSqlSessionFactory.databaseSpecificLimitBetweenStatements.get(databaseType)); properties.put("limitOuterJoinBetween", DbSqlSessionFactory.databaseOuterJoinLimitBetweenStatements.get(databaseType)); properties.put("orderBy", DbSqlSessionFactory.databaseSpecificOrderByStatements.get(databaseType)); properties.put("limitBeforeNativeQuery", ObjectUtils .toString(DbSqlSessionFactory.databaseSpecificLimitBeforeNativeQueryStatements .get(databaseType))); } Configuration configuration = initMybatisConfiguration(environment, reader, properties); sqlSessionFactory = new DefaultSqlSessionFactory(configuration); } catch (Exception e) { throw new ActivitiException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e); } finally { IoUtil.closeSilently(inputStream); } } }