Search in sources :

Example 1 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project mybaits-plus-join by Createsequence.

the class DefaultMybatisPlusExtendConfig method sqlSessionFactory.

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource);
    // 插件
    sqlSessionFactory.setPlugins(new DynamicResultInterceptor());
    MybatisConfiguration configuration = new MybatisConfiguration();
    GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
    // 自定义sql注入
    globalConfig.setSqlInjector(new JoinMethodInjector());
    sqlSessionFactory.setConfiguration(configuration);
    // 插件
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    configuration.addInterceptor(interceptor);
    return sqlSessionFactory.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) DynamicResultInterceptor(top.xiajibagao.mybatis.plus.join.interceptor.DynamicResultInterceptor) MybatisConfiguration(com.baomidou.mybatisplus.core.MybatisConfiguration) GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) JoinMethodInjector(top.xiajibagao.mybatis.plus.join.injector.JoinMethodInjector) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project code by lastwhispers.

the class DataSourceProxyConfig method sqlSessionFactoryBean.

@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
    // 订单服务中引入了mybatis-plus,所以要使用特殊的SqlSessionFactoryBean
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    // 代理数据源
    sqlSessionFactoryBean.setDataSource(new DataSourceProxy(dataSource));
    // 生成SqlSessionFactory
    return sqlSessionFactoryBean.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project spring-boot-web by wangchengming666.

the class MybatisPlusConfig method sqlSessionFactory.

@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Autowired DataSource dataSource, @Autowired List<Interceptor> interceptors) throws Exception {
    /**
     */
    String mapperLocations = "classpath:/config/mapper/*Mapper.xml";
    String configLocation = "classpath:/config/mapper/mybatis-config.xml";
    String typeAliasesPackage = "com.web.framework.core.entity.**";
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    // 数据源
    sqlSessionFactory.setDataSource(dataSource);
    // 全局配置
    sqlSessionFactory.setGlobalConfig(globalConfig());
    Interceptor[] desc = new Interceptor[interceptors.size()];
    // 分页插件
    sqlSessionFactory.setPlugins(interceptors.toArray(desc));
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
        // 自动扫描Mapping.xml文件
        sqlSessionFactory.setMapperLocations(resolver.getResources(mapperLocations));
        sqlSessionFactory.setConfigLocation(resolver.getResource(configLocation));
        sqlSessionFactory.setTypeAliasesPackage(typeAliasesPackage);
        return sqlSessionFactory.getObject();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw e;
    }
}
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) PerformanceInterceptor(com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor) Interceptor(org.apache.ibatis.plugin.Interceptor) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with MybatisSqlSessionFactoryBean

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

the class MultiMyBatisConfig method getFactory.

public MybatisSqlSessionFactoryBean getFactory(DataSource dataSource) {
    MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
        factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    MybatisConfiguration configuration = this.properties.getConfiguration();
    if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
        configuration = new MybatisConfiguration();
    }
    if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
        for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
            customizer.customize(configuration);
        }
    }
    factory.setConfiguration(configuration);
    if (this.properties.getConfigurationProperties() != null) {
        factory.setConfigurationProperties(this.properties.getConfigurationProperties());
    }
    if (!ObjectUtils.isEmpty(this.interceptors)) {
        factory.setPlugins(this.interceptors);
    }
    if (this.databaseIdProvider != null) {
        factory.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
        factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (this.properties.getTypeAliasesSuperType() != null) {
        factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
        factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.typeHandlers)) {
        factory.setTypeHandlers(this.typeHandlers);
    }
    Resource[] mapperLocations = this.properties.resolveMapperLocations();
    if (!ObjectUtils.isEmpty(mapperLocations)) {
        factory.setMapperLocations(mapperLocations);
    }
    // TODO 修改源码支持定义 TransactionFactory
    this.getBeanThen(TransactionFactory.class, factory::setTransactionFactory);
    // TODO 对源码做了一定的修改(因为源码适配了老旧的mybatis版本,但我们不需要适配)
    Class<? extends LanguageDriver> defaultLanguageDriver = this.properties.getDefaultScriptingLanguageDriver();
    if (!ObjectUtils.isEmpty(this.languageDrivers)) {
        factory.setScriptingLanguageDrivers(this.languageDrivers);
    }
    Optional.ofNullable(defaultLanguageDriver).ifPresent(factory::setDefaultScriptingLanguageDriver);
    // TODO 自定义枚举包
    if (StringUtils.hasLength(this.properties.getTypeEnumsPackage())) {
        factory.setTypeEnumsPackage(this.properties.getTypeEnumsPackage());
    }
    // TODO 此处必为非 NULL
    GlobalConfig globalConfig = this.properties.getGlobalConfig();
    // TODO 注入填充器
    this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
    // TODO 注入主键生成器
    this.getBeanThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerator(i));
    // TODO 注入sql注入器
    this.getBeanThen(ISqlInjector.class, globalConfig::setSqlInjector);
    // TODO 注入ID生成器
    this.getBeanThen(IdentifierGenerator.class, globalConfig::setIdentifierGenerator);
    // TODO 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
    factory.setGlobalConfig(globalConfig);
    return factory;
}
Also used : ConfigurationCustomizer(com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) MybatisConfiguration(com.baomidou.mybatisplus.core.MybatisConfiguration) GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) Resource(org.springframework.core.io.Resource)

Example 5 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project springboot-templet-start by thedestiny.

the class MybatisPlusConfigPrimaryDatasource method priSqlSessionFactory.

// pre
@Bean("priSqlSessionFactory")
public SqlSessionFactory priSqlSessionFactory(@Qualifier("priDatasource") DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource);
    MybatisConfiguration configuration = new MybatisConfiguration();
    configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    configuration.setJdbcTypeForNull(JdbcType.NULL);
    configuration.setMapUnderscoreToCamelCase(true);
    configuration.setCacheEnabled(false);
    configuration.setDefaultExecutorType(ExecutorType.BATCH);
    sqlSessionFactory.setConfiguration(configuration);
    sqlSessionFactory.setTypeAliasesPackage("com.destiny.hiootamus.entity.primary");
    sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/primary/*.xml"));
    sqlSessionFactory.setPlugins(plusInterceptor);
    sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(true));
    return sqlSessionFactory.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) MybatisConfiguration(com.baomidou.mybatisplus.core.MybatisConfiguration) GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

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