SpringBoot怎么配置多数据源
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍了SpringBoot怎么配置多数据源,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、建库建表1.1 创建数据库db
千家信息网最后更新 2026年11月02日SpringBoot怎么配置多数据源
这篇文章主要介绍了SpringBoot怎么配置多数据源,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
一、建库建表
1.1 创建数据库db1和数据库db2
1.2 在数据库db1中创建表db1
CREATE TABLE `db1` ( `id` int unsigned zerofill NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `age` int unsigned zerofill DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;1.3 在数据库db2中创建表db2
CREATE TABLE `db2` ( `id` int unsigned zerofill NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `age` int unsigned zerofill DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;二、创建springboot项目
2.1 pom.xml导入依赖
org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine junit junit test 2.2 创建application.yml文件(与 2.3 二选一进行配置,推荐此方法)
server: port: 8080 # 启动端口spring: datasource: db1: # 数据源1 jdbc-url: jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver db2: # 数据源2 jdbc-url: jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver2.3 创建application.properties文件(与 2.2 二选一进行配置)
server.port=8080 spring.datasource.db1.url=jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8 spring.datasource.db1.username=root spring.datasource.db1.password=root spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver spring.datasource.db2.url=jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8 spring.datasource.db2.username=root spring.datasource.db2.password=root spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
2.4 创建mapper文件
我个人是放在mapper包下,文件随便命名的
代码随便写的,测试而已
import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;/** * @Author if * @Description: What is it * @Date 2021-05-20 下午 09:52 */@Mapperpublic interface Db1Mapper { @Insert("insert into db1(name,age) values('if',18)") int add();}import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;/** * @Author if * @Description: What is it * @Date 2021-05-20 下午 09:52 */@Mapperpublic interface Db2Mapper { @Insert("insert into db2(name,age) values('fi',81)") int add();}2.5 创建config配置文件
我个人是放在config包下,文件随便命名的
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 javax.sql.DataSource;/** * @Author if * @Description: 注意以下有些文件路径需要更改 * @Date 2021-05-20 下午 09:56 */@Configuration@MapperScan(basePackages = "com.ifyyf.study.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")public class Db1DataSourceConfig { @Bean("db1DataSource") @ConfigurationProperties(prefix = "spring.datasource.db1") //读取application.yml中的配置参数映射成为一个对象 public DataSource getDb1DataSource(){ return DataSourceBuilder.create().build(); } @Bean("db1SqlSessionFactory") public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致) bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db1/*.xml")); return bean.getObject(); } @Bean("db1SqlSessionTemplate") public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); }}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 javax.sql.DataSource;/** * @Author if * @Description: 注意以下有些文件路径需要更改 * @Date 2021-05-20 下午 09:56 */@Configuration@MapperScan(basePackages = "com.ifyyf.study.mapper.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")public class Db2DataSourceConfig { @Bean("db2DataSource") @ConfigurationProperties(prefix = "spring.datasource.db2") //读取application.yml中的配置参数映射成为一个对象 public DataSource getDb2DataSource(){ return DataSourceBuilder.create().build(); } @Bean("db2SqlSessionFactory") public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致) bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db2/*.xml")); return bean.getObject(); } @Bean("db2SqlSessionTemplate") public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); }}三、测试代码运行
3.1 测试类中测试代码
springboot项目中测试类进行测试
import com.ifyyf.study.mapper.db1.Db1Mapper;import com.ifyyf.study.mapper.db2.Db2Mapper;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;@SpringBootTestclass StudyApplicationTests { @Resource private Db1Mapper db1Mapper; @Resource private Db2Mapper db2Mapper; @Test void contextLoads() { System.out.println(db1Mapper.add()); System.out.println(db2Mapper.add()); }}感谢你能够认真阅读完这篇文章,希望小编分享的"SpringBoot怎么配置多数据源"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
文件
配置
数据
测试
数据源
数据库
篇文章
路径
项目
代码
一致
个人
位置
参数
对象
形式
错误
中创
价值
兴趣
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
耶加雪菲
互联网高科技
厦门中拓互联网络科技有限公司
盈实互联网科技
互联网为科技
卓越科技(互联网 )
金融科技和互联网金融
成都仁贤领冠互联网科技有限公司
海南科技产业互联网平台
广州优美互联网科技
中国科技互联网公司收入
受益于互联网科技的高速发展
互联网还是高科技吗
语蝶互联网科技有限公司
互联网科技革命什么开始
从那以后互联网科技有限公司
深圳互联网科技园地址
互联网对科技影响
上海互联网科技园景观
利用互联网等科技手段
手冲咖啡和咖啡机区别
互联网科技垄断
上海致骋互联网科技
四个字互联网科技公司取名
中国最先进互联网科技公司
中国科技互联网公司收入
中国互联网有什么科技含量
青岛科技大学互联网大赛研究生
迪庆互联网科技招生
锡山区互联网智能科技产品供应