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;
}
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;
}
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();
}
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();
}
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;
}
Aggregations