com.zys.mybatis.config.MybatisConfig Maven / Gradle / Ivy
package com.zys.mybatis.config;
import com.zys.mybatis.exception.MybatisZysException;
import com.zys.mybatis.intercept.ResultTypeInterceptor;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandler;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.beans.FeatureDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author zys
* @version 1.0
* @date 2021/3/2 18:26
*/
@Configuration
public class MybatisConfig {
public MybatisConfig(MybatisProperties properties, ObjectProvider interceptorsProvider, ObjectProvider typeHandlersProvider, ObjectProvider languageDriversProvider, ResourceLoader resourceLoader, ObjectProvider databaseIdProvider, ObjectProvider> configurationCustomizersProvider) {
this.properties = properties;
this.interceptors = interceptorsProvider.getIfAvailable();
this.typeHandlers = typeHandlersProvider.getIfAvailable();
this.languageDrivers = languageDriversProvider.getIfAvailable();
this.resourceLoader = resourceLoader;
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
}
private final MybatisProperties properties;
private final Interceptor[] interceptors;
private final TypeHandler[] typeHandlers;
private final LanguageDriver[] languageDrivers;
private final ResourceLoader resourceLoader;
private final DatabaseIdProvider databaseIdProvider;
private final List configurationCustomizers;
/**
*
mybatis:
mapper-locations:
- classpath:mybatis/mapping/*.xml #mybatis映射文件
- classpath:mybatis/mapping/base/*.xml #mybatis映射文件
#实体扫描,多个package用逗号或者分号分隔
#typeAliasesPackage: com.mikao.human_resources_api.entity
config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件
*/
private Map initBaseXml() {
Map resourceMap = new HashMap<>();
resourceMap.put("/mybatis/mapping/base/Base.xml",MybatisConfig.class.getResourceAsStream("/mybatis/mapping/base/Base.xml"));
resourceMap.put("/mybatis/mapping/base/BaseDao.xml",MybatisConfig.class.getResourceAsStream("/mybatis/mapping/base/BaseDao.xml"));
return resourceMap;
}
private T[] concat(T[] first, T[] second) {
T[] result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(this.properties.getConfigLocation())) {
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
this.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);
}
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
factory.setMapperLocations(this.properties.resolveMapperLocations());
// factory.setMapperLocations(this.properties.resolveMapperLocations());
}
Set factoryPropertyNames = Stream.of((new BeanWrapperImpl(SqlSessionFactoryBean.class)).getPropertyDescriptors()).map(FeatureDescriptor::getName).collect(Collectors.toSet());
Class extends LanguageDriver> defaultLanguageDriver = this.properties.getDefaultScriptingLanguageDriver();
if (factoryPropertyNames.contains("scriptingLanguageDrivers") && !ObjectUtils.isEmpty(this.languageDrivers)) {
factory.setScriptingLanguageDrivers(this.languageDrivers);
if (defaultLanguageDriver == null && this.languageDrivers.length == 1) {
defaultLanguageDriver = this.languageDrivers[0].getClass();
}
}
if (factoryPropertyNames.contains("defaultScriptingLanguageDriver")) {
factory.setDefaultScriptingLanguageDriver(defaultLanguageDriver);
}
return factory.getObject();
}
private void applyConfiguration(SqlSessionFactoryBean factory) {
//两种情况,一种指定了配置文件路径,一种指定了configuration配置
org.apache.ibatis.session.Configuration configuration = null;
if (StringUtils.hasText(this.properties.getConfigLocation())) {
//指定了配置文件路径
try {
XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder(Resources.getResourceAsStream(this.properties.getConfigLocation().replace(ResourceLoader.CLASSPATH_URL_PREFIX,"")));
configuration = xmlConfigBuilder.parse();
setConfiguration(configuration);
factory.setConfigLocation(null);
} catch (IOException e) {
throw new MybatisZysException(e.getMessage(),e);
}
}
if (configuration == null) {
configuration = this.properties.getConfiguration();
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
//没有指定configuration也没有指定配置文件
configuration = new org.apache.ibatis.session.Configuration();
}
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
//在没有指定任何情况下,自定义configuration
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
customizer.customize(configuration);
}
}
if (configuration == null) configuration = new org.apache.ibatis.session.Configuration();
setConfiguration(configuration);
}
factory.setConfiguration(configuration);
}
private void setConfiguration(org.apache.ibatis.session.Configuration configuration){
Map resourceMap = initBaseXml();
resourceMap.forEach((k,v) -> {
XMLMapperBuilder mapperParser = new XMLMapperBuilder(v, configuration, k, configuration.getSqlFragments());
mapperParser.parse();
});
//定义拦截器
configuration.setMapUnderscoreToCamelCase(true);
configuration.addInterceptor(new ResultTypeInterceptor());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy