Spring Boot 揭秘与实战(二) 数据存储篇 - 数据访问与多数据源配置
文章目录
在某些场景下,我们可能会在一个应用中需要依赖和访问多个数据源,例如针对于 MySQL 的分库场景。因此,我们需要配置多个数据源。
环境依赖
修改 POM 文件,添加spring-boot-starter-jdbc依赖。
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
添加mysql依赖。
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.35</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.14</version>
- </dependency>
数据源
在 src/main/resources/application.properties 中配置数据源信息。
- # mysql
- spring.datasource.one.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.one.url=jdbc:mysql://localhost:3307/springboot_db1
- spring.datasource.one.username=root
- spring.datasource.one.password=root
- spring.datasource.two.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.two.url=jdbc:mysql://localhost:3307/springboot_db2
- spring.datasource.two.username=root
- spring.datasource.two.password=root
通过 Java Config 创建 dataSource 和 jdbcTemplate。
我们设定主数据源为 spring.datasource.one 开头的配置,并添加 @Primary ,这样我们还可以享受 Spring Boot 提供的自动配置 DataSource 的机能。此外,第二数据源配置为 spring.datasource.two 开头的配置。
- @Configuration
- @EnableTransactionManagement
- public class BeanConfig {
- @Bean(name = "oneDataSource")
- @Qualifier("oneDataSource")
- @Primary
- @ConfigurationProperties(prefix="spring.datasource.one")
- public DataSource oneDataSource() {
- return DataSourceBuilder.create().build();
- }
- @Bean(name = "twoDataSource")
- @Qualifier("twoDataSource")
- @ConfigurationProperties(prefix="spring.datasource.two")
- public DataSource twoDataSource() {
- return DataSourceBuilder.create().build();
- }
- @Bean(name = "oneJdbcTemplate")
- public JdbcTemplate oneJdbcTemplate(@Qualifier("oneDataSource") DataSource dataSource) {
- return new JdbcTemplate(dataSource);
- }
- @Bean(name = "twoJdbcTemplate")
- public JdbcTemplate twoJdbcTemplate(@Qualifier("twoDataSource") DataSource dataSource) {
- return new JdbcTemplate(dataSource);
- }
- }
单元测试
为了验证我们的配置是否正常,我们通过编写单元测试用例来验证吧。
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(WebMain.class)
- public class JdbcTest {
- @Resource(name="oneJdbcTemplate")
- protected JdbcTemplate jdbcTemplate1;
- @Resource(name="twoJdbcTemplate")
- protected JdbcTemplate jdbcTemplate2;
- @Test
- public void test() throws Exception {
- jdbcTemplate1.update("insert into t_author(id, real_name, nick_name) values(?, ?, ?)", 2, "梁桂钊", "LiangGzone");
- jdbcTemplate2.update("insert into t_author(id, real_name, nick_name) values(?, ?, ?)", 2, "梁桂钊", "LiangGzone");
- }
- }
源代码
相关示例完整代码: springboot-action
(完)

分类:
SpringBoot
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期