Search in sources :

Example 6 with GlobalConfig

use of com.baomidou.mybatisplus.core.config.GlobalConfig in project mybatis-plus-samples by baomidou.

the class MybatisPlusConfig method globalConfig.

@Bean
public GlobalConfig globalConfig() {
    GlobalConfig conf = new GlobalConfig();
    conf.setDbConfig(new GlobalConfig.DbConfig().setColumnFormat("`%s`"));
    DefaultSqlInjector logicSqlInjector = new DefaultSqlInjector() {

        /**
         * 注入自定义全局方法
         */
        @Override
        public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
            List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
            // 不要逻辑删除字段, 不要乐观锁字段, 不要填充策略是 UPDATE 的字段
            methodList.add(new InsertBatchSomeColumn(t -> !t.isLogicDelete() && !t.isVersion() && t.getFieldFill() != FieldFill.UPDATE));
            // 不要填充策略是 INSERT 的字段, 不要字段名是 column4 的字段
            methodList.add(new AlwaysUpdateSomeColumnById(t -> t.getFieldFill() != FieldFill.INSERT && !t.getProperty().equals("column4")));
            return methodList;
        }
    };
    conf.setSqlInjector(logicSqlInjector);
    return conf;
}
Also used : OptimisticLockerInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor) InsertBatchSomeColumn(com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) MapperScan(org.mybatis.spring.annotation.MapperScan) Configuration(org.springframework.context.annotation.Configuration) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MybatisConfiguration(com.baomidou.mybatisplus.core.MybatisConfiguration) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) List(java.util.List) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) MybatisMapWrapperFactory(com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) DataSource(javax.sql.DataSource) FieldFill(com.baomidou.mybatisplus.annotation.FieldFill) AlwaysUpdateSomeColumnById(com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById) JdbcType(org.apache.ibatis.type.JdbcType) Bean(org.springframework.context.annotation.Bean) DefaultSqlInjector(com.baomidou.mybatisplus.core.injector.DefaultSqlInjector) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) DefaultSqlInjector(com.baomidou.mybatisplus.core.injector.DefaultSqlInjector) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) InsertBatchSomeColumn(com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn) AlwaysUpdateSomeColumnById(com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 7 with GlobalConfig

use of com.baomidou.mybatisplus.core.config.GlobalConfig in project mybatis-plus-samples by baomidou.

the class MpConfig method globalConfiguration.

@Bean
public GlobalConfig globalConfiguration() {
    GlobalConfig conf = new GlobalConfig();
    conf.setDbConfig(new GlobalConfig.DbConfig().setKeyGenerators(Arrays.asList(// h2 1.x 的写法(默认 2.x 的写法)
    new IKeyGenerator() {

        @Override
        public String executeSql(String incrementerName) {
            return "select " + incrementerName + ".nextval";
        }

        @Override
        public DbType dbType() {
            return DbType.POSTGRE_SQL;
        }
    })));
    return conf;
}
Also used : GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) IKeyGenerator(com.baomidou.mybatisplus.core.incrementer.IKeyGenerator) DbType(com.baomidou.mybatisplus.annotation.DbType) Bean(org.springframework.context.annotation.Bean)

Example 8 with GlobalConfig

use of com.baomidou.mybatisplus.core.config.GlobalConfig 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)

Example 9 with GlobalConfig

use of com.baomidou.mybatisplus.core.config.GlobalConfig 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 10 with GlobalConfig

use of com.baomidou.mybatisplus.core.config.GlobalConfig in project citrus by Yiuman.

the class DynamicSqlSessionTemplate method getConfiguration.

@Override
public Configuration getConfiguration() {
    Configuration configuration = getSqlSessionFactory().getConfiguration();
    // 不然会出现 Invalid bound statement (not found) 异常
    if (sqlSessionFactory != defaultTargetSqlSessionFactory && CollectionUtils.isEmpty(configuration.getMappedStatements())) {
        // 可能会有缓存先清掉
        GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
        globalConfig.getMapperRegistryCache().clear();
        defaultTargetSqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().forEach(configuration::addMapper);
    }
    return configuration;
}
Also used : GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig)

Aggregations

GlobalConfig (com.baomidou.mybatisplus.core.config.GlobalConfig)21 MybatisSqlSessionFactoryBean (com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean)13 Bean (org.springframework.context.annotation.Bean)12 MybatisConfiguration (com.baomidou.mybatisplus.core.MybatisConfiguration)10 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)7 Resource (org.springframework.core.io.Resource)4 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)4 DbType (com.baomidou.mybatisplus.annotation.DbType)2 IKeyGenerator (com.baomidou.mybatisplus.core.incrementer.IKeyGenerator)2 MybatisPlusInterceptor (com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor)2 PaginationInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor)2 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)2 FieldFill (com.baomidou.mybatisplus.annotation.FieldFill)1 ConfigurationCustomizer (com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer)1 DefaultIdentifierGenerator (com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator)1 AbstractMethod (com.baomidou.mybatisplus.core.injector.AbstractMethod)1 DefaultSqlInjector (com.baomidou.mybatisplus.core.injector.DefaultSqlInjector)1 SqlRunnerInjector (com.baomidou.mybatisplus.core.injector.SqlRunnerInjector)1 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)1 MybatisMapWrapperFactory (com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory)1