Search in sources :

Example 1 with SqlSessionFactoryBean

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

Example 2 with SqlSessionFactoryBean

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

Example 3 with SqlSessionFactoryBean

use of cn.taketoday.orm.mybatis.SqlSessionFactoryBean in project today-framework by TAKETODAY.

the class MybatisAutoConfiguration method sqlSessionFactory.

@MissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setVfs(ResourceLoaderVFS.class);
    if (StringUtils.hasText(properties.getConfigLocation())) {
        factory.setConfigLocation(resourceLoader.getResource(properties.getConfigLocation()));
    }
    applyConfiguration(factory);
    if (properties.getConfigurationProperties() != null) {
        factory.setConfigurationProperties(properties.getConfigurationProperties());
    }
    if (StringUtils.isNotEmpty(properties.getTypeAliasesPackage())) {
        factory.setTypeAliasesPackage(properties.getTypeAliasesPackage());
    }
    if (properties.getTypeAliasesSuperType() != null) {
        factory.setTypeAliasesSuperType(properties.getTypeAliasesSuperType());
    }
    if (StringUtils.isNotEmpty(properties.getTypeHandlersPackage())) {
        factory.setTypeHandlersPackage(properties.getTypeHandlersPackage());
    }
    Resource[] mapperLocations = properties.resolveMapperLocations();
    if (ObjectUtils.isNotEmpty(mapperLocations)) {
        factory.setMapperLocations(mapperLocations);
    }
    interceptors.ifAvailable(factory::setPlugins);
    typeHandlers.ifAvailable(factory::setTypeHandlers);
    databaseIdProvider.ifAvailable(factory::setDatabaseIdProvider);
    Class<? extends LanguageDriver> defaultLanguageDriver = properties.getDefaultScriptingLanguageDriver();
    factory.setDefaultScriptingLanguageDriver(defaultLanguageDriver);
    languageDrivers.ifAvailable(languageDrivers -> {
        factory.setScriptingLanguageDrivers(languageDrivers);
        if (defaultLanguageDriver == null && languageDrivers.length == 1) {
            // override
            factory.setDefaultScriptingLanguageDriver(languageDrivers[0].getClass());
        }
    });
    applySqlSessionFactoryBeanCustomizers(factory);
    return factory.getObject();
}
Also used : Resource(cn.taketoday.core.io.Resource) SqlSessionFactoryBean(cn.taketoday.orm.mybatis.SqlSessionFactoryBean) ConditionalOnMissingBean(cn.taketoday.context.condition.ConditionalOnMissingBean) MissingBean(cn.taketoday.context.annotation.MissingBean)

Aggregations

SqlSessionFactoryBean (cn.taketoday.orm.mybatis.SqlSessionFactoryBean)3 AbstractMyBatisTodayTest (cn.taketoday.orm.mybatis.AbstractMyBatisTodayTest)2 SqlSessionTemplate (cn.taketoday.orm.mybatis.SqlSessionTemplate)2 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)2 Test (org.junit.jupiter.api.Test)2 MissingBean (cn.taketoday.context.annotation.MissingBean)1 ConditionalOnMissingBean (cn.taketoday.context.condition.ConditionalOnMissingBean)1 Resource (cn.taketoday.core.io.Resource)1 MyBatisSystemException (cn.taketoday.orm.mybatis.MyBatisSystemException)1