Multi-data source association component adaptation method and system
By adopting a multi-data source association component adaptation method and system, the code maintenance cost and thread safety issues of traditional data source switching are solved, achieving lightweight and secure dynamic data source management, supporting smooth architecture migration and independent microservice evolution, and improving migration efficiency and data consistency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-24
- Publication Date
- 2026-03-10
AI Technical Summary
In traditional monolithic architectures, switching data sources requires annotations or hard coding, violating the open/closed principle and increasing code maintenance costs. In microservice architectures, existing solutions have thread safety issues and data consistency problems, and the architecture flexibility decreases after merging. In hybrid cloud environments, existing data source transformation solutions are complex, resulting in high transformation costs and are not suitable for lightweight architecture migration.
It provides a method and system for adapting components with multiple data sources. Built on the MyBatis-Spring plugin, it enables dynamic data source management through data source mapping configuration, association engine implementation, MapperScan annotation shielding, and mode switching. It supports smooth switching between monolithic and microservice architectures and uses a stateless routing strategy to solve thread safety issues.
Significantly improves architecture migration efficiency, reduces resource costs, maintains the independent evolution capability of microservices, avoids code intrusion, is compatible with existing technology stacks, and supports data consistency guarantees in high-concurrency scenarios.
Smart Images

Figure CN121635949A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of software design, in particular to a multi-data source association component adaptation method and system. BACKGROUND
[0002] Under the traditional monolithic architecture, MyBatis-Spring adopts a single data source binding mode by default, and if the data source needs to be switched, it is usually necessary to use annotations or hard coding to achieve it, which not only violates the open-closed principle, but also increases the code maintenance cost. In the scenario of merging microservice architecture into monolithic architecture, the existing solution (such as AbstractRoutingDataSource) relies on rewriting the determineCurrentLookupKey method to achieve dynamic data source switching, but since it stores the data source identifier based on thread variables, there is a thread safety problem, which makes it difficult to ensure data consistency in high concurrency scenarios. In addition, when databases are merged, the table structures of different microservices may conflict, and after merging, the independent evolution ability of each service cannot be maintained, resulting in a decrease in architecture flexibility.
[0003] In a hybrid cloud environment, when resource elasticity is insufficient, enterprises may need to temporarily switch back to monolithic architecture to improve resource utilization, but existing data source transformation solutions (such as ShardingSphere) are usually complex in design and introduce too many middleware dependencies, which are not suitable for lightweight architecture migration requirements, resulting in high transformation costs and long implementation cycles.
[0004] How to provide a lightweight, secure, and flexible dynamic data source management solution to support smooth architecture migration while taking into account the independent evolution ability of microservices is a technical problem that needs to be solved. SUMMARY
[0005] The technical task of the present application is to provide a multi-data source association component adaptation method and system to solve the technical problem of how to provide a lightweight, secure, and flexible dynamic data source management solution to support smooth architecture migration while taking into account the independent evolution ability of microservices.
[0006] In a first aspect, the present application provides a multi-data source association component adaptation method, wherein the multi-data source association component is based on MyBatis-Spring plug-ins, and the method comprises the following steps:
[0007] Data source mapping configuration: add multiple data sources and the corresponding scanning package paths of each data source to the MyBatis-Spring plug-in configuration file to form a multi-data source configuration file, and the multi-data source association component supports the configuration file of MyBatis-Spring plug-ins in single data source mode;
[0008] The data source association engine implementation: for each data source, an independent MyBatis running environment is constructed, including data source, transaction manager, session factory, data access execution, and MyBatis scanning mapping configuration;
[0009] Shielding of the MapperScan annotation: dynamically manage the MapperScan annotation function provided by MyBatis-Spring, if the data source is configured with the attribute scanner-base-packages, then automatically shield the function of the MapperScan annotation, avoid the repeated registration of Mapper classes, if the data source is not configured with the attribute scanner-base-packages, then use the default MapperScan annotation to scan the Mapper interface;
[0010] Mode switching: through modifying the build script of the program and the data source configuration file, the smooth switching between monolithic architecture and microservice architecture is realized.
[0011] As a preferred, the multi-data source configuration file is in yaml format, and the multi-data source configuration file defines data source group definition, data source driver, data source URL, data source username, data source password, and scanning package path.
[0012] As a preferred, when the data source association engine is implemented, the multi-data source configuration file is read, for each data source, it is checked whether there is a data source configuration in the multi-data source configuration file, if there is, the data source module, the transaction management module, the data session factory module, the data access execution component, and the MyBatis scanning mapping configuration module are initialized in turn, and the scanning package path of the current data source configuration and the data access execution component are bound, and the data source association engine initialization is completed;
[0013] Among them, the data source module, the transaction management module, the data session factory module, the data access execution component, and the MyBatis scanning mapping configuration module are MyBatis-Spring plug-in native modules;
[0014] The MyBatis mapping scanning configuration module is used to automatically register the Mapper interface according to the specified package path, and a specific implementation in the Spring framework is:
[0015] org.mybatis.spring.mapper.MapperScannerConfigurer;
[0016] The specific implementation of the data source module can select an open source connection pool component;
[0017] The transaction management module adopts a transaction manager based on a JDBC data source to manage the life cycle of a transaction through an associated data source object, and supports transaction opening, submission and rollback operations.
[0018] The data session factory module adopts a factory class of an ORM framework to be responsible for analyzing a Mapper configuration and generating a data session instance.
[0019] The data access execution component adopts a thread-safe ORM template class.
[0020] As a preference, MapperScan is an annotation class provided by MyBatis-Spring by nature, which is used to automatically scan all interfaces according to a specified package path and identify the interfaces as Mapper interfaces of MyBatis.
[0021] Correspondingly, the shielding of the MapperScan annotation includes the following steps:
[0022] An implementation class P of BeanDefinitionRegistryPostProcessor is declared, which is used to intercept all MyBatis mapping scanning configuration module instances, wherein the BeanDefinitionRegistryPostProcessor interface is a standard interface in the Spring framework for extending the registration of Bean definitions.
[0023] In the method postProcessBeanDefinitionRegistry of the implementation class P, all MyBatis mapping scanning configuration module instances are obtained, and removal or retention of the MyBatis mapping scanning configuration module instances is performed according to conditions.
[0024] As a preference, under a monolithic architecture, all modules are aggregated in a service, and data sources and Mapper interface scanning paths are configured according to the modules.
[0025] Under a microservice architecture, each module is an independent service, and there are two configuration methods as follows:
[0026] Each microservice only configures a data source and a Mapper file scanning path of the current service.
[0027] The exclusive multi-data source configuration method of the component is deleted, and the same configuration as Mybatis official MyBatis-Spring-boot-starter is directly adopted.
[0028] In a second aspect, the application is a multi-data-source association component adaptation system, the multi-data-source association component is based on MyBatis-Spring plug-in construction, and the system comprises a data source mapping configuration module, a data source association engine implementation module, a MapperScan annotation shielding module, and a mode switching module.
[0029] The data source mapping configuration module is configured to add multiple data sources and the scanning package path corresponding to each data source on the basis of the MyBatis-Spring plug-in configuration file, form a multi-data-source configuration file, and enable the multi-data-source association component to support the configuration file of the MyBatis-Spring plug-in in a single data source mode.
[0030] The data source association engine implementation module is configured to construct an independent MyBatis running environment for each data source, including a data source, a transaction manager, a session factory, a data access execution, and MyBatis scanning mapping configuration.
[0031] The MapperScan annotation shielding module is configured to dynamically manage the MapperScan annotation function provided by the MyBatis-Spring, automatically shield the MapperScan annotation function if the data source is configured with the attribute scanner-base-packages, avoid repeated registration of Mapper classes, and use the default MapperScan annotation to scan Mapper interfaces if the data source is not configured with the attribute scanner-base-packages.
[0032] The mode switching module is configured to realize smooth switching between monolithic architecture and micro-service architecture by modifying the build script of the program and the data source configuration file.
[0033] Preferably, the multi-data-source configuration file is in a yaml format, and the multi-data-source configuration file defines a data source group definition, a data source driver, a data source URL, a data source username, a data source password, and a scanning package path.
[0034] Preferably, the data source association engine implementation module is configured to read the multi-data-source configuration file, check whether there is a data source configuration in the multi-data-source configuration file for each data source, initialize the data source module, the transaction management module, the data session factory module, the data access execution component, and the MyBatis scanning mapping configuration module in sequence if there is a data source configuration, bind the scanning package path of the current data source configuration and the data access execution component, and complete the initialization of the data source association engine.
[0035] The data source module, the transaction management module, the data session factory module, the data access execution component and the MyBatis scanning mapping configuration module are native modules of MyBatis-Spring plug-in.
[0036] The MyBatis mapping scanning configuration module is used for automatically registering a Mapper interface according to a specified package path, and a specific implementation thereof in a Spring framework is as follows:
[0037] org.mybatis.spring.mapper.MapperScannerConfigurer;
[0038] A specific implementation of the data source module can select an open source connection pool component.
[0039] The transaction management module adopts a transaction manager based on a JDBC data source to manage a life cycle of a transaction through an associated data source object, and supports opening, committing and rolling back operations of the transaction.
[0040] The data session factory module selects a factory class of an ORM framework to parse a Mapper configuration and generate a data session instance.
[0041] The data access execution component adopts a thread-safe ORM template class.
[0042] As a preference, MapperScan is an annotation class provided by MyBatis-Spring, which is used for automatically scanning all interfaces according to a specified package path and identifying the interfaces as Mapper interfaces of MyBatis.
[0043] Correspondingly, the shielding of the MapperScan annotation includes the following steps:
[0044] An implementation class P of a BeanDefinitionRegistryPostProcessor is declared, which is used for intercepting all MyBatis mapping scanning configuration module instances, wherein the BeanDefinitionRegistryPostProcessor interface is a standard interface in a Spring framework for extending registration of a Bean definition.
[0045] In a method postProcessBeanDefinitionRegistry of the implementation class P, all MyBatis mapping scanning configuration module instances are obtained, and removal or reservation of the MyBatis mapping scanning configuration module instances is performed according to a condition.
[0046] As preferred, under the monolithic architecture, all modules are aggregated in one service, and the data source and Mapper interface scanning path are configured respectively according to the modules;
[0047] Under the microservice architecture, each module is an independent service, and the following two configuration methods exist:
[0048] Each microservice only configures the data source and Mapper file scanning path of the current service;
[0049] The exclusive multi-data source configuration method of the component is deleted, and the same configuration as MyBatis-Spring-boot-starter of Mybatis official is directly adopted.
[0050] The multi-data source association component adaptation method and system have the following advantages:
[0051] 1. Significantly improve the architecture migration efficiency: through the dynamic data source management mechanism, the work amount required for switching the traditional architecture is greatly reduced from 8 person-days to 0.5 person-days (actual measurement data), the efficiency is improved by 16 times, and the business system transformation cycle is greatly shortened;
[0052] 2. Effectively reduce resource cost: support multi-data source on-demand routing, avoid forced database merging, reduce more than 30% of storage redundancy; retain the independent evolution ability of microservices, reduce the subsequent expansion and maintenance cost;
[0053] 3. Zero-intrusion adaptation: completely avoid the intrusive modification of the existing business code, and do not need to reconstruct the MyBatis mapping file or DAO layer logic; provide declarative configuration, and developers only need to realize the dynamic switching of the data source through simple annotations;
[0054] 4. Seamless compatibility with existing technology stack: 100% compatible with Mybatis native ecology, support all standard MyBatis features and third-party plugins; adapt to mainstream frameworks such as Spring Boot / Cloud, without additional dependencies or customized modification;
[0055] 5. High reliability and thread safety: adopt a stateless routing strategy, completely solve the thread safety problem of the traditional scheme (such as AbstractRoutingDataSource); support data consistency guarantee in the distributed transaction scenario. BRIEF DESCRIPTION OF DRAWINGS
[0056] In order to make the technical solutions in the embodiments of the present application clearer, the accompanying drawings needed in the embodiments or prior art description will be briefly introduced below. Obviously, the accompanying drawings in the following description only constitute some embodiments of the present application, and other accompanying drawings can be obtained by those skilled in the art without any creative effort on the basis of these accompanying drawings.
[0057] The present application will be further described below in conjunction with the accompanying drawings.
[0058] Figure 1 A flow chart of a data association engine implementation in the multi-data source association component adaptation method of embodiment 1;
[0059] Figure 2 An implementation logic block diagram of MapperScan annotation shielding in the multi-data source association component adaptation method of embodiment 1. DETAILED DESCRIPTION
[0060] The present application will be further described below in conjunction with the accompanying drawings and specific embodiments, so that those skilled in the art can better understand the present application and implement it. However, the embodiments are not limiting to the present application, and the technical features in the embodiments and the embodiments can be combined with each other without conflict.
[0061] The embodiments of the present application provide a multi-data source association component adaptation method, which is used to solve the technical problem of how to provide a lightweight, secure and flexible dynamic data source management scheme to support architecture smooth migration while taking into account the independent evolution ability of microservices.
[0062] Embodiment 1:
[0063] The present application is a multi-data source association component adaptation method, wherein the multi-data source association component is based on MyBatis-Spring plug-in construction, and the method comprises four steps of data source mapping configuration, data source association engine implementation, MapperScan annotation shielding and mode switching.
[0064] Step S100: data source mapping configuration: add multiple data sources and the corresponding scanning package paths of each data source to the MyBatis-Spring plug-in configuration file to form a multi-data source configuration file, and the multi-data source association component supports the configuration file of MyBatis-Spring plug-in in single data source mode.
[0065] In this embodiment, the multi-data source configuration file is in yaml format, and the multi-data source configuration file defines data source group definition, data source driver, data source URL, data source username, data source password and scanning package path. The format of the multi-data source configuration file is as follows:
[0066]
[0067] The step S200 data source association engine implementation: for each data source, build an independent MyBatis running environment, including data source, transaction manager, session factory, data access execution and MyBatis scanning mapping configuration.
[0068] As a specific implementation, when the data source association engine is implemented, read the multi-data source configuration file, for each data source, check whether there is a data source configuration in the multi-data source configuration file, if there is, initialize the data source module, transaction management module, data session factory module, data access execution component and MyBatis scanning mapping configuration module in turn, and bind the scanning package path of the current data source configuration and the data access execution component, complete the initialization of the data source association engine.
[0069] Among them, the data source module, transaction management module, data session factory module, data access execution component and MyBatis scanning mapping configuration module are MyBatis-Spring plug-in native modules.
[0070] The MyBatis mapping scanning configuration module is used to automatically register the Mapper interface according to the specified package path, and a specific implementation thereof in the Spring framework is as follows:
[0071] org.mybatis.spring.mapper.MapperScannerConfigurer.
[0072] The specific implementation of the data source module can select an open source connection pool component, for example, the HikariDataSource class (com.zaxxer.hikari.HikariDataSource) provided by HikariCP.
[0073] The transaction management module adopts a transaction manager based on the JDBC data source, for example, the DataSourceTransactionManager class (org.springframework.jdbc.datasource.DataSourceTransactionManager) provided by the Spring framework.
[0074] The transaction management module adopts a transaction manager based on the JDBC data source, for example, the DataSourceTransactionManager class (org.springframework.jdbc.datasource.DataSourceTransactionManager) provided by the Spring framework.
[0075] The transaction management module adopts a transaction manager based on the JDBC data source, for example, the DataSourceTransactionManager class (org.springframework.jdbc.datasource.DataSourceTransactionManager) provided by the Spring framework.
[0076] The data session factory module selects a factory class implementation of an ORM framework, such as the SqlSessionFactory interface (org.mybatis.spring.SqlSessionFactoryBean) used when Spring integrates MyBatis, which is responsible for parsing Mapper configurations and generating data session instances, and in this embodiment is specifically configured to [describe your specific configuration, such as: specify the XML mapping file path / enable secondary cache, etc.] "responsible for parsing Mapper configurations and generating data session instances.
[0077] The data access execution component adopts a thread-safe ORM template class, such as the SqlSessionTemplate (org.mybatis.spring.SqlSessionTemplate) provided by the MyBatis-Spring integration module, which implements database operations through [describe your usage, such as: automatically participate in Spring transaction management / inject Mapper interface proxy implementation, etc.].
[0078] The step S200 of the embodiment constructs an instance of the MyBatis mapping scan configuration module for each data source according to the data source configuration of step S100, and configures the corresponding data access execution component instance and Mapper interface scan package path, adopts the agreed instance naming format F, and registers with the instance factory of Spring, finally realizing the automatic scanning and isolated registration of the MyBatis Mapper interface.
[0079] Step S300: Shielding of MapperScan annotation: dynamically manage the MapperScan annotation function provided by MyBatis-Spring, if the data source is configured with the attribute scanner-base-packages, automatically shield the function of MapperScan annotation, avoid the repeated registration of Mapper class, if the data source does not configure the attribute scanner-base-packages, use the default MapperScan annotation to scan the Mapper interface.
[0080] Among them, MapperScan is an annotation class provided by MyBatis-Spring, which is used to automatically scan all interfaces according to the specified package path, and identify the interface as a Mappr interface of MyBatis;
[0081] Correspondingly, the shielding of MapperScan annotation includes the following steps:
[0082] (1) Declare an implementation class P of BeanDefinitionRegistryPostProcessor for intercepting all MyBatis mapping scanning configuration module instances, wherein the BeanDefinitionRegistryPostProcessor interface is a standard interface for extending the registration of Bean definitions in the Spring framework;
[0083] (2) In the method postProcessBeanDefinitionRegistry of the implementation class P, all MyBatis mapping scanning configuration module instances are obtained, and removal or retention of the MyBatis mapping scanning configuration module instances is performed according to conditions.
[0084] Step S400 mode switching: by modifying the build script and data source configuration file of the program, smooth switching between monolithic architecture and microservice architecture is realized.
[0085] Under the monolithic architecture, all modules are aggregated in one service, and the data source and Mapper interface scanning path are configured according to the module.
[0086] Under the microservice architecture, each module is an independent service, and there are two configuration methods as follows:
[0087] (1) Each microservice only configures the data source and Mapper file scanning path of the current service;
[0088] (2) By using the feature of the component compatible with the official configuration, the exclusive multi-data source configuration method of the component can be directly deleted, and the same configuration as MyBatis-Spring-boot-starter of MyBatis official can be directly used.
[0089] The method of the embodiment realizes the automatic association of the Mapper interface and the multi-data source by optimizing the package path scanning strategy. The technical scheme includes that the package path scanner identifies the Mapper interface path, the component binds the Mapper of different package paths to independent data sources according to the configuration, and the architecture adapter supports the seamless switching of the microservice and the monolithic architecture. The method of the embodiment solves the problems of code invasive modification and database merging conflict, significantly improves the architecture migration efficiency, and is suitable for the flexible deployment demand in the cloud native environment.
[0090] Embodiment 2:
[0091] The application discloses a multi-data-source association component adaptation system, wherein the multi-data-source association component is constructed based on a MyBatis-Spring plug-in, and the system comprises a data source mapping configuration module, a data source association engine implementation module, a MapperScan annotation shielding module and a mode switching module.
[0092] The data source mapping configuration module is used for adding multiple data sources and scanning package paths corresponding to each data source on the basis of a MyBatis-Spring plug-in configuration file to form a multi-data-source configuration file, and the multi-data-source association component is compatible with the configuration file of the MyBatis-Spring plug-in in a single data source mode.
[0093] In the embodiment, the multi-data-source configuration file is in a yaml format, and the multi-data-source configuration file is defined with a data source group definition, a data source driver, a data source URL, a data source username, a data source password and a scanning package path. The multi-data-source configuration file format is as follows:
[0094]
[0095] The data source association engine implementation module is used for constructing an independent MyBatis running environment for each data source, including a data source, a transaction manager, a session factory, data access execution and MyBatis scanning mapping configuration.
[0096] As a specific implementation, the data source association engine implementation module is used for performing the following operations: reading the multi-data-source configuration file, checking whether there is a data source configuration in the multi-data-source configuration file for each data source, initializing a data source module, a transaction management module, a data session factory module, a data access execution component and a MyBatis scanning mapping configuration module in sequence if the data source configuration exists, and binding the scanning package path of the current data source configuration and the data access execution component to complete the initialization of the data source association engine.
[0097] The data source module, the transaction management module, the data session factory module, the data access execution component and the MyBatis scanning mapping configuration module are original modules of the MyBatis-Spring plug-in.
[0098] The MyBatis mapping scanning configuration module is used for automatically registering a Mapper interface according to a specified package path, and a specific implementation thereof in a Spring framework is as follows:
[0099] org.mybatis.spring.mapper.MapperScannerConfigurer.
[0100] The specific implementation of the data source module can optionally use an open source connection pool component, such as the HikariDataSource class (com.zaxxer.hikari.HikariDataSource) provided by HikariCP.
[0101] The transaction management module is implemented using a JDBC data source-based transaction manager, such as the DataSourceTransactionManager class (org.springframework.jdbc.datasource.DataSourceTransactionManager) provided by the Spring framework.
[0102] The transaction management module is implemented using a JDBC data source-based transaction manager, such as the DataSourceTransactionManager class (org.springframework.jdbc.datasource.DataSourceTransactionManager) provided by the Spring framework.
[0103] The transaction management module is implemented using a JDBC data source-based transaction manager, such as the DataSourceTransactionManager class (org.springframework.jdbc.datasource.DataSourceTransactionManager) provided by the Spring framework.
[0104] The data session factory module is implemented using an ORM framework factory class, such as the SqlSessionFactory interface (org.mybatis.spring.SqlSessionFactoryBean) used when Spring integrates MyBatis. This factory class is responsible for parsing Mapper configurations and generating data session instances. In this embodiment, the specific configuration is [describe your specific configuration, such as specifying the XML mapping file path / enabling secondary cache, etc.] responsible for parsing Mapper configurations and generating data session instances.
[0105] The data access execution component uses a thread-safe ORM template class, such as the SqlSessionTemplate (org.mybatis.spring.SqlSessionTemplate) provided by the MyBatis-Spring integration module. This template class implements database operations through [describe your usage method, such as automatically participating in Spring transaction management / injecting Mapper interface proxy implementation, etc.].
[0106] According to the multi-data source configuration, the module constructs an instance of the MyBatis mapping scan configuration module for each data source, configures the corresponding data access execution component instance and Mapper interface scan package path, and registers it in the instance factory of Spring using the agreed instance naming format F, finally realizing the automatic scanning and isolation registration of the MyBatisMapper interface.
[0107] The shielding module of the MapperScan annotation is used to perform the following: dynamically managing the MapperScan annotation function provided by MyBatis-Spring natively, if the data source is configured with the attribute scanner-base-packages, then automatically shielding the function of the MapperScan annotation, avoiding the repeated registration of Mapper classes, if the data source is not configured with the attribute scanner-base-packages, then using the default MapperScan annotation to scan Mapper interfaces.
[0108] Among them, MapperScan is an annotation class provided by MyBatis-Spring natively, which is used to automatically scan all interfaces according to the specified package path, and identify the interface as a Mappr interface of MyBatis;
[0109] Correspondingly, the MapperScan annotation shielding module is used to perform the following operations:
[0110] (1) Declare an implementation class P of BeanDefinitionRegistryPostProcessor, which is used to intercept all MyBatis mapping scanning configuration module instances, wherein the BeanDefinitionRegistryPostProcessor interface is a standard interface in the Spring framework for extending Bean definition registration;
[0111] (2) In the method postProcessBeanDefinitionRegistry of the implementation class P, all MyBatis mapping scanning configuration module instances are obtained, and MyBatis mapping scanning configuration module instances are removed or retained according to the conditions.
[0112] The mode switching module is used to perform the following: by modifying the build script of the program and the data source configuration file, the smooth switching between monolithic architecture and micro-service architecture is realized.
[0113] Among them, in the monolithic architecture, all modules are aggregated in one service, and the data source and Mapper interface scanning path are configured according to the module.
[0114] Under the micro-service architecture, each module is an independent service, and there are two configuration methods as follows:
[0115] (1) Each micro-service only configures the data source and Mapper file scanning path of the current service;
[0116] (2) Using the characteristics of the official configuration compatible with the component, the exclusive multi-data source configuration method of the component can be directly deleted, and the same configuration as MyBatis-Spring-boot-starter of Mybatis official is directly used.
[0117] The system of the embodiment can execute the method disclosed in embodiment 1 to realize multi-data source adaptation.
[0118] The multi-data source association component adaptation method and system provided by the application are described in detail above, and the principles and implementation modes of the application are described by applying specific examples; the above embodiment is only used to help understand the method and core idea of the application; meanwhile, for those skilled in the art, according to the idea of the application, the specific implementation mode and application range will be changed, and the above description should not be understood as a limitation on the application.
Claims
1. A method for adapting components associated with multiple data sources, characterized in that, The multi-data source association component is constructed based on a MyBatis-Spring plug-in, and the method comprises the following steps: Data source mapping configuration: adding multi-data sources and the scanning package path corresponding to each data source to the MyBatis-Spring plug-in configuration file to form a multi-data source configuration file, and the multi-data source association component supports the configuration file of the MyBatis-Spring plug-in in a single data source mode; Data source association engine implementation: constructing an independent MyBatis running environment for each data source, including a data source, a transaction manager, a session factory, data access execution and MyBatis scanning mapping configuration; Shielding of the MapperScan annotation: dynamically managing the MapperScan annotation function provided by MyBatis-Spring, if the data source is configured with the attribute scanner-base-packages, then the function of the MapperScan annotation is automatically shielded to avoid repeated registration of the Mapper class, if the data source is not configured with the attribute scanner-base-packages, then the default MapperScan annotation is used for Mapper interface scanning; Mode switching: realizing smooth switching between monolithic architecture and micro-service architecture by modifying the build script of the program and the data source configuration file. Smooth switching between monolithic architecture and micro-service architecture is realized by modifying the build script of the program and the data source configuration file.
2. The method of claim 1, wherein, The multi-data source configuration file is in the yaml format, and the multi-data source configuration file defines a data source group definition, a data source driver, a data source URL, a data source username, a data source password and a scanning package path.
3. The method of claim 1, wherein, When the data source association engine is implemented, the multi-data source configuration file is read, for each data source, it is checked whether the data source configuration exists in the multi-data source configuration file, if the data source configuration exists, the data source module, the transaction management module, the data session factory module, the data access execution component and the MyBatis scanning mapping configuration module are initialized in sequence, and the scanning package path of the current data source configuration and the data access execution component are bound, and the data source association engine initialization is completed; The data source module, the transaction management module, the data session factory module, the data access execution component and the MyBatis scanning mapping configuration module are original modules of the MyBatis-Spring plug-in; The MyBatis mapping scanning configuration module is used for automatically registering the Mapper interface according to the specified package path, and a specific implementation thereof in the Spring framework is as follows: org.mybatis.spring.mapper.MapperScannerConfigurer; The specific implementation of the data source module can select an open source connection pool component; The transaction management module adopts a transaction manager based on a JDBC data source to manage the life cycle of a transaction through an associated data source object, and supports the opening, submission and rollback operations of a transaction; The data session factory module selects a factory class implementation of an ORM framework, is responsible for analyzing Mapper configuration and generating data session instances; The data access execution component adopts a thread-safe ORM template class.
4. The method of claim 1, wherein, MapperScan is an annotation class provided by MyBatis-Spring, which is used to automatically scan all interfaces according to the specified package path, and identify the interface as a MyBatis Mappr interface; Correspondingly, the shielding of the MapperScan annotation includes the following steps: An implementation class P of BeanDefinitionRegistryPostProcessor is declared, which is used to intercept all MyBatis mapping scanning configuration module instances, wherein the BeanDefinitionRegistryPostProcessor interface is a standard interface in the Spring framework for extending Bean definition registration. In the method postProcessBeanDefinitionRegistry of the implementation class P, all MyBatis mapping scanning configuration module instances are obtained, and MyBatis mapping scanning configuration module instances are removed or retained according to the conditions.
5. The method of claim 1, wherein, Under the monolithic architecture, all modules are aggregated in a service, and the data source and Mapper interface scanning path are configured according to the module; Under the microservice architecture, each module is an independent service, and there are two configuration methods as follows: Each microservice only configures the data source and Mapper file scanning path of the current service; Delete the exclusive multi-data source configuration method of the component, and directly use the same configuration as Mybatis official MyBatis-Spring-boot-starter.
6. A multi-data source association component adaptation system, characterized by, The multi-data source association component is based on MyBatis-Spring plug-in construction, and the system includes a data source mapping configuration module, a data source association engine implementation module, a MapperScan annotation shielding module, and a mode switching module; The data source mapping configuration module is used to perform the following: based on the MyBatis-Spring plug-in configuration file, add multi-data sources and the scanning package path corresponding to each data source to form a multi-data source configuration file, and the multi-data source association component supports the configuration file of MyBatis-Spring plug-in in single-data source mode; The data source association engine implementation module is used to perform the following: for each data source, construct an independent MyBatis running environment, including data source, transaction manager, session factory, data access execution, and MyBatis scanning mapping configuration; The shielding module of the MapperScan annotation is configured to perform the following: dynamically managing the MapperScan annotation function provided by MyBatis-Spring, if the attribute scanner-base-packages is configured in the data source, automatically shielding the function of the MapperScan annotation, avoiding the repeated registration of the Mapper class, if the attribute scanner-base-packages is not configured in the data source, using the default MapperScan annotation to scan the Mapper interface; The mode switching module is configured to perform the following: through modifying the build script and the data source configuration file of the program, the smooth switching between the monolithic architecture and the micro-service architecture is realized.
7. The multi-data source correlation component adaptation system of claim 6, wherein, The multi-data source configuration file is in the yaml format, and the multi-data source configuration file defines the data source group definition, the data source driver, the data source URL, the data source username, the data source password and the scanning package path.
8. The multi-data source correlation component adaptation system of claim 6, wherein, The data source association engine implementation module is configured to perform the following: reading the multi-data source configuration file, for each data source, checking whether the data source configuration exists in the multi-data source configuration file, if the data source configuration exists, initializing the data source module, the transaction management module, the data session factory module, the data access execution component and the MyBatis scanning mapping configuration module in sequence, and binding the scanning package path of the current data source configuration and the data access execution component, completing the initialization of the data source association engine; The data source module, the transaction management module, the data session factory module, the data access execution component and the MyBatis scanning mapping configuration module are the original modules of MyBatis-Spring plug-ins; The MyBatis mapping scanning configuration module is configured to automatically register the Mapper interface according to the specified package path, and a specific implementation thereof in the Spring framework is as follows: org.mybatis.spring.mapper.MapperScannerConfigurer; The specific implementation of the data source module can select the open source connection pool component; The transaction management module adopts the transaction manager based on the JDBC data source, and manages the life cycle of the transaction through the associated data source object, supporting the opening, submission and rollback operations of the transaction; The data session factory module selects the factory class of the ORM framework, and is responsible for analyzing the Mapper configuration and generating the data session instance; The data access execution component adopts the thread-safe ORM template class.
9. The multi-data source correlation component adaptation system of claim 6, wherein, MapperScan is an annotation class provided by MyBatis-Spring, which is used to automatically scan all interfaces according to the specified package path, and identify the interfaces as the Mappr interface of MyBatis; Correspondingly, the shielding of the MapperScan annotation includes the following steps: Declare a BeanDefinitionRegistryPostProcessor implementation class P to intercept all MyBatis mapping scan configuration module instances, where the BeanDefinitionRegistryPostProcessor interface is a standard interface in the Spring framework for extending Bean definition registration. In the method postProcessBeanDefinitionRegistry of the implementation class P, get all MyBatis mapping scan configuration module instances, and remove or retain them according to the conditions.
10. The multi-data source correlation component adaptation system of claim 6, wherein, Under the monolithic architecture, all modules are aggregated in a service, and the data source and Mapper interface scanning path are configured according to the module; Under the microservice architecture, each module is an independent service, and there are two configuration methods as follows: Each microservice only configures the data source and Mapper file scanning path of the current service. Delete the dedicated multi-data source configuration method of the component, and directly use the same configuration as Mybatis official MyBatis-Spring-boot-starter.