use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class ConfigurationTest method applyPropertyValueOnXmlConfiguration.
@Test
public void applyPropertyValueOnXmlConfiguration() throws IOException {
Properties props = new Properties();
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
props.setProperty("settings.jdbcTypeForNull", JdbcType.CHAR.name());
props.setProperty("db.name", "global_variables_defaults_custom");
props.setProperty("productName.hsql", "Hsql");
props.setProperty("objectFactory.name", "custom");
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
Configuration configuration = factory.getConfiguration();
Assert.assertThat(configuration.getJdbcTypeForNull(), Is.is(JdbcType.CHAR));
Assert.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl(), Is.is("jdbc:hsqldb:mem:global_variables_defaults_custom"));
Assert.assertThat(configuration.getDatabaseId(), IsNull.nullValue());
Assert.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"), Is.is("custom"));
}
use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class InsertTest method testInsertMappedBatch.
// Not supported yet in PostgreSQL
@Ignore
@Test
public void testInsertMappedBatch() throws Exception {
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
Name name = new Name();
name.setFirstName("Fred");
name.setLastName("Flintstone");
mapper.insertNameMapped(name);
Name name2 = new Name();
name2.setFirstName("Wilma");
name2.setLastName("Flintstone");
mapper.insertNameMapped(name2);
List<BatchResult> batchResults = sqlSession.flushStatements();
assertNotNull(name.getId());
assertNotNull(name2.getId());
assertEquals(1, batchResults.size());
} finally {
sqlSession.close();
}
}
use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class InlineCollectionWithDotTest method openSession.
public void openSession(String aConfig) throws Exception {
final String resource = "org/apache/ibatis/submitted/inline_association_with_dot/ibatis-" + aConfig + ".xml";
Reader batisConfigReader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory;
try {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(batisConfigReader);
} catch (Exception anException) {
throw new RuntimeException("Mapper configuration failed, expected this to work: " + anException.getMessage(), anException);
}
SqlSession session = sqlSessionFactory.openSession();
Connection conn = session.getConnection();
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.setErrorLogWriter(null);
Reader createScriptReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/inline_association_with_dot/create.sql");
runner.runScript(createScriptReader);
sqlSession = sqlSessionFactory.openSession();
}
use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class CustomizationTest method applyDefaultValueWhenCustomizeDefaultValueSeparator.
@Test
public void applyDefaultValueWhenCustomizeDefaultValueSeparator() throws IOException {
Properties props = new Properties();
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config-custom-separator.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
Configuration configuration = factory.getConfiguration();
configuration.addMapper(CustomDefaultValueSeparatorMapper.class);
SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(CustomDefaultValueSeparatorMapper.class.getName()));
Assert.assertThat(configuration.getJdbcTypeForNull(), Is.is(JdbcType.NULL));
Assert.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl(), Is.is("jdbc:hsqldb:mem:global_variables_defaults"));
Assert.assertThat(configuration.getDatabaseId(), Is.is("hsql"));
Assert.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"), Is.is("default"));
Assert.assertThat(cache.getName(), Is.is("default"));
SqlSession sqlSession = factory.openSession();
try {
CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class);
Assert.assertThat(mapper.selectValue(null), Is.is("default"));
} finally {
sqlSession.close();
}
}
use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class XmlMapperTest method applyPropertyValueOnXmlMapper.
@Test
public void applyPropertyValueOnXmlMapper() throws IOException {
Properties props = new Properties();
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
props.setProperty("ping.sql", "SELECT 'Hi' FROM INFORMATION_SCHEMA.SYSTEM_USERS");
props.setProperty("cache.name", "custom");
props.setProperty("select.columns", "'5555'");
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
Configuration configuration = factory.getConfiguration();
configuration.addMapper(XmlMapper.class);
SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(XmlMapper.class.getName()));
Assert.assertThat(cache.getName(), Is.is("custom"));
SqlSession sqlSession = factory.openSession();
try {
XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
Assert.assertThat(mapper.ping(), Is.is("Hi"));
Assert.assertThat(mapper.selectOne(), Is.is("1"));
Assert.assertThat(mapper.selectFromVariable(), Is.is("5555"));
} finally {
sqlSession.close();
}
}
Aggregations