org.apache.ibatis.mapping.Environment Java Examples

The following examples show how to use org.apache.ibatis.mapping.Environment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultSqlSessionFactory.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
  Transaction tx = null;
  try {
    final Environment environment = configuration.getEnvironment();
    final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
    //通过事务工厂来产生一个事务
    tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
    //生成一个执行器(事务包含在执行器里)
    final Executor executor = configuration.newExecutor(tx, execType);
    //然后产生一个DefaultSqlSession
    return new DefaultSqlSession(configuration, executor, autoCommit);
  } catch (Exception e) {
    //如果打开事务出错,则关闭它
    closeTransaction(tx); // may have fetched a connection so lets call close()
    throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  } finally {
    //最后清空错误上下文
    ErrorContext.instance().reset();
  }
}
 
Example #2
Source File: XMLConfigBuilder.java    From QuickProject with Apache License 2.0 6 votes vote down vote up
private void environmentsElement(XNode context) throws Exception {
  if (context != null) {
    if (environment == null) {
      environment = context.getStringAttribute("default");
    }
    for (XNode child : context.getChildren()) {
      String id = child.getStringAttribute("id");
      if (isSpecifiedEnvironment(id)) {
        TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
        DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
        DataSource dataSource = dsFactory.getDataSource();
        Environment.Builder environmentBuilder = new Environment.Builder(id)
            .transactionFactory(txFactory)
            .dataSource(dataSource);
        configuration.setEnvironment(environmentBuilder.build());
      }
    }
  }
}
 
Example #3
Source File: ProcessEngineConfigurationImpl.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public Configuration initMybatisConfiguration(Environment environment, Reader reader, Properties properties) {
  XMLConfigBuilder parser = new XMLConfigBuilder(reader, "", properties);
  Configuration configuration = parser.getConfiguration();

  if(databaseType != null) {
      configuration.setDatabaseId(databaseType);
  }

  configuration.setEnvironment(environment);

  initMybatisTypeHandlers(configuration);
  initCustomMybatisMappers(configuration);

  configuration = parseMybatisConfiguration(configuration, parser);
  return configuration;
}
 
Example #4
Source File: NpeExtendsTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private SqlSessionFactory getSqlSessionFactoryWithConstructor() {
    UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory();
    Properties properties = new Properties();
    properties.setProperty("driver", "org.hsqldb.jdbcDriver");
    properties.setProperty("url", "jdbc:hsqldb:mem:extends_with_constructor");
    properties.setProperty("username", "sa");
    unpooledDataSourceFactory.setProperties(properties);
    Environment environment = new Environment("extends_with_constructor", new JdbcTransactionFactory(), unpooledDataSourceFactory.getDataSource());
    
    Configuration configuration = new Configuration();
    configuration.setEnvironment(environment);
    configuration.addMapper(StudentConstructorMapper.class);
    configuration.addMapper(TeacherMapper.class);
    configuration.getMappedStatementNames();
    configuration.setAutoMappingBehavior(AutoMappingBehavior.NONE);
    
    return new DefaultSqlSessionFactory(configuration);
}
 
Example #5
Source File: NpeExtendsTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private SqlSessionFactory getSqlSessionFactoryWithConstructor() {
    UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory();
    Properties properties = new Properties();
    properties.setProperty("driver", "org.hsqldb.jdbcDriver");
    properties.setProperty("url", "jdbc:hsqldb:mem:extends_with_constructor");
    properties.setProperty("username", "sa");
    unpooledDataSourceFactory.setProperties(properties);
    Environment environment = new Environment("extends_with_constructor", new JdbcTransactionFactory(), unpooledDataSourceFactory.getDataSource());
    
    Configuration configuration = new Configuration();
    configuration.setEnvironment(environment);
    configuration.addMapper(StudentConstructorMapper.class);
    configuration.addMapper(TeacherMapper.class);
    configuration.getMappedStatementNames();
    configuration.setAutoMappingBehavior(AutoMappingBehavior.NONE);
    
    return new DefaultSqlSessionFactory(configuration);
}
 
Example #6
Source File: BaseDb.java    From live-chat-engine with Apache License 2.0 6 votes vote down vote up
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
	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);
}
 
Example #7
Source File: BaseDb.java    From live-chat-engine with Apache License 2.0 6 votes vote down vote up
private CommitOnCloseSession openCommitOnCloseSession(boolean batch){
	
	ExecutorType executorType = batch? ExecutorType.BATCH : ExecutorType.SIMPLE;
	if( ! isSingleTxMode()){
		return new CommitOnCloseSession(sessionFactory.openSession(executorType));
	}

	//SINGLE CONN MODE
	Environment env = sessionFactory.getConfiguration().getEnvironment();
	DataSource ds = env.getDataSource();
	
	Connection conn = null;
	try {
		conn = getSingleOrNewConnection(ds);
	}catch (Exception e) {
		throw new IllegalStateException("can't get conneciton", e);
	}
	
	return new CommitOnCloseSession(sessionFactory.openSession(executorType, conn));


}
 
Example #8
Source File: HierarchicalXMLConfigBuilder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void environmentsElement(XNode context) throws Exception {
    if (context != null) {
        if (environment == null) {
            environment = context.getStringAttribute("default");
        }
        for (XNode child : context.getChildren()) {
            String id = child.getStringAttribute("id");
            if (isSpecifiedEnvironment(id)) {
                TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
                DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
                DataSource dataSource = dsFactory.getDataSource();
                Environment.Builder environmentBuilder = new Environment.Builder(id)
                        .transactionFactory(txFactory)
                        .dataSource(dataSource);
                configuration.setEnvironment(environmentBuilder.build());
            }
        }
    }
}
 
Example #9
Source File: MyBatisGeneratorTool.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 获取SqlSession
 * @return
 * @throws IOException
 */
public SqlSession getSqlSession() throws IOException, ClassNotFoundException {
    org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
    config.setCallSettersOnNulls(true); // 设计null调用setter方法
    config.setMapUnderscoreToCamelCase(true);   // 驼峰命名支持

    // 设置mapper
    config.addMappers(targetPackage);
    // 设置数据源,事务
    PooledDataSourceFactory dataSourceFactory = new PooledDataSourceFactory();
    dataSourceFactory.setProperties(DBHelper.properties);
    DataSource dataSource = dataSourceFactory.getDataSource();
    JdbcTransactionFactory transactionFactory = new JdbcTransactionFactory();

    Environment environment = new Environment("test", transactionFactory, dataSource);
    config.setEnvironment(environment);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
    return sqlSessionFactory.openSession(true);
}
 
Example #10
Source File: SubstitutionInAnnotsTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:annots", "sa", "");
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/substitution_in_annots/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(c);
  runner.setLogWriter(null);
  runner.setErrorLogWriter(null);
  runner.runScript(reader);
  c.commit();
  reader.close();

  Configuration configuration = new Configuration();
  Environment environment = new Environment("test", new JdbcTransactionFactory(), new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:annots", null));
  configuration.setEnvironment(environment);
  
  configuration.addMapper(SubstitutionInAnnotsMapper.class);
  
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
 
Example #11
Source File: ManyAnnoTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetMessageForEmptyDatabase() throws Exception {
  final Environment environment = new Environment("test", new JdbcTransactionFactory(), BaseDataTest.createBlogDataSource());
  final Configuration config = new Configuration(environment);
  config.addMapper(PostMapper.class);
  final SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config);
  final SqlSession session = factory.openSession();
  
  PostMapper mapper = session.getMapper(PostMapper.class);
  List<AnnoPost> posts = mapper.getPosts(101);


  assertEquals(3,posts.size());
  assertEquals(3,posts.get(0).getTags().size());
  assertEquals(1,posts.get(1).getTags().size());
  assertEquals(0,posts.get(2).getTags().size());

  session.close();

}
 
Example #12
Source File: EnumWithOgnlTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfiguration() {
    UnpooledDataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory();
    Properties dataSourceProperties = new Properties();
    dataSourceProperties.put("driver", "org.hsqldb.jdbcDriver");
    dataSourceProperties.put("url", "jdbc:hsqldb:mem:xml_references");
    dataSourceProperties.put("username", "sa");
    dataSourceFactory.setProperties(dataSourceProperties);
    Environment environment = new Environment("test", new JdbcTransactionFactory(), dataSourceFactory.getDataSource());
    Configuration configuration = new Configuration();
    configuration.setEnvironment(environment);
    configuration.getTypeAliasRegistry().registerAlias(Person.class);
    configuration.addMapper(PersonMapper.class);
    configuration.addMapper(PersonMapper2.class);
    new DefaultSqlSessionFactory(configuration);
}
 
Example #13
Source File: SubstitutionInAnnotsTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:annots", "sa", "");
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/substitution_in_annots/CreateDB.sql");
  ScriptRunner runner = new ScriptRunner(c);
  runner.setLogWriter(null);
  runner.setErrorLogWriter(null);
  runner.runScript(reader);
  c.commit();
  reader.close();

  Configuration configuration = new Configuration();
  Environment environment = new Environment("test", new JdbcTransactionFactory(), new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:annots", null));
  configuration.setEnvironment(environment);
  
  configuration.addMapper(SubstitutionInAnnotsMapper.class);
  
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
 
Example #14
Source File: DynamicSqlSessionFactory.java    From hsweb-framework with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("all")
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
        final Environment environment = getConfiguration().getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        DataSource ds = DataSourceHolder.currentDataSource().getNative();
        if (ds == null) {
            ds = environment.getDataSource();
        }
        tx = transactionFactory.newTransaction(ds, level, autoCommit);
        final Executor executor = getConfiguration().newExecutor(tx, execType);
        return new DefaultSqlSession(getConfiguration(), executor, autoCommit);
    } catch (Exception e) {
        closeTransaction(tx); // may have fetched a connection so lets call close()
        throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}
 
Example #15
Source File: DefaultSqlSessionFactory.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
  Transaction tx = null;
  try {
    final Environment environment = configuration.getEnvironment();
    final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
    //通过事务工厂来产生一个事务
    tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
    //生成一个执行器(事务包含在执行器里)
    final Executor executor = configuration.newExecutor(tx, execType);
    //然后产生一个DefaultSqlSession
    return new DefaultSqlSession(configuration, executor, autoCommit);
  } catch (Exception e) {
    //如果打开事务出错,则关闭它
    closeTransaction(tx); // may have fetched a connection so lets call close()
    throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  } finally {
    //最后清空错误上下文
    ErrorContext.instance().reset();
  }
}
 
Example #16
Source File: XMLConfigBuilder.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private void environmentsElement(XNode context) throws Exception {
  if (context != null) {
    if (environment == null) {
      environment = context.getStringAttribute("default");
    }
    for (XNode child : context.getChildren()) {
      String id = child.getStringAttribute("id");
//循环比较id是否就是指定的environment
      if (isSpecifiedEnvironment(id)) {
        //7.1事务管理器
        TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
        //7.2数据源
        DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
        DataSource dataSource = dsFactory.getDataSource();
        Environment.Builder environmentBuilder = new Environment.Builder(id)
            .transactionFactory(txFactory)
            .dataSource(dataSource);
        configuration.setEnvironment(environmentBuilder.build());
      }
    }
  }
}
 
Example #17
Source File: XMLConfigBuilder.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private void environmentsElement(XNode context) throws Exception {
  if (context != null) {
    if (environment == null) {
      environment = context.getStringAttribute("default");
    }
    for (XNode child : context.getChildren()) {
      String id = child.getStringAttribute("id");
//循环比较id是否就是指定的environment
      if (isSpecifiedEnvironment(id)) {
        //7.1事务管理器
        TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
        //7.2数据源
        DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
        DataSource dataSource = dsFactory.getDataSource();
        Environment.Builder environmentBuilder = new Environment.Builder(id)
            .transactionFactory(txFactory)
            .dataSource(dataSource);
        configuration.setEnvironment(environmentBuilder.build());
      }
    }
  }
}
 
Example #18
Source File: MybatisHelper.java    From snakerflow with Apache License 2.0 6 votes vote down vote up
/**
 * 使用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);
}
 
Example #19
Source File: MyBatisDataStore.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void doStart(final String storeName, final Map<String, String> attributes) throws Exception {
  boolean isContentStore = !CONFIG_DATASTORE_NAME.equalsIgnoreCase(storeName);

  dataSource = new HikariDataSource(configureHikari(storeName, attributes));
  Environment environment = new Environment(storeName, new JdbcTransactionFactory(), dataSource);
  sessionFactory = new DefaultSqlSessionFactory(configureMyBatis(environment));

  registerCommonTypeHandlers(isContentStore);

  if (beanLocator != null) {
    // register the appropriate type handlers with the store
    beanLocator.watch(TYPE_HANDLER_KEY,
        isContentStore ? CONTENT_TYPE_HANDLER_MEDIATOR : CONFIG_TYPE_HANDLER_MEDIATOR, this);
  }
}
 
Example #20
Source File: BindingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws Exception {
  DataSource dataSource = BaseDataTest.createBlogDataSource();
  BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL);
  BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA);
  TransactionFactory transactionFactory = new JdbcTransactionFactory();
  Environment environment = new Environment("Production", transactionFactory, dataSource);
  Configuration configuration = new Configuration(environment);
  configuration.setLazyLoadingEnabled(true);
  configuration.getTypeAliasRegistry().registerAlias(Blog.class);
  configuration.getTypeAliasRegistry().registerAlias(Post.class);
  configuration.getTypeAliasRegistry().registerAlias(Author.class);
  configuration.addMapper(BoundBlogMapper.class);
  configuration.addMapper(BoundAuthorMapper.class);
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
 
Example #21
Source File: EnumWithOgnlTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfiguration() {
    UnpooledDataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory();
    Properties dataSourceProperties = new Properties();
    dataSourceProperties.put("driver", "org.hsqldb.jdbcDriver");
    dataSourceProperties.put("url", "jdbc:hsqldb:mem:xml_references");
    dataSourceProperties.put("username", "sa");
    dataSourceFactory.setProperties(dataSourceProperties);
    Environment environment = new Environment("test", new JdbcTransactionFactory(), dataSourceFactory.getDataSource());
    Configuration configuration = new Configuration();
    configuration.setEnvironment(environment);
    configuration.getTypeAliasRegistry().registerAlias(Person.class);
    configuration.addMapper(PersonMapper.class);
    configuration.addMapper(PersonMapper2.class);
    new DefaultSqlSessionFactory(configuration);
}
 
Example #22
Source File: AbstractEngineConfiguration.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public Configuration initMybatisConfiguration(Environment environment, Reader reader, Properties properties) {
    XMLConfigBuilder parser = new XMLConfigBuilder(reader, "", properties);
    Configuration configuration = parser.getConfiguration();

    if (databaseType != null) {
        configuration.setDatabaseId(databaseType);
    }

    configuration.setEnvironment(environment);

    initCustomMybatisMappers(configuration);
    initMybatisTypeHandlers(configuration);
    initCustomMybatisInterceptors(configuration);
    if (isEnableLogSqlExecutionTime()) {
        initMyBatisLogSqlExecutionTimePlugin(configuration);
    }

    configuration = parseMybatisConfiguration(parser);
    return configuration;
}
 
Example #23
Source File: ManyAnnoTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetMessageForEmptyDatabase() throws Exception {
  final Environment environment = new Environment("test", new JdbcTransactionFactory(), BaseDataTest.createBlogDataSource());
  final Configuration config = new Configuration(environment);
  config.addMapper(PostMapper.class);
  final SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config);
  final SqlSession session = factory.openSession();
  
  PostMapper mapper = session.getMapper(PostMapper.class);
  List<AnnoPost> posts = mapper.getPosts(101);


  assertEquals(3,posts.size());
  assertEquals(3,posts.get(0).getTags().size());
  assertEquals(1,posts.get(1).getTags().size());
  assertEquals(0,posts.get(2).getTags().size());

  session.close();

}
 
Example #24
Source File: BindingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws Exception {
  DataSource dataSource = BaseDataTest.createBlogDataSource();
  BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL);
  BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA);
  TransactionFactory transactionFactory = new JdbcTransactionFactory();
  Environment environment = new Environment("Production", transactionFactory, dataSource);
  Configuration configuration = new Configuration(environment);
  configuration.setLazyLoadingEnabled(true);
  configuration.getTypeAliasRegistry().registerAlias(Blog.class);
  configuration.getTypeAliasRegistry().registerAlias(Post.class);
  configuration.getTypeAliasRegistry().registerAlias(Author.class);
  configuration.addMapper(BoundBlogMapper.class);
  configuration.addMapper(BoundAuthorMapper.class);
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
 
Example #25
Source File: ReverseIncludeTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:xmlextref", "sa", "");
  initDb(c);

  Configuration configuration = new Configuration();
  Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
      "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
  configuration.setEnvironment(environment);

  configuration.addMapper(ReverseIncludePersonMapper.class);

  return new SqlSessionFactoryBuilder().build(configuration);
}
 
Example #26
Source File: CountingExecutor.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
public Integer execute() throws SQLException {
	String rawSql = boundSql.getSql().trim();
	String countingSql = dialect.getCountingSQL(rawSql);

	Connection conn = null;
	try{
		Environment evn = ms.getConfiguration().getEnvironment();
		conn = evn.getDataSource().getConnection();
		return new JdbcUtil().counting(conn, countingSql, this);
	}finally{
		JdbcUtil.close(conn);
	}
}
 
Example #27
Source File: MultipleCrossIncludeTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:xmlextref", "sa", "");
  initDb(c);

  Configuration configuration = new Configuration();
  Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
      "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
  configuration.setEnvironment(environment);

  configuration.addMapper(MultipleCrossIncludePersonMapper.class);
  configuration.addMapper(MultipleCrossIncludePetMapper.class);

  return new SqlSessionFactoryBuilder().build(configuration);
}
 
Example #28
Source File: JsonHandlersTestApi.java    From mybatis-gson with MIT License 5 votes vote down vote up
protected static SqlSessionFactory setUpDb(DataSource ds, String initSql) throws SQLException, IOException {
    try (final Connection cnx = ds.getConnection(); final Statement st = cnx.createStatement()) {
        st.execute(getResourceAsString(initSql));
    }

    // Init mybatis
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("jneat", transactionFactory, ds);
    Configuration configuration = new Configuration(environment);
    configuration.getTypeHandlerRegistry().register("com.github.jneat.mybatis");
    configuration.addMapper(JsonMapper.class);

    return new SqlSessionFactoryBuilder().build(configuration);
}
 
Example #29
Source File: TestSuite.java    From mybatis-types with MIT License 5 votes vote down vote up
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);
}
 
Example #30
Source File: MultipleIncludeTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:xmlextref", "sa", "");
  initDb(c);

  Configuration configuration = new Configuration();
  Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
      "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
  configuration.setEnvironment(environment);

  configuration.addMapper(MultipleIncludePersonMapper.class);

  return new SqlSessionFactoryBuilder().build(configuration);
}