Search in sources :

Example 1 with SqlSessionTemplate

use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-infrastructure 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();
}
Also used : SqlSessionTemplate(cn.taketoday.orm.mybatis.SqlSessionTemplate) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Test(org.junit.jupiter.api.Test)

Example 2 with SqlSessionTemplate

use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-infrastructure 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);
}
Also used : SqlSessionTemplate(cn.taketoday.orm.mybatis.SqlSessionTemplate) AbstractMyBatisTest(cn.taketoday.orm.mybatis.AbstractMyBatisTest) Test(org.junit.jupiter.api.Test)

Example 3 with SqlSessionTemplate

use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-infrastructure 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);
    }
}
Also used : SqlSessionTemplate(cn.taketoday.orm.mybatis.SqlSessionTemplate) Environment(org.apache.ibatis.mapping.Environment) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) AbstractMyBatisTest(cn.taketoday.orm.mybatis.AbstractMyBatisTest) Test(org.junit.jupiter.api.Test)

Example 4 with SqlSessionTemplate

use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-infrastructure 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);
    }
}
Also used : SqlSessionTemplate(cn.taketoday.orm.mybatis.SqlSessionTemplate) DefaultTransactionDefinition(cn.taketoday.transaction.support.DefaultTransactionDefinition) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Environment(org.apache.ibatis.mapping.Environment) TransactionStatus(cn.taketoday.transaction.TransactionStatus) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) MockConnection(com.mockrunner.mock.jdbc.MockConnection) AbstractMyBatisTest(cn.taketoday.orm.mybatis.AbstractMyBatisTest) Test(org.junit.jupiter.api.Test)

Example 5 with SqlSessionTemplate

use of cn.taketoday.orm.mybatis.SqlSessionTemplate in project today-infrastructure 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);
}
Also used : SqlSessionTemplate(cn.taketoday.orm.mybatis.SqlSessionTemplate) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) SqlSessionFactoryBean(cn.taketoday.orm.mybatis.SqlSessionFactoryBean) AbstractMyBatisTest(cn.taketoday.orm.mybatis.AbstractMyBatisTest) Test(org.junit.jupiter.api.Test)

Aggregations

SqlSessionTemplate (cn.taketoday.orm.mybatis.SqlSessionTemplate)16 Test (org.junit.jupiter.api.Test)16 AbstractMyBatisTest (cn.taketoday.orm.mybatis.AbstractMyBatisTest)12 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)8 SqlSessionFactoryBean (cn.taketoday.orm.mybatis.SqlSessionFactoryBean)4 Environment (org.apache.ibatis.mapping.Environment)4 DefaultSqlSessionFactory (org.apache.ibatis.session.defaults.DefaultSqlSessionFactory)4 JdbcTransactionFactory (org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)4 RuntimeBeanReference (cn.taketoday.beans.factory.config.RuntimeBeanReference)2 MyBatisSystemException (cn.taketoday.orm.mybatis.MyBatisSystemException)2 MapperScannerConfigurer (cn.taketoday.orm.mybatis.mapper.MapperScannerConfigurer)2 TransactionStatus (cn.taketoday.transaction.TransactionStatus)2 DefaultTransactionDefinition (cn.taketoday.transaction.support.DefaultTransactionDefinition)2 MockConnection (com.mockrunner.mock.jdbc.MockConnection)2 MockDataSource (com.mockrunner.mock.jdbc.MockDataSource)2