springboot 整合mybatis datasourceConfig java配置

来源:互联网 发布:矩阵潜袭 母神 编辑:程序博客网 时间:2024/06/05 01:16
import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;/** * Created by wwr on 2016/11/25. */@Configuration@MapperScan(basePackages = "com.lc.**.dao.master", sqlSessionTemplateRef  = "mybatisMasterSqlSessionTemplate")public class MybatisMasterDataSourceConfig {    @Bean(name = "mybatisMasterDataSource")    @ConfigurationProperties(prefix = "spring.datasource.master")    public DataSource mybatisMasterDataSource() {        return DataSourceBuilder.create().build();    }    @Bean(name = "mybatisMasterSqlSessionFactory")    public SqlSessionFactory mybatisMasterSqlSessionFactory(@Qualifier("mybatisMasterDataSource") DataSource dataSource) throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mybatis/mapper/master/*.xml"));        return bean.getObject();    }    @Bean(name = "mybatisMasterTransactionManager")    public DataSourceTransactionManager mybatisMasterTransactionManager(@Qualifier("mybatisMasterDataSource") DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);    }    @Bean(name = "mybatisMasterSqlSessionTemplate")    public SqlSessionTemplate mybatisMasterSqlSessionTemplate(@Qualifier("mybatisMasterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {        return new SqlSessionTemplate(sqlSessionFactory);    }}
原创粉丝点击