Search in sources :

Example 11 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project java-apply by javachengwc.

the class DbConfig method sqlSessionFactory.

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/sqlmap/**/*.xml"));
    factoryBean.setTypeAliasesPackage("com.micro.course.model");
    SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
    sqlSessionFactory.getConfiguration().addInterceptor(paginationInterceptor());
    sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
    return sqlSessionFactory;
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 12 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project java-apply by javachengwc.

the class DbConfig method sqlSessionFactory.

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/sqlmap/**/*.xml"));
    factoryBean.setTypeAliasesPackage("com.micro.store.model");
    SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
    sqlSessionFactory.getConfiguration().addInterceptor(paginationInterceptor());
    sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
    return sqlSessionFactory;
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 13 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project java-apply by javachengwc.

the class DbConfig method sqlSessionFactory.

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/sqlmap/**/*.xml"));
    factoryBean.setTypeAliasesPackage("com.micro.user.model");
    SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
    // sqlSessionFactory.getConfiguration().addInterceptor(new PaginationInterceptor());
    sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
    return sqlSessionFactory;
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 14 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project lamp-util by zuihou.

the class BaseDatabaseConfiguration method sqlSessionFactory.

/**
 * 构建sqlSession工厂
 *
 * @param dataSource 数据源
 * @return sqlSession工厂
 * @throws Exception 异常
 */
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    // TODO 使用 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean
    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()));
    }
    applyConfiguration(factory);
    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.getBeansThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerators(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.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) Resource(org.springframework.core.io.Resource)

Example 15 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project mybatis-plus-samples by baomidou.

the class MybatisPlusConfig method sqlSessionFactory.

@Bean("mybatisSqlSession")
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig) throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    /* 数据源 */
    sqlSessionFactory.setDataSource(dataSource);
    /* 枚举扫描 */
    sqlSessionFactory.setTypeEnumsPackage("com.baomidou.mybatisplus.samples.mysql.enums");
    /* xml扫描 */
    sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*.xml"));
    /* 扫描 typeHandler */
    // sqlSessionFactory.setTypeHandlersPackage("com.baomidou.mybatisplus.samples.mysql.type");
    MybatisConfiguration configuration = new MybatisConfiguration();
    configuration.setJdbcTypeForNull(JdbcType.NULL);
    /* 驼峰转下划线 */
    configuration.setMapUnderscoreToCamelCase(true);
    MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
    mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    sqlSessionFactory.setPlugins(mybatisPlusInterceptor);
    /* map 下划线转驼峰 */
    configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
    sqlSessionFactory.setConfiguration(configuration);
    /* 自动填充插件 */
    globalConfig.setMetaObjectHandler(new MysqlMetaObjectHandler());
    sqlSessionFactory.setGlobalConfig(globalConfig);
    return sqlSessionFactory.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) MybatisConfiguration(com.baomidou.mybatisplus.core.MybatisConfiguration) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MybatisMapWrapperFactory(com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory) OptimisticLockerInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor) 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