当前位置:网站首页>How to realize the usage of connecting multiple databases in idel
How to realize the usage of connecting multiple databases in idel
2022-07-01 09:12:00 【CSDN Q & A】
The only data connected to my project is the local database , How to connect two databases , The primary database is a local database , The secondary database is 192.168.1.123 The database above , When I log in, I access my master database , When I query, the data may be obtained from my secondary database

Take the answer :
Set master-slave Library

** establish DataSourceConfig **
@Configurationpublic class DataSourceConfig { @Bean(name = "dbOneDataSource") @Primary // application.yml In the corresponding db Corresponding to the company's own air grid system database @ConfigurationProperties(prefix = "spring.db1") public DataSource dbOneDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "dbTwoDataSource") // application.yml Corresponding database in 1 @ConfigurationProperties(prefix = "spring.db2") public DataSource dbTwoDataSource() { return DataSourceBuilder.create().build(); } //@Bean(name = "dbThreeDataSource") // application.yml In the corresponding database 2 //@ConfigurationProperties(prefix = "spring.db3") /*public DataSource dbThreeDataSource() { return DataSourceBuilder.create().build(); }*/}To write MybatisConfig
```java /** * The first database SqlSessionFactory && SqlSessionTemplate establish */ @Configuration // Corresponding to the binding data source mapper[ First use ] @MapperScan(basePackages = {
"com.hqkj.httpdemo.demo.one.mapper"}, sqlSessionFactoryRef = "sqlSessionFactoryOne", sqlSessionTemplateRef = "sqlSessionTemplateOne") public static class DBOne{ @Resource DataSource dbOneDataSource; @Bean @Primary public SqlSessionFactory sqlSessionFactoryOne() throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dbOneDataSource); return factoryBean.getObject(); } @Bean @Primary public SqlSessionTemplate sqlSessionTemplateOne() throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryOne()); return template; } }```java/** * The second database SqlSessionFactory && SqlSessionTemplate establish */ @Configuration // Corresponding to the original data binding maper[ Second use ] @MapperScan(basePackages = {
"com.hqkj.httpdemo.demo.two.mapper"}, sqlSessionFactoryRef = "sqlSessionFactoryTwo", sqlSessionTemplateRef ="sqlSessionTemplateTwo" ) public static class DBTwo{ @Resource DataSource dbTwoDataSource; @Bean public SqlSessionFactory sqlSessionFactoryTwo() throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dbTwoDataSource); return factoryBean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplateTwo() throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryTwo()); return template; } }边栏推荐
- [interview brush 101] linked list
- pcl_viewer命令
- MySQL optimization
- Jetson Nano 安装TensorFlow GPU及问题解决
- 【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- 队列的实现和应用
- 如何解决固定资产管理和盘点的难题?
- Installing Oracle EE
- MapReduce编程基础
- Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
猜你喜欢

如何做好固定资产管理?易点易动提供智能化方案

Jeecg restart alarm 40001

Principles of Microcomputer - Introduction

FAQ | FAQ for building applications for large screen devices

【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()

MapReduce编程基础

Principle and application of single chip microcomputer timer, serial communication and interrupt system

树结构---二叉树2非递归遍历

2.2 【pytorch】torchvision. transforms

【检测技术课案】简易数显电子秤的设计与制作
随机推荐
pcl_ Viewer command
Mysql8.0 learning record 17 -create table
Daily office consumables management solution
樹結構---二叉樹2非遞歸遍曆
Graduation season, I want to tell you
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
【pytorch】nn. AdaptiveMaxPool2d
[pytorch] 2.4 convolution function nn conv2d
【pytorch】nn.AdaptiveMaxPool2d
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
LogBack
How to launch circle of friends marketing and wechat group activities
Football and basketball game score live broadcast platform source code /app development and construction project
Simple load balancing with Nacos
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
Can diffusion models be regarded as an autoencoder?
Shell script - positional parameters (command line parameters)
猿人学第20题(题目会不定时更新)
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
Redis source code learning (29), compressed list learning, ziplist C (II)