In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Springboot in how to achieve dynamic data sources, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
/ * function: save a thread-safe DatabaseType container * / public class DatabaseContextHolder {private static final ThreadLocal contextHolder = new ThreadLocal () / * before you need to operate the database, that is, before calling the mapper.c/r/u/d method, you can call this method * the function of this method is to set the database to which you need to connect * because it is thread-safe, if a new controller connection request is made, the method is not explicitly called before operating the database Then the databaseType get to will be null * but this does not affect the operation of the database, because the default data source has been set in the setting of the data source * when in the same thread (that is, in the same request processing of the system controller), if the method has been called * then the subsequent database operation, that is, mapper.c/r/u/d, get will reach the set good data source Unless the set method is explicitly called again to change the data source * / public static void setDatabaseType (String type) {contextHolder.set (type) } / * when manipulating the database through the mapper.c/r/u/d method, etc. * this method is automatically called by the determineCurrentLookupKey method to * determineCurrentLookupKey overrides the determineCurrentLookupKey method of the AbstractRoutingDataSource class in Spring * * @ see DynamicDataSource * / public static String getDatabaseType () {return contextHolder.get ();}} import lombok.extern.slf4j.Slf4j Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.env.Environment;import javax.sql.DataSource;import java.util.HashMap;import java.util.Map;/** * data Source tool Class * / @ Configuration@Slf4jpublic class DataSourceUtil {@ Autowired private Environment env / / default data source private DataSource defaultDataSource; / / user-defined data source private Map slaveDataSources = new HashMap (); / * @ see org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource * / @ Bean @ Primary public DynamicDataSource dataSource () throws Exception {initDefaultDataSource (); initSlaveDataSources (); Map targetDataSources = new HashMap (); targetDataSources.put (", defaultDataSource) TargetDataSources.putAll (slaveDataSources); DynamicDataSource dataSource = new DynamicDataSource (); dataSource.setDefaultTargetDataSource (defaultDataSource); / / this method is AbstractRoutingDataSource's method, default data source dataSource.setTargetDataSources (targetDataSources); / / this method is AbstractRoutingDataSource's method, data source map return dataSource;} private void initDefaultDataSource () {/ / read master data source Map dsMap = new HashMap () DsMap.put ("driver", env.getProperty ("spring.datasource.driver")); dsMap.put ("url", env.getProperty ("spring.datasource.url")); dsMap.put ("username", env.getProperty ("spring.datasource.username")); dsMap.put ("password", env.getProperty ("spring.datasource.password")); defaultDataSource = buildDataSource (dsMap) } private void initSlaveDataSources () {/ / read configuration file to get more data sources String dsPrefixs = env.getProperty ("slave.datasource.names"); for (String dsPrefix: dsPrefixs.split (",")) {/ / multiple data sources Map dsMap = new HashMap () DsMap.put ("driver", env.getProperty ("slave.datasource." + dsPrefix + ".driver"); dsMap.put ("url", env.getProperty ("slave.datasource." + dsPrefix + ".url"); dsMap.put ("username", env.getProperty ("slave.datasource." + dsPrefix + ".username")) DsMap.put ("password", env.getProperty ("slave.datasource." + dsPrefix + ".password"); DataSource ds = buildDataSource (dsMap); slaveDataSources.put (dsPrefix, ds);}} / / specify the default data source (springboot2.0 default data source is how hikari wants to use other data sources can be configured by itself) private static final String DATASOURCE_TYPE_DEFAULT = "com.zaxxer.hikari.HikariDataSource" Private DataSource buildDataSource (Map dataSourceMap) {try {Object type = dataSourceMap.get ("type"); if (type = = null) {type = DATASOURCE_TYPE_DEFAULT;// default DataSource} log.debug ("data source type: {}", type); 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.