com.jn.sqlhelper.mybatisplus.spring.boot.autoconfigure.DynamicSqlSessionTemplateAutoConfiguration Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.gnu.org/licenses/lgpl-2.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jn.sqlhelper.mybatisplus.spring.boot.autoconfigure;
import com.baomidou.mybatisplus.autoconfigure.*;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisPlusVersion;
import com.jn.langx.annotation.NonNull;
import com.jn.langx.util.Emptys;
import com.jn.langx.util.Preconditions;
import com.jn.langx.util.Throwables;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.comparator.StringComparator;
import com.jn.langx.util.function.Consumer;
import com.jn.langx.util.function.Predicate;
import com.jn.langx.util.reflect.Reflects;
import com.jn.sqlhelper.datasource.DataSourceRegistry;
import com.jn.sqlhelper.datasource.NamedDataSource;
import com.jn.sqlhelper.datasource.key.MethodInvocationDataSourceKeySelector;
import com.jn.sqlhelper.datasource.supports.spring.boot.DynamicDataSourcesAutoConfiguration;
import com.jn.sqlhelper.mybatis.spring.session.factory.dynamicdatasource.DelegatingSqlSessionFactory;
import com.jn.sqlhelper.mybatis.spring.session.factory.dynamicdatasource.DynamicSqlSessionFactory;
import com.jn.sqlhelper.mybatis.spring.session.factory.dynamicdatasource.DynamicSqlSessionTemplate;
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.MyBatisExceptionTranslator;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ListFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import javax.sql.DataSource;
import java.util.List;
@ConditionalOnProperty(name = "sqlhelper.dynamic-datasource.enabled", havingValue = "true", matchIfMissing = false)
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class, DynamicSqlSessionFactory.class})
@ConditionalOnBean(name = "dataSourcesFactoryBean")
@EnableConfigurationProperties(MybatisPlusProperties.class)
@AutoConfigureAfter(DynamicDataSourcesAutoConfiguration.class)
@AutoConfigureBefore({MybatisPlusAutoConfiguration.class})
@Configuration
public class DynamicSqlSessionTemplateAutoConfiguration implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(DynamicSqlSessionTemplateAutoConfiguration.class);
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
private MybatisPlusProperties cloneMybatisPlusProperties(@NonNull MybatisPlusProperties properties) {
String mybatisPlusVersion = MybatisPlusVersion.getVersion();
if(new StringComparator().compare(mybatisPlusVersion, "3.5.3.2")<=0){
return new MyBatisPlusPropertiesClone_LE_3_5_3().cloneObject(properties);
}else{
return new MyBatisPlusPropertiesClone_GE_3_5_4().cloneObject(properties);
}
}
@Bean(name = "sqlSessionFactory")
public DynamicSqlSessionFactory sqlSessionFactory(
final ObjectProvider registryProvider,
@Qualifier("dataSourcesFactoryBean")
ListFactoryBean dataSourcesFactoryBean,
final MybatisPlusProperties properties,
final ObjectProvider interceptorsProvider,
final ObjectProvider typeHandlerProvider,
final ObjectProvider languageDriverProvider,
final ResourceLoader resourceLoader,
final ObjectProvider databaseIdProvider,
final ObjectProvider> configurationCustomizersProvider,
/**
* 从sqlhelper 4.0.3 开始新加该参数,为了应对mybatis-plus 3.5.3。
* 但是 mybatis-plus 3.5.3 版本才开始加这个接口
*/
final ObjectProvider> sqlSessionFactoryBeanCustomizers,
final ObjectProvider> mybatisPlusPropertiesCustomizerProvider) throws BeanCreationException {
List dataSources = null;
try {
List ds = dataSourcesFactoryBean.getObject();
dataSources = (List) ds;
} catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
}
if (Emptys.isNotEmpty(dataSources)) {
try {
registryProvider.getObject();
} catch (BeansException ex) {
logger.error("please check whether the sqlhelper-datasource.jar in the classpath or not");
throw ex;
}
List customizers = configurationCustomizersProvider.getIfAvailable();
final ConfigurationCustomizer transactionFactoryCustomizer = Collects.findFirst(customizers, new Predicate() {
@Override
public boolean test(ConfigurationCustomizer customizer) {
return customizer instanceof DynamicDataSourceTransactionFactoryCustomizer;
}
});
final DynamicSqlSessionFactory dynamicSqlSessionFactory = new DynamicSqlSessionFactory();
Collects.forEach(dataSources, new Consumer() {
@Override
public void accept(DataSource dataSource) {
NamedDataSource namedDataSource = registryProvider.getObject().wrap(dataSource);
try {
logger.info("===[SQLHelper & MyBatis-Plus 3.x]=== Create mybatis SqlSessionFactory instance for datasource {}", namedDataSource.getDataSourceKey());
MybatisPlusProperties newProperties = cloneMybatisPlusProperties(properties);
SqlSessionFactory delegate = createSqlSessionFactory(dataSource, newProperties, interceptorsProvider, typeHandlerProvider, languageDriverProvider, resourceLoader, databaseIdProvider,configurationCustomizersProvider,sqlSessionFactoryBeanCustomizers, mybatisPlusPropertiesCustomizerProvider);
if (delegate != null) {
if (transactionFactoryCustomizer != null) {
transactionFactoryCustomizer.customize((MybatisConfiguration) delegate.getConfiguration());
}
DelegatingSqlSessionFactory sqlSessionFactory = new DelegatingSqlSessionFactory();
sqlSessionFactory.setDelegate(delegate);
PersistenceExceptionTranslator translator = new MyBatisExceptionTranslator(delegate.getConfiguration().getEnvironment().getDataSource(), true);
sqlSessionFactory.setPersistenceExceptionTranslator(translator);
dynamicSqlSessionFactory.addSqlSessionFactory(namedDataSource.getDataSourceKey(), sqlSessionFactory);
}
} catch (Throwable ex) {
logger.error("Error occur when create SqlSessionFactory for datasource {}, error: {}", namedDataSource.getDataSourceKey(), ex.getMessage(), ex);
}
}
});
return dynamicSqlSessionFactory;
} else {
throw new BeanCreationException("Can't find any jdbc datasource");
}
}
private SqlSessionFactory createSqlSessionFactory(DataSource dataSource,
MybatisPlusProperties properties,
ObjectProvider interceptorsProvider,
ObjectProvider typeHandlerProvider,
ObjectProvider languageDriverProvider,
ResourceLoader resourceLoader,
ObjectProvider databaseIdProviderObjectProvider,
ObjectProvider> configurationCustomizersProvider,
/**
* 从sqlhelper 4.0.3 开始新加该参数,为了应对mybatis-plus 3.5.3
*/
ObjectProvider> sqlSessionFactoryBeanCustomizers,
ObjectProvider> mybatisPlusPropertiesCustomizerProvider) throws Exception {
MybatisPlusAutoConfiguration mybatisAutoConfiguration = null;
// mybatis-plus-boot-starter 不同版本的构造器会有变化
// mybatis-plus-boot-starter 3.1.2
mybatisAutoConfiguration = Reflects.newInstance(MybatisPlusAutoConfiguration.class,
new Class[]{
MybatisPlusProperties.class,
ObjectProvider.class,
ResourceLoader.class,
ObjectProvider.class,
ObjectProvider.class,
ObjectProvider.class,
ApplicationContext.class
},
new Object[]{
properties,
interceptorsProvider,
resourceLoader,
databaseIdProviderObjectProvider,
configurationCustomizersProvider,
mybatisPlusPropertiesCustomizerProvider,
this.applicationContext
});
// mybatis-plus-boot-starter 3.2.0 ~ 3.4.1
if (mybatisAutoConfiguration == null) {
mybatisAutoConfiguration = Reflects.newInstance(MybatisPlusAutoConfiguration.class,
new Class[]{
MybatisPlusProperties.class,
ObjectProvider.class,
ObjectProvider.class,
ObjectProvider.class,
ResourceLoader.class,
ObjectProvider.class,
ObjectProvider.class,
ObjectProvider.class,
ApplicationContext.class
},
new Object[]{
properties,
interceptorsProvider,
typeHandlerProvider,
languageDriverProvider,
resourceLoader,
databaseIdProviderObjectProvider,
configurationCustomizersProvider,
mybatisPlusPropertiesCustomizerProvider,
this.applicationContext
});
}
// 从 mybatis-plus 3.5 开始
if(mybatisAutoConfiguration==null){
mybatisAutoConfiguration = Reflects.newInstance(MybatisPlusAutoConfiguration.class,
new Class[]{
MybatisPlusProperties.class,
ObjectProvider.class,
ObjectProvider.class,
ObjectProvider.class,
ResourceLoader.class,
ObjectProvider.class,
ObjectProvider.class,
ObjectProvider.class,
ObjectProvider.class,
ApplicationContext.class
},
new Object[]{
properties,
interceptorsProvider,
typeHandlerProvider,
languageDriverProvider,
resourceLoader,
databaseIdProviderObjectProvider,
configurationCustomizersProvider,
sqlSessionFactoryBeanCustomizers,
mybatisPlusPropertiesCustomizerProvider,
this.applicationContext
});
}
Preconditions.checkNotNull(mybatisAutoConfiguration, "the mybatis-plus 3.x autoconfiguration is null");
mybatisAutoConfiguration.afterPropertiesSet();
try {
return mybatisAutoConfiguration.sqlSessionFactory(dataSource);
}catch (Throwable e){
logger.error(e.getMessage(), e);
throw Throwables.wrapAsRuntimeException(e);
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(
MybatisPlusProperties mybatisProperties,
SqlSessionFactory sessionFactory,
MethodInvocationDataSourceKeySelector selector) {
DynamicSqlSessionTemplate template = new DynamicSqlSessionTemplate(sessionFactory, mybatisProperties.getExecutorType());
template.setSelector(selector);
return template;
}
}