lodsve.mybatis.configuration.MyBatisConfiguration Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package lodsve.mybatis.configuration;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
import java.util.List;
/**
* message-mybatis配置包扫描路径.
*
* @author sunhao([email protected])
* @date 16/1/19 下午10:21
*/
@org.springframework.context.annotation.Configuration
public class MyBatisConfiguration {
private static final String DATA_SOURCE_BEAN_NAME = "lodsveDataSource";
private final List customizers;
private final Interceptor[] interceptors;
public MyBatisConfiguration(ObjectProvider> customizers, ObjectProvider interceptors) {
this.customizers = customizers.getIfAvailable();
this.interceptors = interceptors.getIfAvailable();
}
@Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier(DATA_SOURCE_BEAN_NAME) DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
if (ArrayUtils.isNotEmpty(interceptors)) {
factory.setPlugins(interceptors);
}
Configuration configuration = new Configuration();
customizers.forEach(c -> c.customize(configuration));
factory.setConfiguration(configuration);
return factory.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}