package com.pku.springboot.dbandcache.config;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.alibaba.druid.pool.DruidDataSourceFactory;
/**
* 数据库链接配置类
*/
@Configuration
@MapperScan(basePackages="com.pku.springboot.mapper")//设置需要扫描的包路径
public class MyBatisConfig {
@Autowired
private Environment env;
/**
* 初始化数据源1
* @return
* @throws Exception
*/
@Bean
public DataSource microservicedb1DataSource()throws Exception{
Properties props=new Properties();//初始化properties解析工具类
props.put("driverClassName", env.getProperty("microservicedb1.jdbc.driverClassName"));//设置数据库链接驱动类
props.put("url", env.getProperty("microservicedb1.jdbc.url"));//设置数据库链接的URl
props.put("username", env.getProperty("microservicedb1.jdbc.username"));//设置数据库链接的用户名
props.put("password", env.getProperty("microservicedb1.jdbc.password"));
return DruidDataSourceFactory.createDataSource(props);
}
/**
* 初始化数据源2
* @return
* @throws Exception
*/
@Bean
public DataSource microservicedb2DataSource()throws Exception{
Properties props=new Properties();//初始化properties解析工具类
props.put("driverClassName", env.getProperty("microservicedb2.jdbc.driverClassName"));//设置数据库链接驱动类
props.put("url", env.getProperty("microservicedb2.jdbc.url"));//设置数据库链接的URl
props.put("username", env.getProperty("microservicedb2.jdbc.username"));//设置数据库链接的用户名
props.put("password", env.getProperty("microservicedb2.jdbc.password"));
return DruidDataSourceFactory.createDataSource(props);
}
/**
* 动态数据源
* @param microservicedb1DataSource
* @param microservicedb2DataSource
* @return
*/
@Bean
@Primary
public DynamicDatasource dataSource(@Qualifier("microservicedb1DataSource") DataSource microservicedb1DataSource
,@Qualifier("microservicedb2DataSource") DataSource microservicedb2DataSource){
Map
targetDataSources=new HashMap();
targetDataSources.put(DatabaseType.microservicedb1,microservicedb1DataSource);
targetDataSources.put(DatabaseType.microservicedb2, microservicedb2DataSource);
DynamicDatasource dataSource=new DynamicDatasource();//动态数据源
dataSource.setTargetDataSources(targetDataSources);//设置目标数据源
dataSource.setDefaultTargetDataSource(microservicedb1DataSource);//设置默认的数据源
return dataSource;
}
/**
* 获取sql会话工厂
* @param ds
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactory sqlSessionFactory(DynamicDatasource ds)throws Exception{
SqlSessionFactoryBean ssfb=new SqlSessionFactoryBean();//实例化sql会话工厂bean
ssfb.setDataSource(ds);//设置数据源实例
ssfb.setTypeAliasesPackage("com.pku.springboot.model");//设置基础包路径
ssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/*.xml"));//设置数据库实体表的映射文件的文件路径
return ssfb.getObject();
}
}
注:
在DynamicDataSource中Qualifier注解中标示的两数据的名称要和上面声明的两个或多个数据源的方法名保持一致,不然在启动过程中就会一直报数据源找不到。