日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

springboot 配置双mysql数据库

發(fā)布時間:2024/10/5 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 配置双mysql数据库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

項(xiàng)目中用到,學(xué)習(xí)了一下,記錄下來,先回用,再搞懂原理

架構(gòu)

springboot+mybatis+mysql連接池(springboot默認(rèn)的HikariCP)

?配置點(diǎn)

1.就目錄里的DataSourceConfigBackup和DataSourceConfigMaster,以第一個舉例

package com.faith.twodatabase.configuration;import com.zaxxer.hikari.HikariDataSource; 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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configuration @MapperScan(basePackages = {"com.faith.twodatabase.mapper.master"}, sqlSessionFactoryRef = "masterSqlSessionFactory") public class DataSourceConfigMaster {@Primary@ConfigurationProperties(prefix = "spring.datasource.master")@Bean(name = "masterDataSource")public HikariDataSource dataSource() {return new HikariDataSource();}@Primary@Bean(name = "masterSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();// 指定這個 mapper 文件的位置sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResource("classpath:mapper/master/ProductConfigMapper.xml"));sqlSessionFactoryBean.setDataSource(dataSource);return sqlSessionFactoryBean.getObject();}/* @Primary@Bean("masterSqlSessionTemplate")public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}*/}

網(wǎng)上帖子說還要配置SqlSessionTemplate,就是注釋掉的部分,其實(shí)就是定制化一下此連接池使用的mybatis的SqlSessionTemplate而已,不配置就是Mybatis默認(rèn)的,我用默認(rèn)的表示兩個不同的連接池公用一個相同的SqlSessionTemplate,用過了,沒什么問題。

2.yaml配置兩個連接池

# 應(yīng)用名稱 spring:application:name: twodatabase# 數(shù)據(jù)庫驅(qū)動:datasource:# 數(shù)據(jù)源名稱master:type: com.zaxxer.hikari.HikariDataSourcejdbc-url: jdbc:mysql://192.168.228.140:3306/mysql?useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=truedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: rootminimum-idle: 1maximun-pool-size: 5auto-commit: trueidle-timeout: 60000pool-name: mastermax-lifetime: 1800000connection-timeout: 30000connection-test-query: select 1backup:type: com.zaxxer.hikari.HikariDataSourcejdbc-url: jdbc:mysql://192.168.228.132:3307/mysql?useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=truedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: rootminimum-idle: 1maximun-pool-size: 5auto-commit: trueidle-timeout: 60000pool-name: backupmax-lifetime: 1800000connection-timeout: 30000connection-test-query: select 1 server:port: 8080 mybatis:configuration:map-underscore-to-camel-case: true

3.再service層(有帖子說再M(fèi)apper層也可以)配置@DS注解,表明用哪個連接,以MasterService舉例

package com.faith.twodatabase.service;import com.baomidou.dynamic.datasource.annotation.DS; import com.faith.twodatabase.mapper.master.ProductConfigMapper; import com.faith.twodatabase.vo.ProductConfigVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;@Service public class MasterService {@AutowiredProductConfigMapper productConfigMapper;@DS("masterDataSource")public List<ProductConfigVO> loadProductConfigs() {return productConfigMapper.selectALl();} }

DS注解是github上開源的組件,maven引進(jìn)來就行了

<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version></dependency>

分析一下原理

眾所周知,無論什么永久層框架,肯定是封裝的 jdbc 的連接,mybatis 也不能例外,只要是 jdbc,就逃不過這幾部:

1.實(shí)例化Driver

Driver driver = (Driver) Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

2.獲取連接

String url = "jdbc:mysql://192.168.228.140:3306/mysql?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false";Properties properties = new Properties();properties.setProperty("user", "root");properties.setProperty("password", "root");connection = driver.connect(url, properties);

3.準(zhǔn)備?PreparedStatement

PreparedStatement preparedStatement = connection.prepareStatement("select project_id from product_config");

4.執(zhí)行

ResultSet resultSet = preparedStatement.executeQuery();

5.剩下的就是封裝 resultSet 了。

要切庫,一定是在第二步?driver.connect(url,properties),根據(jù)不同的庫傳進(jìn)來不同的url和properties,講道理,無論是用aop,asm,動態(tài)代理,javassist,肯定是在這里做了動態(tài)攔截進(jìn)行更改了,@DS這個注解肯定是做了這方面的工作了?;仡^研究明白了更新博客。

總結(jié)

以上是生活随笔為你收集整理的springboot 配置双mysql数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。