org.apache.ibatis.session.defaults.DefaultSqlSessionFactory Java Examples

The following examples show how to use org.apache.ibatis.session.defaults.DefaultSqlSessionFactory. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithMultipleHandlers.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    DefaultResultHandler handler1 = new DefaultResultHandler();
    DefaultResultHandler handler2 = new DefaultResultHandler();
    AuthorMapperWithMultipleHandlers mapper = sqlSession.getMapper(AuthorMapperWithMultipleHandlers.class);
    mapper.selectAuthor(101, handler1, handler2);
  } finally {
    sqlSession.close();
  }
}
 
Example #7
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoRowBounds() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithRowBounds.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    RowBounds bounds1 = new RowBounds(0, 1);
    RowBounds bounds2 = new RowBounds(0, 1);
    AuthorMapperWithRowBounds mapper = sqlSession.getMapper(AuthorMapperWithRowBounds.class);
    mapper.selectAuthor(101, bounds1, bounds2);
  } finally {
    sqlSession.close();
  }
}
 
Example #8
Source File: ProviderTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseDefaultId() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multidb/MultiDbConfig.xml");
  DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) new SqlSessionFactoryBuilder().build(reader);
  Configuration c = sqlSessionFactory.getConfiguration();
  assertEquals("hsql", c.getDatabaseId());
}
 
Example #9
Source File: ProviderTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseProvider() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multidb/ProviderConfig.xml");
  DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) new SqlSessionFactoryBuilder().build(reader);
  Configuration c = sqlSessionFactory.getConfiguration();
  assertEquals("translated", c.getDatabaseId());
}
 
Example #10
Source File: Utils.java    From ClosureTableCateogryStore with MIT License 5 votes vote down vote up
public static SqlSession createSqlSession(DataSource dataSource) {
	TransactionFactory transactionFactory = new JdbcTransactionFactory();
	Environment environment = new Environment("test", transactionFactory, dataSource);

	Configuration config = new Configuration();
	config.setCacheEnabled(false);
	config.addMapper(CategoryMapper.class);
	config.setEnvironment(environment);

	SqlSessionFactory sessionFactory = new DefaultSqlSessionFactory(config);
	return sessionFactory.openSession();
}
 
Example #11
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithMultipleHandlers.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    DefaultResultHandler handler1 = new DefaultResultHandler();
    DefaultResultHandler handler2 = new DefaultResultHandler();
    AuthorMapperWithMultipleHandlers mapper = sqlSession.getMapper(AuthorMapperWithMultipleHandlers.class);
    mapper.selectAuthor(101, handler1, handler2);
  } finally {
    sqlSession.close();
  }
}
 
Example #12
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoRowBounds() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithRowBounds.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    RowBounds bounds1 = new RowBounds(0, 1);
    RowBounds bounds2 = new RowBounds(0, 1);
    AuthorMapperWithRowBounds mapper = sqlSession.getMapper(AuthorMapperWithRowBounds.class);
    mapper.selectAuthor(101, bounds1, bounds2);
  } finally {
    sqlSession.close();
  }
}
 
Example #13
Source File: ProviderTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseDefaultId() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multidb/MultiDbConfig.xml");
  DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) new SqlSessionFactoryBuilder().build(reader);
  Configuration c = sqlSessionFactory.getConfiguration();
  assertEquals("hsql", c.getDatabaseId());
}
 
Example #14
Source File: ProviderTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseProvider() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multidb/ProviderConfig.xml");
  DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) new SqlSessionFactoryBuilder().build(reader);
  Configuration c = sqlSessionFactory.getConfiguration();
  assertEquals("translated", c.getDatabaseId());
}
 
Example #15
Source File: SqlSessionFactoryBuilder.java    From mybaties with Apache License 2.0 4 votes vote down vote up
public SqlSessionFactory build(Configuration config) {
  return new DefaultSqlSessionFactory(config);
}
 
Example #16
Source File: JxSqlSessionFactoryTest.java    From sinavi-jfw with Apache License 2.0 4 votes vote down vote up
@Before
public void instantiate() {
    factory = new JxSqlSessionFactory(new DefaultSqlSessionFactory(null));
}
 
Example #17
Source File: SqlSessionFactoryBuilder.java    From mybatis with Apache License 2.0 4 votes vote down vote up
public SqlSessionFactory build(Configuration config) {
  return new DefaultSqlSessionFactory(config);
}
 
Example #18
Source File: TestMybatis.java    From ace with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) throws Exception {

        TestH2 h2 = new TestH2();
        // 开始服务
        h2.startServer();
        h2.testH2();

        Class.forName("org.h2.Driver");
        JdbcDataSource JdbcDataSource=new JdbcDataSource();
        JdbcDataSource.setUrl("jdbc:h2:./test");
        JdbcDataSource.setUser("sa");
        JdbcDataSource.setPassword("");


        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("Production", transactionFactory, JdbcDataSource);

        Configuration configuration = new Configuration(environment);
        configuration.addMapper(PersonMapper.class);
        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);


        SqlSession sqlSession = sqlSessionFactory.openSession();

        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person=new Person();
        person.setId(1);
        Person person1= personMapper.queryByPrimaryKey(person);
        System.out.println(person1);

        h2.stopServer();


    }