In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use SpringBoot + Mybatis multi-source configuration to achieve read-write separation", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to use SpringBoot + Mybatis multi-data source configuration to achieve read-write separation"!
Application scenario: there are some report statistics and query functions in the project, which do not require high real-time data, so consider the statistics and query of the report to operate slave db to reduce the pressure on master.
According to the test of multiple data on the Internet, it is found that the master data source is always used and cannot be switched to slave. After many debugging and modification, it has passed perfectly. The detailed steps and complete code are as follows:
Implementation: configure multiple data sources, use Spring AOP to intercept annotations to achieve dynamic switching of data sources.
1. Application.yml database configuration:
Druid: type: com.alibaba.druid.pool.DruidDataSource master: url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true driver-class-name: com.mysql.jdbc.Driver username: test password: 123 initial-size: 5 max-active: 10 min-idle: 5 max-wait: 60000 time-between-eviction-runs-millis: 3000 min-evictable-idle-time-millis: 300000 validation- Query: SELECT 'x' FROM DUAL test-while-idle: true test-on-borrow: true test-on-return: false filters: stat Wall Log4j2 slave: url: jdbc:mysql://127.0.0.1:3307/test?characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true driver-class-name: com.mysql.jdbc.Driver username: test password: 123 initial-size: 5 max-active: 10 min-idle: 5 max-wait: 60000 time-between-eviction-runs-millis: 3000 min-evictable-idle-time-millis: 300000 validation-query: SELECT'x' FROM DUAL test-while- Idle: true test-on-borrow: true test-on-return: false filters: stat Wall,log4j2
two。 Multi-source injection through MybatisAutoConfiguration:
@ Configuration@EnableTransactionManagementpublic class DataSourceConfiguration extends MybatisAutoConfiguration {@ Value ("${druid.type}") private Class
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.