SpringBoot?配置多個JdbcTemplate的實現(xiàn)步驟
前言
開發(fā)中使用多數(shù)據(jù)源配置是一個非常常見的需求。Spring和SpringBoot中,對此都有相應(yīng)的解決方案。
多數(shù)據(jù)源的首選分布式數(shù)據(jù)庫中間件MyCat或者Sharing-Jdbc去解決相關(guān)問題。使用MyCat,然后分表策略使用sharding-by-intfile。
本文我們只討論如何在SpringBoot中簡單配置多個JdbcTemplate。
一、創(chuàng)建一個SpringBoot 項目,并引入如下依賴
<!--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ū)動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!--單元測試--> <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配置多個數(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); } }
四、多個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"; }
這里只是簡單使用,讀者可以根據(jù)自己的業(yè)務(wù)需要添加相應(yīng)的AOP用戶數(shù)據(jù)源的切換。
到此這篇關(guān)于SpringBoot 配置多個JdbcTemplate的文章就介紹到這了,更多相關(guān)SpringBoot 配置多個JdbcTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot集成shiro詳細(xì)教程(小結(jié))
這篇文章主要介紹了spring boot 集成shiro詳細(xì)教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01解決mybatis plus報錯Invalid bound statement
在使用MyBatis時遇到InvalidBoundStatement異常,常因多個MapperScan配置沖突或者包掃描路徑設(shè)置錯誤,解決方法包括保留一個MapperScan聲明、檢查jar包沖突、確保命名空間和掃描路徑正確,使用@TableId注解指定主鍵2024-11-11基于springboot實現(xiàn)整合shiro實現(xiàn)登錄認(rèn)證以及授權(quán)過程解析
這篇文章主要介紹了基于springboot實現(xiàn)整合shiro實現(xiàn)登錄認(rèn)證以及授權(quán)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12