/* * Copyright 2015-2016 http://hsweb.me * * Licensed under the Apache License, 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.apache.org/licenses/LICENSE-2.0 * * 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 org.hsweb.web.mybatis; import org.apache.ibatis.mapping.DatabaseIdProvider; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.TransactionIsolationLevel; import org.apache.ibatis.transaction.Transaction; import org.hsweb.web.mybatis.dynamic.DynamicDataSourceSqlSessionFactoryBuilder; import org.hsweb.web.mybatis.dynamic.DynamicSpringManagedTransaction; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; import org.mybatis.spring.transaction.SpringManagedTransactionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.springframework.util.StringUtils; import javax.sql.DataSource; @Configuration @EnableConfigurationProperties(MybatisProperties.class) @ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) public class MyBatisAutoConfiguration { @Autowired private MybatisProperties properties; @Autowired(required = false) private Interceptor[] interceptors; @Autowired private ResourceLoader resourceLoader = new DefaultResourceLoader(); @Autowired(required = false) private DatabaseIdProvider databaseIdProvider; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setVfs(SpringBootVFS.class); if (properties.isDynamicDatasource()) { factory.setSqlSessionFactoryBuilder(new DynamicDataSourceSqlSessionFactoryBuilder()); factory.setTransactionFactory(new SpringManagedTransactionFactory() { @Override public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) { return new DynamicSpringManagedTransaction(); } }); } factory.setDataSource(dataSource); if (StringUtils.hasText(this.properties.getConfigLocation())) { factory.setConfigLocation(this.resourceLoader.getResource(this.properties .getConfigLocation())); } if (this.interceptors != null && this.interceptors.length > 0) { factory.setPlugins(this.interceptors); } if (this.databaseIdProvider != null) { factory.setDatabaseIdProvider(this.databaseIdProvider); } factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); String typeHandlers = "org.hsweb.web.mybatis.handler"; if (this.properties.getTypeHandlersPackage() != null) { typeHandlers = typeHandlers + ";" + this.properties.getTypeHandlersPackage(); } factory.setTypeHandlersPackage(typeHandlers); factory.setMapperLocations(this.properties.resolveMapperLocations()); return factory.getObject(); } }