use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class CustomizationTest method applyPropertyValueWhenCustomizeDefaultValueSeparator.
@Test
public void applyPropertyValueWhenCustomizeDefaultValueSeparator() throws IOException {
Properties props = new Properties();
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");
props.setProperty("settings:jdbcTypeForNull", JdbcType.CHAR.name());
props.setProperty("db:name", "global_variables_defaults_custom");
props.setProperty("productName:hsql", "Hsql");
props.setProperty("objectFactory:name", "customObjectFactory");
props.setProperty("cache:name", "customCache");
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.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("customObjectFactory"));
Assert.assertThat(cache.getName(), Is.is("customCache"));
SqlSession sqlSession = factory.openSession();
try {
CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class);
Assert.assertThat(mapper.selectValue("3333"), Is.is("3333"));
} finally {
sqlSession.close();
}
}
use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class XmlMapperTest method applyDefaultValueOnXmlMapper.
@Test
public void applyDefaultValueOnXmlMapper() throws IOException {
Properties props = new Properties();
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
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("default"));
SqlSession sqlSession = factory.openSession();
try {
XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
Assert.assertThat(mapper.ping(), Is.is("Hello"));
Assert.assertThat(mapper.selectOne(), Is.is("1"));
Assert.assertThat(mapper.selectFromVariable(), Is.is("9999"));
} finally {
sqlSession.close();
}
}
use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class ManyAnnoTest method testGetMessageForEmptyDatabase.
@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();
}
use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class AnnotationMapperTest method applyDefaultValueOnAnnotationMapper.
@Test
public void applyDefaultValueOnAnnotationMapper() throws IOException {
Properties props = new Properties();
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
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(AnnotationMapper.class);
SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(AnnotationMapper.class.getName()));
Assert.assertThat(cache.getName(), Is.is("default"));
SqlSession sqlSession = factory.openSession();
try {
AnnotationMapper mapper = sqlSession.getMapper(AnnotationMapper.class);
Assert.assertThat(mapper.ping(), Is.is("Hello"));
} finally {
sqlSession.close();
}
}
use of org.apache.ibatis.session.SqlSessionFactory in project mybatis-3 by mybatis.
the class InsertTest method testInsertAnnotated.
@Test
public void testInsertAnnotated() throws Exception {
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
Name name = new Name();
name.setFirstName("Fred");
name.setLastName("Flintstone");
int rows = mapper.insertNameAnnotated(name);
assertNotNull(name.getId());
assertEquals(1, rows);
} finally {
sqlSession.close();
}
}
Aggregations