SpringBoot?配置多個(gè)JdbcTemplate的實(shí)現(xiàn)步驟
前言
開(kāi)發(fā)中使用多數(shù)據(jù)源配置是一個(gè)非常常見(jiàn)的需求。Spring和SpringBoot中,對(duì)此都有相應(yīng)的解決方案。
多數(shù)據(jù)源的首選分布式數(shù)據(jù)庫(kù)中間件MyCat或者Sharing-Jdbc去解決相關(guān)問(wèn)題。使用MyCat,然后分表策略使用sharding-by-intfile。
本文我們只討論如何在SpringBoot中簡(jiǎn)單配置多個(gè)JdbcTemplate。
一、創(chuàng)建一個(gè)SpringBoot 項(xiàng)目,并引入如下依賴
<!--web應(yīng)用--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql驅(qū)動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!--單元測(cè)試--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
二、在application.properties中添加數(shù)據(jù)源配置
spring.datasource.one.url=jdbc:mysql://localhost:3306/oy1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.one.jdbcUrl=jdbc:mysql://localhost:3306/oy1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.one.username=*** spring.datasource.one.password=*** spring.datasource.one.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.two.url=jdbc:mysql://localhost:3306/oy2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.two.jdbcUrl=jdbc:mysql://localhost:3306/oy2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.two.username=*** spring.datasource.two.password=*** spring.datasource.two.driver-class-name=com.mysql.cj.jdbc.Driver
三、新增DadaSourceConfig.java配置多個(gè)數(shù)據(jù)源以及JdbcTemplate,代碼如下:
package com.bestoyc.jdbctemplatedemo; 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.jdbc.core.JdbcTemplate; import javax.sql.DataSource; /** * @author oyc * @Title: DataSourceConfig * @ProjectName jdbctemplatedemo * @Description: TODO * @date 2019/9/22 0:47 */ @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.one") @Qualifier("oneDataSource") DataSource dsOne() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.two") @Qualifier("twoDataSource") DataSource dsTwo() { return DataSourceBuilder.create().build(); } @Bean(name = "oneJdbcTemplate") public JdbcTemplate primaryJdbcTemplate( @Qualifier("oneDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "twoJdbcTemplate") public JdbcTemplate secondaryJdbcTemplate( @Qualifier("twoDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
四、多個(gè)JdbcTemplate使用
@Autowired @Qualifier("oneJdbcTemplate") private JdbcTemplate oneJdbcTemplate; @Autowired @Qualifier("twoJdbcTemplate") private JdbcTemplate twoJdbcTemplate; @RequestMapping("/createUser1") public String createUser1() { oneJdbcTemplate.update("INSERT INTO `user`(`id`, `name`, `age`) VALUES (?,?,?);", null,"ouyang", 12); return "success"; } @RequestMapping("/createUser2") public String createUser2() { twoJdbcTemplate.update("INSERT INTO `user`(`id`, `name`, `age`) VALUES (?,?,?);", null,"ouyang", 12); return "success"; }
這里只是簡(jiǎn)單使用,讀者可以根據(jù)自己的業(yè)務(wù)需要添加相應(yīng)的AOP用戶數(shù)據(jù)源的切換。
到此這篇關(guān)于SpringBoot 配置多個(gè)JdbcTemplate的文章就介紹到這了,更多相關(guān)SpringBoot 配置多個(gè)JdbcTemplate內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- spring boot使用sharding jdbc的配置方式
- 詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法
- springboot2.0.0配置多數(shù)據(jù)源出現(xiàn)jdbcUrl is required with driverClassName的錯(cuò)誤
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置
- springboot實(shí)現(xiàn)以代碼的方式配置sharding-jdbc水平分表
- SpringBoot3+ShardingJDBC5.5.0 讀寫(xiě)分離配置的實(shí)現(xiàn)
- SpringBoot+MybatisPlus+jdbc連接池配置多數(shù)據(jù)源的實(shí)現(xiàn)
- Spring?JDBC配置與使用的實(shí)現(xiàn)
相關(guān)文章
Java實(shí)現(xiàn)動(dòng)態(tài)獲取文件的絕對(duì)路徑
我們知道在?Java?中讀取一些配置文件信息,是在開(kāi)發(fā)中十分常用的要求。這篇文章就來(lái)和大家聊聊Java如何實(shí)現(xiàn)動(dòng)態(tài)獲取文件的絕對(duì)路徑,感興趣的可以了解一下2023-02-02詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn)
這篇文章主要介紹了詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10淺談java字符串比較到底應(yīng)該用==還是equals
這篇文章主要介紹了淺談java字符串比較到底應(yīng)該用==還是equals,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12如何解決@value獲取不到y(tǒng)aml數(shù)組的問(wèn)題
文章介紹了在使用YAML配置文件時(shí),通過(guò)@Value注解獲取整數(shù)和數(shù)組列表的配置方法,并提供了兩種解決方案:一種適用于非嵌套列表,另一種適用于嵌套列表等復(fù)雜配置2024-11-11Activiti進(jìn)階之組任務(wù)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Activiti進(jìn)階之組任務(wù)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08java線程并發(fā)控制同步工具CountDownLatch
這篇文章主要為大家介紹了java線程并發(fā)控制同步工具CountDownLatch使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08SpringBoot Nacos實(shí)現(xiàn)自動(dòng)刷新
這篇文章主要介紹了SpringBoot Nacos實(shí)現(xiàn)自動(dòng)刷新,Nacos(Dynamic Naming and Configuration Service)是阿里巴巴開(kāi)源的一個(gè)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺(tái)2023-01-01