In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to operate multiple databases at the same time in the SpringBoot project". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to operate multiple databases at the same time in a SpringBoot project.
In the actual project development, there may be scenarios where you need to operate two databases at the same time, such as reading data from A database and writing data to B database after operation. At this time, you need to configure multiple databases. This article takes operating local and online MySQL databases as an example:
1. Import related pom files mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-jdbc org.mybatis mybatis 3.5.5 com.alibaba druid 1.2.3 org.mybatis mybatis-spring 2.0.7 org.projectlombok lombok true II, application.yml configuration file preparation
The configuration of single data sources is as follows:
Spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/meal_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 username: root password: root type: com.alibaba.druid.pool.DruidDataSource
The configuration of multiple data sources is as follows:
Spring: datasource: dev: driver-class-name: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://xxx.xx.xx.xx:3306/meal_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 username: root password: root type: com.alibaba.druid.pool.DruidDataSource local: driver-class-name: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://127.0. 0.1:3306/db2021?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 username: root password: root type: com.alibaba.druid.pool.DruidDataSource
After comparison, it can be found that:
1. Specific names need to be specified in the configuration of multi-data sources to distinguish different databases (dev and local in the above configuration, whose names can be customized according to specific needs)
2. Need to use jdbcUrl instead of url
Database connection profile
Dev data Source profile:
@ Configuration@MapperScan (basePackages = "com.multiple.mapper.dev", sqlSessionFactoryRef = "devSqlSessionFactory") public class DevDataSourceConfig {@ Primary @ Bean (name = "devDataSource") @ ConfigurationProperties ("spring.datasource.dev") public DataSource masterDataSource () {return DataSourceBuilder.create (). Build ();} @ Bean (name = "devSqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("devDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean () SessionFactoryBean.setDataSource (dataSource); sessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath:mapping/dev/*.xml")); return sessionFactoryBean.getObject ();}}
Local data Source profile:
@ Configuration@MapperScan (basePackages = "com.multiple.mapper.local", sqlSessionFactoryRef = "localSqlSessionFactory") public class LocalDataSourceConfig {@ Primary @ Bean (name = "localDataSource") @ ConfigurationProperties ("spring.datasource.local") public DataSource masterDataSource () {return DataSourceBuilder.create (). Build ();} @ Bean (name = "localSqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("localDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean () SessionFactoryBean.setDataSource (dataSource); sessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath:mapping/local/*.xml")); return sessionFactoryBean.getObject ();}}
Different configuration files distinguish mapper files under different databases by the content of @ MapperScan annotation, and load specified data sources through @ ConfigurationProperties annotation.
Take DevDataSourceConfig as an example
Fourth, the main startup class annotation modified @ SpringBootApplication (exclude= {DataSourceAutoConfiguration.class})
The directory structure is as follows:
5. Testing
Query the data from the dev library, and insert the fields into the local library:
Public interface DevMapper {@ Select ("select * from test") List getAllTest ();} public interface LocalMapper {@ Insert ("insert into payment (serial) values (# {name})") int insertMessage (String name);} @ SpringBootTestclass MultipleDatabaseApplicationTests {@ Autowired private DevMapper devMapper; @ Autowired private LocalMapper localMapper; @ Test void contextLoads () {List testList = devMapper.getAllTest () For (com.multiple.pojo.Test test: testList) {localMapper.insertMessage (test.getAa ());}}
Run the test code, and the data found from the dev library can be successfully added to the local library.
This method is also suitable for scenarios that require the use of a variety of different databases, such as MySQL and Oracle, by modifying the data source configuration file.
At this point, I believe you have a deeper understanding of "how to operate multiple databases at the same time in the SpringBoot project". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.