当前位置:网站首页>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; } }
边栏推荐
- 【检测技术课案】简易数显电子秤的设计与制作
- Shell脚本-位置参数(命令行参数)
- Bird recognition app
- The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
- Shell脚本-case in语句
- [MFC development (16)] tree control
- Why is the Ltd independent station a Web3.0 website!
- MapReduce编程基础
- 【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
- Principle and application of single chip microcomputer timer, serial communication and interrupt system
猜你喜欢
[MFC development (16)] tree control
MySQL optimization
NiO zero copy
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
How to launch circle of friends marketing and wechat group activities
Phishing identification app
Understanding and implementation of AVL tree
TV size and viewing distance
Bird recognition app
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
随机推荐
Log4j log framework
Shell脚本-case in语句
Shell脚本-echo命令 转义符
Promise异步编程
How to manage fixed assets efficiently in one stop?
Installing Oracle EE
Interrupt sharing variables with other functions and protection of critical resources
小鸟识别APP
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
How to effectively align team cognition
FreeRTOS learning easy notes
Embedded Engineer Interview Question 3 Hardware
It technology ebook collection
Input标签的type设置为number,去掉上下箭头
Summary of reptile knowledge points
JCL and slf4j
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Shell script echo command escape character
Redis——Lettuce连接redis集群
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警