use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-framework by TAKETODAY.
the class MapperFactoryBeanTest method testWithNonTodayTransactionFactory.
// MapperFactoryBeans should be usable outside of TX, as long as a there is no active
// transaction
@Test
void testWithNonTodayTransactionFactory() throws Exception {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
Environment nonToday = new Environment("non-today", new JdbcTransactionFactory(), dataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonToday);
try {
find(new SqlSessionTemplate(sqlSessionFactory));
// SqlSessionTemplate autocommits
assertCommit();
assertCommitSession();
assertSingleConnection();
assertExecuteCount(1);
} finally {
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-framework by TAKETODAY.
the class MapperFactoryBeanTest method testNonTodayWithTx.
// similar to testNonTodayTxFactoryNonTodayDSWithTx() in MyBatisTodayTest
@Test
void testNonTodayWithTx() throws Exception {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
MockDataSource mockDataSource = new MockDataSource();
mockDataSource.setupConnection(createMockConnection());
Environment nonToday = new Environment("non-today", new JdbcTransactionFactory(), mockDataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonToday);
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
TransactionStatus status;
try {
status = txManager.getTransaction(new DefaultTransactionDefinition());
find(sqlSessionTemplate);
txManager.commit(status);
// txManager still uses original connection
assertCommit();
assertSingleConnection();
// SqlSessionTemplate uses its own connection
MockConnection mockConnection = (MockConnection) mockDataSource.getConnection();
assertThat(mockConnection.getNumberCommits()).as("should call commit on Connection").isEqualTo(1);
assertThat(mockConnection.getNumberRollbacks()).as("should not call rollback on Connection").isEqualTo(0);
assertCommitSession();
} finally {
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-framework by TAKETODAY.
the class SqlSessionDaoSupportTest method testWithBothFactoryAndTemplate.
@Test
void testWithBothFactoryAndTemplate() {
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
sqlSessionDaoSupport.setSqlSessionTemplate(sessionTemplate);
sqlSessionDaoSupport.setSqlSessionFactory(sqlSessionFactory);
sqlSessionDaoSupport.afterPropertiesSet();
assertThat(sqlSessionDaoSupport.getSqlSession()).as("should ignore the Factory").isEqualTo(sessionTemplate);
}
use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-infrastructure by TAKETODAY.
the class SqlSessionDaoSupportTest method testWithBothFactoryAndTemplate.
@Test
void testWithBothFactoryAndTemplate() {
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
sqlSessionDaoSupport.setSqlSessionTemplate(sessionTemplate);
sqlSessionDaoSupport.setSqlSessionFactory(sqlSessionFactory);
sqlSessionDaoSupport.afterPropertiesSet();
assertThat(sqlSessionDaoSupport.getSqlSession()).as("should ignore the Factory").isEqualTo(sessionTemplate);
}
use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-infrastructure 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);
}
Aggregations