Search in sources :

Example 6 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project poseidon by muggle0.

the class MybatisPlusConfig method createSqlSessionFactoryBean.

@Bean(name = "sqlSessionFactory")
public SqlSessionFactory createSqlSessionFactoryBean(@Qualifier(value = "globalConfig") GlobalConfig configuration) throws Exception {
    log.info("初始化SqlSessionFactory");
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    Interceptor[] interceptor = { new PaginationInterceptor() };
    sqlSessionFactoryBean.setPlugins(interceptor);
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    // sqlSessionFactoryBean.setTypeHandlers(new TypeHandler[]{new MybatisDataHandler()});
    try {
        sqlSessionFactoryBean.setGlobalConfig(configuration);
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));
        sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
        return sqlSessionFactoryBean.getObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sqlSessionFactoryBean.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) Interceptor(org.apache.ibatis.plugin.Interceptor) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 7 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project scaleph by flowerfine.

the class MasterDataSourceConfig method masterSqlSessionFactory.

@Primary
@Bean(MASTER_SQL_SESSION_FACTORY)
public SqlSessionFactory masterSqlSessionFactory() throws Exception {
    MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
    MybatisPlusProperties props = new MybatisPlusProperties();
    props.setMapperLocations(new String[] { MASTER_MAPPER_XML_PATH });
    factoryBean.setMapperLocations(props.resolveMapperLocations());
    MybatisConfiguration configuration = new MybatisConfiguration();
    configuration.setMapUnderscoreToCamelCase(true);
    configuration.setLogImpl(Slf4jImpl.class);
    factoryBean.setConfiguration(configuration);
    factoryBean.setDataSource(masterDataSource());
    factoryBean.setTypeAliasesPackage(MASTER_ENTITY_PACKAGE);
    factoryBean.setPlugins(mybatisPlusInterceptor);
    return factoryBean.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) MybatisConfiguration(com.baomidou.mybatisplus.core.MybatisConfiguration) MybatisPlusProperties(com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties) Primary(org.springframework.context.annotation.Primary) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 8 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project loc-framework by lord-of-code.

the class LocMybatisAutoConfiguration method createSqlSessionFactory.

@Nullable
private SqlSessionFactory createSqlSessionFactory(ConfigurableListableBeanFactory configurableListableBeanFactory, String prefixName, MybatisProperties mybatisProperties) {
    DataSource dataSource = configurableListableBeanFactory.getBean(prefixName + "Ds", DataSource.class);
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    sqlSessionFactoryBean.setVfs(LocSpringBootVFS.class);
    Optional.ofNullable(mybatisProperties.getConfigLocation()).map(this.resourceLoader::getResource).ifPresent(sqlSessionFactoryBean::setConfigLocation);
    org.apache.ibatis.session.Configuration configuration = mybatisProperties.getConfiguration();
    if (configuration == null && !StringUtils.hasText(mybatisProperties.getConfigLocation())) {
        configuration = new org.apache.ibatis.session.Configuration();
    }
    sqlSessionFactoryBean.setConfiguration(configuration);
    Optional.ofNullable(mybatisProperties.getConfigurationProperties()).ifPresent(sqlSessionFactoryBean::setConfigurationProperties);
    Optional.ofNullable(mybatisProperties.getTypeAliasesPackage()).ifPresent(sqlSessionFactoryBean::setTypeAliasesPackage);
    Optional.ofNullable(mybatisProperties.getTypeHandlersPackage()).ifPresent(sqlSessionFactoryBean::setTypeHandlersPackage);
    if (!ObjectUtils.isEmpty(mybatisProperties.resolveMapperLocations())) {
        sqlSessionFactoryBean.setMapperLocations(mybatisProperties.resolveMapperLocations());
    }
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    sqlSessionFactoryBean.setPlugins(new Interceptor[] { paginationInterceptor });
    try {
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
        if (sqlSessionFactory == null) {
            log.error("sqlSessionFactoryBean get object is null");
            return null;
        }
        register(configurableListableBeanFactory, sqlSessionFactory, prefixName + "SessionFactory", prefixName + "Sf");
        if (!Strings.isNullOrEmpty(mybatisProperties.getBasePackage())) {
            createBasePackageScanner((BeanDefinitionRegistry) configurableListableBeanFactory, mybatisProperties.getBasePackage(), prefixName);
        } else {
            createClassPathMapperScanner((BeanDefinitionRegistry) configurableListableBeanFactory, prefixName);
        }
        return sqlSessionFactory;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return null;
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) BeansException(org.springframework.beans.BeansException) DataSource(javax.sql.DataSource) Nullable(org.springframework.lang.Nullable)

Example 9 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project muses by acgist.

the class MssqlConfiguration method mssqlSqlSessionFactory.

@Bean(name = "mssqlSqlSessionFactory")
public SqlSessionFactory mssqlSqlSessionFactory(@Qualifier("mssqlDataSource") DataSource mssqlDataSource) throws Exception {
    final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
    sessionFactory.setDataSource(mssqlDataSource);
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mybatis/mssql/mapper/*.xml"));
    return sessionFactory.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 10 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project citrus by Yiuman.

the class DynamicDataSourceAutoConfiguration method createSqlSessionFactory.

/**
 * 用于动态数据源
 * 根据数据源创建SqlSessionFactory
 *
 * @param dataSource 数据源
 * @return SqlSessionFactory
 * @throws Exception in case of creation errors
 */
private SqlSessionFactory createSqlSessionFactory(DataSource dataSource) throws Exception {
    // 使用 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean
    MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.mybatisPlusProperties.getConfigLocation())) {
        factory.setConfigLocation(this.resourceLoader.getResource(this.mybatisPlusProperties.getConfigLocation()));
    }
    applyConfiguration(factory);
    if (this.mybatisPlusProperties.getConfigurationProperties() != null) {
        factory.setConfigurationProperties(this.mybatisPlusProperties.getConfigurationProperties());
    }
    if (this.databaseIdProvider != null) {
        factory.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.mybatisPlusProperties.getTypeAliasesPackage())) {
        factory.setTypeAliasesPackage(this.mybatisPlusProperties.getTypeAliasesPackage());
    }
    if (this.mybatisPlusProperties.getTypeAliasesSuperType() != null) {
        factory.setTypeAliasesSuperType(this.mybatisPlusProperties.getTypeAliasesSuperType());
    }
    if (StringUtils.hasLength(this.mybatisPlusProperties.getTypeHandlersPackage())) {
        factory.setTypeHandlersPackage(this.mybatisPlusProperties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.typeHandlers)) {
        factory.setTypeHandlers(this.typeHandlers);
    }
    if (!ObjectUtils.isEmpty(this.mybatisPlusProperties.resolveMapperLocations())) {
        factory.setMapperLocations(this.mybatisPlusProperties.resolveMapperLocations());
    }
    Class<? extends LanguageDriver> defaultLanguageDriver = this.mybatisPlusProperties.getDefaultScriptingLanguageDriver();
    if (!ObjectUtils.isEmpty(this.languageDrivers)) {
        factory.setScriptingLanguageDrivers(this.languageDrivers);
    }
    Optional.ofNullable(defaultLanguageDriver).ifPresent(factory::setDefaultScriptingLanguageDriver);
    if (!ObjectUtils.isEmpty(this.interceptors)) {
        factory.setPlugins(this.interceptors);
    }
    // 自定义枚举包
    if (StringUtils.hasLength(this.mybatisPlusProperties.getTypeEnumsPackage())) {
        factory.setTypeEnumsPackage(this.mybatisPlusProperties.getTypeEnumsPackage());
    }
    // 这里每个MybatisSqlSessionFactoryBean对应的都是独立的一个GlobalConfig,不然会出现问题
    GlobalConfig globalConfig = GlobalConfigUtils.defaults();
    // 去除打印
    globalConfig.setBanner(false);
    // 注入填充器
    this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
    // 注入主键生成器
    this.getBeansThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerators(i));
    // 注入sql注入器
    this.getBeanThen(ISqlInjector.class, globalConfig::setSqlInjector);
    // 注入ID生成器
    this.getBeanThen(IdentifierGenerator.class, globalConfig::setIdentifierGenerator);
    // 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
    factory.setGlobalConfig(globalConfig);
    return factory.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig)

Aggregations

MybatisSqlSessionFactoryBean (com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean)29 Bean (org.springframework.context.annotation.Bean)23 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)18 MybatisConfiguration (com.baomidou.mybatisplus.core.MybatisConfiguration)12 GlobalConfig (com.baomidou.mybatisplus.core.config.GlobalConfig)11 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)8 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)7 Resource (org.springframework.core.io.Resource)5 PaginationInterceptor (com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor)4 Interceptor (org.apache.ibatis.plugin.Interceptor)3 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)3 Primary (org.springframework.context.annotation.Primary)3 MybatisPlusProperties (com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties)2 MybatisPlusInterceptor (com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor)2 PaginationInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor)2 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)2 ConfigurationCustomizer (com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer)1 MybatisMapWrapperFactory (com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory)1 PerformanceInterceptor (com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor)1 OptimisticLockerInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor)1