use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-framework by TAKETODAY.
the class MybatisAutoConfigurationTests method testAutoScanWithInjectSqlSessionOnMapperScanIsFalse.
@Test
void testAutoScanWithInjectSqlSessionOnMapperScanIsFalse() {
TestPropertyValues.of("mybatis.inject-sql-session-on-mapper-scan:false").applyTo(this.context);
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisBootMapperScanAutoConfiguration.class, MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
SqlSessionFactory sqlSessionFactory = this.context.getBean(SqlSessionFactory.class);
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
assertThat(this.context.getBean(SqlSessionTemplate.class).getExecutorType()).isEqualTo(ExecutorType.SIMPLE);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().isMapUnderscoreToCamelCase()).isFalse();
this.context.getBean(CityMapper.class);
assertThat(sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers()).hasSize(1);
assertThat(this.context.getBeanDefinition("cityMapper").getPropertyValues().getPropertyValue("sqlSessionTemplate")).isNull();
assertThat(this.context.getBeanDefinition("cityMapper").getPropertyValues().getPropertyValue("sqlSessionFactory")).isNull();
}
use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-framework by TAKETODAY.
the class MapperFactoryBeanTest method testAddToConfigTrue.
@Test
void testAddToConfigTrue() throws Exception {
// the default SqlSessionFactory in AbstractMyBatisTodayTest is created with an explicitly set
// MapperLocations list, so create a new factory here that tests auto-loading the config
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDatabaseIdProvider(null);
// mapperLocations properties defaults to null
factoryBean.setDataSource(dataSource);
factoryBean.setPlugins(executorInterceptor);
SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
find(new SqlSessionTemplate(sqlSessionFactory), true);
// SqlSesssionTemplate autocommits
assertCommit();
assertSingleConnection();
assertExecuteCount(1);
}
use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-framework by TAKETODAY.
the class MapperFactoryBeanTest method testAddToConfigFalse.
// will fail because TestDao's mapper config is never loaded
@Test
void testAddToConfigFalse() throws Throwable {
try {
// the default SqlSessionFactory in AbstractMyBatisTodayTest is created with an explicitly
// set MapperLocations list, so create a new factory here that tests auto-loading the
// config
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// mapperLocations properties defaults to null
factoryBean.setDataSource(dataSource);
SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
assertThrows(org.apache.ibatis.binding.BindingException.class, () -> find(new SqlSessionTemplate(sqlSessionFactory), false));
// fail("TestDao's mapper xml should not be loaded");
} catch (MyBatisSystemException mbse) {
// unwrap exception so the exact MyBatis exception can be tested
throw mbse.getCause();
} finally {
// connection not used; force close to avoid failing in validateConnectionClosed()
connection.close();
}
}
use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-framework by TAKETODAY.
the class MybatisAutoConfigurationTests method testAutoScanWithDefault.
@Test
void testAutoScanWithDefault() {
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisBootMapperScanAutoConfiguration.class, MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
SqlSessionFactory sqlSessionFactory = this.context.getBean(SqlSessionFactory.class);
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
assertThat(this.context.getBean(SqlSessionTemplate.class).getExecutorType()).isEqualTo(ExecutorType.SIMPLE);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().isMapUnderscoreToCamelCase()).isFalse();
this.context.getBean(CityMapper.class);
assertThat(sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers()).hasSize(1);
assertThat(((RuntimeBeanReference) this.context.getBeanDefinition("cityMapper").getPropertyValues().getPropertyValue("sqlSessionTemplate")).getBeanName()).isEqualTo("sqlSessionTemplate");
assertThat(context.getBeanDefinition(CollectionUtils.firstElement(context.getBeanNamesForType(MapperScannerConfigurer.class))).getRole()).isEqualTo(BeanDefinition.ROLE_INFRASTRUCTURE);
}
use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-framework by TAKETODAY.
the class SqlSessionDaoSupportTest method testWithSqlSessionTemplate.
@Test
void testWithSqlSessionTemplate() {
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
sqlSessionDaoSupport.setSqlSessionTemplate(sessionTemplate);
sqlSessionDaoSupport.afterPropertiesSet();
assertThat(sqlSessionDaoSupport.getSqlSession()).as("should store the Template").isEqualTo(sessionTemplate);
}
Aggregations