参考资料:
一. 配置文件
spring:
datasource:
# 数据源1
primary:
jdbc-url: jdbc:mysql://localhost/myblog?useUnicode=true&characterEncoding=utf-8
username: root
password: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
# 数据源2
secondary:
jdbc-url: jdbc:mysql://localhost/pythonblog?useUnicode=true&characterEncoding=utf-8
username: root
password: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
⏹同时连接了Mysql中的两个数据库,myblog
和pythonblog
二. 多数据源配置类
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 javax.sql.DataSource;
@Configuration
public class DataSourceConfiguration {
// Primary注解是在没有指明使用哪个数据源的时候指定默认使用的主数据源
@Primary
@Bean("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean("secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
三. 多数据源Mybatis配置
⭕注意事项:
3.1 primary数据源配置
和3.2 secondary数据源配置
是两种不同的配置方式,都能实现多数据源的效果.此处使用两种不同的方式进行配置,只是为了展示不同的配置方式.sqlSessionFactoryBean.setMapperLocations
适用于Mybatis的Mapper接口文件和 .xml文件不在同一个目录下的情况,用于指定 .xml文件 所在的文件路径.@Primary
注解用于指定默认的数据源.
3.1 primary数据源配置
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@MapperScan(
// 指定该数据源扫描指定包下面的Mapper接口文件
basePackages = "com.example.jmw.mapper",
sqlSessionFactoryRef = "sqlSessionFactoryPrimary",
sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
publc class DataSourcePrimaryConfig {
// 注入数据源1
@Resource
private DataSource primaryDataSource;
@Bean
@Primary
public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(primaryDataSource);
return sqlSessionFactoryBean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryPrimary());
}
}
3.2 secondary数据源配置
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(
basePackages = "com.example.jmw.mapper1",
sqlSessionFactoryRef = "sqlSessionFactorySecondary")
public class DataSourceSecondaryConfig {
// mapper扫描xml文件的路径
private static final String MAPPER_LOCATION = "classpath:mapper1/*.xml";
private DataSource secondaryDataSource;
// 通过构造方法进行注入
public DataSourceSecondaryConfig(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
this.secondaryDataSource = secondaryDataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactorySecondary() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 指定数据源
sqlSessionFactoryBean.setDataSource(secondaryDataSource);
/*
获取xml文件资源对象
当Mapper接口所对应的.xml文件与Mapper接口文件分离,存储在 resources
文件夹下的时候,需要手动指定.xml文件所在的路径
*/
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION);
sqlSessionFactoryBean.setMapperLocations(resources);
return sqlSessionFactoryBean.getObject();
}
@Bean
public DataSourceTransactionManager SecondaryDataSourceManager() {
return new DataSourceTransactionManager(secondaryDataSource);
}
}
四. 效果
import com.example.jmw.mapper.I18nMessageMapper;
import com.example.jmw.mapper1.BlogTagMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/test12")
public class Test12Controller {
// 数据源1Mapper注入
@Resource
private I18nMessageMapper i18nMessageMapper;
// 数据源2Mapper注入
@Resource
private BlogTagMapper blogTagMapper;
@GetMapping("/selectManyDataSourceData")
@ResponseBody
public void selectManyDataSourceData() {
// 查询数据源1中的数据
List<I18MessageEnttiy> allLocaleMessage = i18nMessageMapper.getAllLocaleMessage();
System.out.println(allLocaleMessage);
// 查询数据源2中的数据
List<BlogTag> allBlogTag = blogTagMapper.getAllBlogTag();
System.out.println(allBlogTag);
}
}
版权声明:本文为feyehong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。