io.github.nichetoolkit.mybatis.MybatisSqlProviderHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-toolkit-starter Show documentation
Show all versions of mybatis-toolkit-starter Show documentation
mybatis toolkit starter project for Spring Boot
package io.github.nichetoolkit.mybatis;
import io.github.nichetoolkit.mybatis.configure.MybatisTableProperties;
import io.github.nichetoolkit.mybatis.enums.DatabaseType;
import io.github.nichetoolkit.mybatis.error.MybatisProviderLackError;
import io.github.nichetoolkit.rest.RestOptional;
import io.github.nichetoolkit.rest.util.GeneralUtils;
import io.github.nichetoolkit.rest.util.OptionalUtils;
import io.github.nichetoolkit.rice.ServiceIntend;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.support.SpringFactoriesLoader;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* MybatisSqlProviderHolder
* The mybatis sql provider holder class.
* @author Cyan ([email protected])
* @see io.github.nichetoolkit.rice.ServiceIntend
* @see lombok.extern.slf4j.Slf4j
* @see lombok.Setter
* @since Jdk1.8
*/
@Slf4j
@Setter
public class MybatisSqlProviderHolder implements ServiceIntend {
/**
* SQL_PROVIDER_CACHES
* {@link java.util.Map} The constant SQL_PROVIDER_CACHES
field.
* @see java.util.Map
*/
private static final Map> SQL_PROVIDER_CACHES = new ConcurrentHashMap<>(DatabaseType.values().length);
/**
* tableProperties
* {@link io.github.nichetoolkit.mybatis.configure.MybatisTableProperties} The tableProperties
field.
* @see io.github.nichetoolkit.mybatis.configure.MybatisTableProperties
* @see javax.annotation.Resource
*/
@Resource
private MybatisTableProperties tableProperties;
/**
* sqlProviders
* {@link java.util.List} The sqlProviders
field.
* @see java.util.List
* @see javax.annotation.Resource
*/
@Resource
private List sqlProviders;
/**
* INSTANCE
* {@link io.github.nichetoolkit.mybatis.MybatisSqlProviderHolder} The constant INSTANCE
field.
*/
private static MybatisSqlProviderHolder INSTANCE = null;
@Override
public void afterPropertiesSet() {
INSTANCE = this;
}
/**
* instance
* The instance method.
* @return {@link io.github.nichetoolkit.mybatis.MybatisSqlProviderHolder} The instance return object is MybatisSqlProviderHolder
type.
*/
public static MybatisSqlProviderHolder instance() {
return INSTANCE;
}
@Override
public void afterAutowirePropertiesSet() {
loadOfSqlProviders();
defaultSqlProviders();
}
/**
* loadOfSqlProviders
* The load of sql providers method.
*/
private void loadOfSqlProviders() {
List sqlProvidersOfServiceLoader = SpringFactoriesLoader.loadFactories(MybatisSqlProvider.class, null);
if (GeneralUtils.isNotEmpty(sqlProvidersOfServiceLoader)) {
this.sqlProviders.addAll(sqlProvidersOfServiceLoader);
}
if (GeneralUtils.isNotEmpty(this.sqlProviders)) {
this.sqlProviders.stream().distinct().collect(Collectors.groupingBy(MybatisSqlProvider::databaseType))
.forEach((databaseType, sqlProviderList) -> {
if (SQL_PROVIDER_CACHES.containsKey(databaseType)) {
SQL_PROVIDER_CACHES.get(databaseType).addAll(sqlProviderList);
} else {
SQL_PROVIDER_CACHES.put(databaseType, sqlProviderList);
}
});
}
String messageOfSqlProviders = "the sql providers can not be loaded from spring factories loader or spring beans";
OptionalUtils.ofEmptyError(RestOptional.ofEmptyable(SQL_PROVIDER_CACHES), messageOfSqlProviders, log, MybatisProviderLackError::new);
}
/**
* defaultSqlProviders
* The default sql providers method.
*/
private void defaultSqlProviders() {
DatabaseType databaseType = defaultDatabaseType();
List sqlProviders = defaultSqlProviders(databaseType);
String messageOfDatabaseType = "the sql providers can not found, maybe it is unsupported with '" + databaseType.name() + "' type";
OptionalUtils.ofEmptyError(RestOptional.ofEmptyable(sqlProviders), messageOfDatabaseType, log,MybatisProviderLackError::new);
}
/**
* defaultDatabaseType
* The default database type method.
* @return {@link io.github.nichetoolkit.mybatis.enums.DatabaseType} The default database type return object is DatabaseType
type.
* @see io.github.nichetoolkit.mybatis.enums.DatabaseType
*/
public static DatabaseType defaultDatabaseType() {
return INSTANCE.tableProperties.getDatabaseType();
}
/**
* defaultSqlProviders
* The default sql providers method.
* @param databaseType {@link io.github.nichetoolkit.mybatis.enums.DatabaseType} The database type parameter is DatabaseType
type.
* @return {@link java.util.List} The default sql providers return object is List
type.
* @see io.github.nichetoolkit.mybatis.enums.DatabaseType
* @see java.util.List
*/
public static List defaultSqlProviders(DatabaseType databaseType) {
return SQL_PROVIDER_CACHES.get(databaseType);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy