先来整体看一下我的项目结构:
现在开始说多数据源配置:
1、配置yml
上图需要注意的是连接用jdbc-url,要不然会报错:
2、配置数据源的config
这里说一下bean.setTypeEnumsPackage(“com.cn.slt.exchange.enums”)这句话,如果这行代码注释了,但是在你的yml里面配置了:
如上图:这种方法是不生效了,测试一下,查询一个带有枚举类型的实体类,会报错:
这里一定要注意!!!!
3、启动类要配置扫描包,就是初始化spring的时候能找到你的DataSourceNewsConfig和DataSourceOldConfig文件并且初始化
下面附上我的两个配置文件代码:
package com.cn.slt.exchange.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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.boot.jdbc.DataSourceBuilder;
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 javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.cn.slt.exchange.dao.old",sqlSessionTemplateRef = "oldSqlSessionTemplate")
public class DataSourceOldConfig {
@Bean(name = "oldDataSource")
@ConfigurationProperties(prefix = "spring.datasource.old")
@Primary
public DataSource testDataSource() {
System.out.println("初始化老的数据源");
return DataSourceBuilder.create().build();
}
@Bean(name = "oldSqlSessionFactory")
@Primary
public SqlSessionFactory oldSqlSessionFactory(@Qualifier("oldDataSource") DataSource dataSource) throws Exception {
//SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeEnumsPackage("com.cn.slt.exchange.enums");
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/old/*.xml"));
return bean.getObject();
}
@Bean(name = "oldSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("oldSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.cn.slt.exchange.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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.boot.jdbc.DataSourceBuilder;
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;
@Configuration
@MapperScan(basePackages = "com.cn.slt.exchange.dao.news",sqlSessionTemplateRef = "newsSqlSessionTemplate")
public class DataSourceNewsConfig {
@Bean(name = "newsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.news")
public DataSource testDataSource() {
System.out.println("初始化新的数据源");
return DataSourceBuilder.create().build();
}
@Bean(name = "newsSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("newsDataSource") DataSource dataSource) throws Exception {
//SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeEnumsPackage("com.cn.slt.exchange.enums"); //多数据源配置了后必须在这里设置enums的指向包,在pom里面设置无效
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/news/*.xml"));
return bean.getObject();
}
@Bean(name = "newsTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("newsDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "newsSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("newsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
END!!!