1. 程式人生 > >SpringBoot下配置Mybatis多資料來源

SpringBoot下配置Mybatis多資料來源

package com.ai.demos.manager;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;



@Configuration
//掃描 Mapper 介面並容器管理
@MapperScan(basePackages = StudentDataBase.PACKAGE, sqlSessionFactoryRef = "studentSqlSessionFactory")

//從資料來源,不是主資料來源,對於bean的注入千萬不要使用@Primary,只能主資料庫使用,不然報錯
public class StudentDataBase {

	//dao層的包路徑
	static final String PACKAGE = "com.ai.demos.dao.student";
	//mapper檔案的相對路徑
	static final String MAPPER_LOCATION = "classpath:mapper/student/*.xml";
	
	@Value("${student.datasource.url}")
    private String url;
 
    @Value("${student.datasource.username}")
    private String user;
 
    @Value("${student.datasource.password}")
    private String password;
 
    @Value("${student.datasource.driverClassName}")
    private String driverClass;
	
    @Bean(name = "studentDataSource", destroyMethod =  "close")
    public DataSource studentDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(user);//使用者名稱
        dataSource.setPassword(password);//密碼
        dataSource.setDriverClassName(driverClass);
        dataSource.setInitialSize(2);//初始化時建立物理連線的個數
        dataSource.setMaxActive(20);//最大連線池數量
        dataSource.setMinIdle(0);//最小連線池數量
        dataSource.setMaxWait(60000);//獲取連線時最大等待時間,單位毫秒。
        dataSource.setValidationQuery("SELECT 1");//用來檢測連線是否有效的sql
        dataSource.setTestOnBorrow(false);//申請連線時執行validationQuery檢測連線是否有效
        dataSource.setTestWhileIdle(true);//建議配置為true,不影響效能,並且保證安全性。
        dataSource.setPoolPreparedStatements(false);//是否快取preparedStatement,也就是PSCache
        return dataSource;
    }
    
    @Bean(name = "studentTransactionManager")
    public DataSourceTransactionManager studentTransactionManager() {
        return new DataSourceTransactionManager(studentDataSource());
    }
	
	@Bean(name = "studentSqlSessionFactory")
    public SqlSessionFactory studentSqlSessionFactory(@Qualifier("studentDataSource") DataSource studentDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(studentDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(StudentDataBase.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}