springboot的数据库配置config

package com.lansi.businessdivide.config.db.mybatis;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
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.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**

  • 分单库数据源配置
    */
    @Configuration
    @MapperScan(basePackages = {“com.lansi.businessdivide.dao.bd.mapper”}, sqlSessionTemplateRef = “businessSessionTemplate”)
    public class DataSourceBdConfig {

    @Primary
    @Bean(name = “businessDataSource”)
    @ConfigurationProperties(prefix = “spring.datasource.druid.business.assign.order”)
    public DataSource businessDataSource() {
    return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = “businessJdbcTemplate”)
    public JdbcTemplate businessJdbcTemplate(@Qualifier(“businessDataSource”) DataSource businessDataSource) {
    return new JdbcTemplate(businessDataSource);
    }

    @Bean(name = “businessSqlSessionFactory”)
    public SqlSessionFactory businessSqlSessionFactory(@Qualifier(“businessDataSource”) DataSource businessDataSource, @Qualifier(“pageInterceptor”) PageInterceptor pageInterceptor) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(businessDataSource);
    //添加XML目录
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    bean.setMapperLocations(resolver.getResources(“classpath:bd/mapper/*.xml”));
    bean.setPlugins(new Interceptor[]{pageInterceptor});
    return bean.getObject();
    }

    @Bean(name = “businessSessionTemplate”)
    public SqlSessionTemplate businessSessionTemplate(@Qualifier(“businessSqlSessionFactory”) SqlSessionFactory businessSqlSessionFactory) {
    //使用上面配置的Factory
    return new SqlSessionTemplate(businessSqlSessionFactory);
    }

    @Bean(name = “businessDataSourceTransactionManager”)
    public DataSourceTransactionManager businessDataSourceTransactionManager(@Qualifier(“businessDataSource”) DataSource businessDataSource) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
    dataSourceTransactionManager.setDataSource(businessDataSource);
    return dataSourceTransactionManager;
    }
    }

上一篇:@Autowired和@Resource的区别


下一篇:大数据最难源码 hbase 阅读(一)