com.jn.sqlhelper.tkmapper.spring.boot.autoconfigure.DynamicSqlSessionTemplateAutoConfiguration Maven / Gradle / Ivy
/*
* 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.tkmapper.spring.boot.autoconfigure;
import com.jn.langx.util.Emptys;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.function.Consumer;
import com.jn.langx.util.function.Predicate;
import com.jn.sqlhelper.datasource.DataSourceRegistry;
import com.jn.sqlhelper.datasource.NamedDataSource;
import com.jn.sqlhelper.datasource.key.MethodInvocationDataSourceKeySelector;
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.session.SqlSessionFactory;
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.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.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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 tk.mybatis.mapper.autoconfigure.ConfigurationCustomizer;
import tk.mybatis.mapper.autoconfigure.MapperAutoConfiguration;
import tk.mybatis.mapper.autoconfigure.MybatisProperties;
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(MybatisProperties.class)
@AutoConfigureBefore({MapperAutoConfiguration.class, DataSourceAutoConfiguration.class})
@Configuration
public class DynamicSqlSessionTemplateAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(DynamicSqlSessionTemplateAutoConfiguration.class);
@Bean("sqlSessionFactory")
public DynamicSqlSessionFactory dynamicSqlSessionFactory(
final ObjectProvider registryProvider,
@Qualifier("dataSourcesFactoryBean")
ListFactoryBean dataSourcesFactoryBean,
final MybatisProperties properties,
final ObjectProvider interceptorsProvider,
final ResourceLoader resourceLoader,
final ObjectProvider databaseIdProvider,
final ObjectProvider> configurationCustomizersProvider) 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 & tk.mapper]=== Create mybatis SqlSessionFactory instance for datasource {}", namedDataSource.getDataSourceKey());
SqlSessionFactory delegate = createSqlSessionFactory(dataSource, properties, interceptorsProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider);
if (delegate != null) {
if (transactionFactoryCustomizer != null) {
transactionFactoryCustomizer.customize(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,
MybatisProperties properties,
ObjectProvider interceptorsProvider,
ResourceLoader resourceLoader,
ObjectProvider databaseIdProviderObjectProvider,
ObjectProvider> configurationCustomizersProvider) throws Exception {
MapperAutoConfiguration mybatisAutoConfiguration = new MapperAutoConfiguration(properties, interceptorsProvider, resourceLoader, databaseIdProviderObjectProvider, configurationCustomizersProvider);
return mybatisAutoConfiguration.sqlSessionFactory(dataSource);
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(
MybatisProperties mybatisProperties,
SqlSessionFactory sessionFactory,
MethodInvocationDataSourceKeySelector selector) {
DynamicSqlSessionTemplate template = new DynamicSqlSessionTemplate(sessionFactory, mybatisProperties.getExecutorType());
template.setSelector(selector);
return template;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy