Mybatisplus-based data source setting method and device, storage medium and management platform
By using MyBatis Plus plugin classes and configuration classes to manage data sources in Spring Boot applications, the problem of data source settings intruding on business code is solved, and efficient processing of multiple data source requests is achieved.
Patent Information
- Application Number
- CN202510891145.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-30
- Publication Date
- 2025-11-07
AI Technical Summary
In existing technologies, Spring Boot-based applications require additional code to be added to the business logic when setting up the data source, resulting in high coupling and the fact that only one data source can be used per business request, which affects efficiency.
By creating a MyBatis Plus plugin class that implements the InnerInterceptor interface and a configuration class that implements the InitializingBean interface, the application can intercept and set the data source during database operations. The data source index and ThreadLocal variables are used to manage the thread data source state, thus achieving separation between the data source and business code.
This separates data source settings from business logic, reduces program coupling, and allows a single business request to use multiple data sources, thus improving request efficiency.
Smart Images

Figure CN120909669A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application relates to the technical field of application programs, and in particular to a data source setting method based on mybatisplus, a computer readable storage medium, a data source setting device based on mybatisplus and a service management platform. BACKGROUND
[0002] To meet increasingly complex business requirements, a project needs to use multiple databases to store data, and therefore, a target database needs to be specified for the database operation. For example, in an application program built based on springboot, a dynamic-datasource is usually used to specify a target database for the database operation, and the dynamic-datasource provides two methods for setting a data source, one is to add a @DS annotation on a class or a method in the business code to declare the set data source, and the other is to use a DynamicDataSourceContextHolder method to set the data source in the business code. However, both methods need to add additional code unrelated to the business in the business code, thereby invading the business and increasing the coupling degree of the business code.
[0003] In addition, in the related art, a method of intercepting a business request and analyzing business parameters is usually used to judge and set a data source, however, this method needs to analyze business parameters, invades the business, and only one data source can be used for one business request, which affects the efficiency of the business request. SUMMARY
[0004] The present application aims to at least solve one of the technical problems in the related art. To this end, the first object of the present application is to provide a data source setting method based on mybatisplus, which can separate the code for setting the data source from the business code, avoid its invasion of the business, reduce the coupling degree of the program, and at the same time, enable multiple data sources to be used for one business request and improve the efficiency of the business request.
[0005] The second object of the present application is to provide a computer readable storage medium.
[0006] The third object of the present application is to provide a data source setting device based on mybatisplus.
[0007] The fourth object of the present application is to provide a service management platform.
[0008] To achieve the above object, the mybatisplus-based data source setting method according to the first aspect of the present application comprises the following steps: defining a data source and creating a mapper interface class according to the requirements of a dynamic-datasource-spring-boot-starter component; creating a mybatisplus plug-in class and implementing an InnerInterceptor interface, so that the application program can intercept database operations through the InnerInterceptor interface; creating a configuration class and implementing an InitializingBean interface, so that the application program registers the mybatisplus plug-in class, so that the application program can set the data source of the current thread through the mybatisplus plug-in class.
[0009] The mybatisplus-based data source setting method according to the embodiment of the present application can set the data source of the current thread through the mybatisplus plug-in class according to the corresponding relationship between the data source and the mapper interface class, so that the code for setting the data source is separated from the business code, the invasion of the business is avoided, the coupling degree of the program is reduced, and one business request can use multiple data sources, thereby improving the efficiency of the business request.
[0010] In addition, the mybatisplus-based data source setting method according to the above embodiment of the present application can have the following additional technical features:
[0011] According to one embodiment of the present application, after defining a data source and creating a mapper interface class according to the requirements of a dynamic-datasource-spring-boot-starter component, the mybatisplus-based data source setting method further comprises the following steps: creating a data source index, which is used to indicate the corresponding relationship between the mapper method in the mapper interface class and the data source.
[0012] According to one embodiment of the present application, creating a data source index comprises the following steps: obtaining the unique name of each data source; creating a folder corresponding to each data source, wherein each folder is named with the unique name of each data source; and setting the mapper interface class in the folder corresponding to the data source to which the mapper interface class belongs.
[0013] According to one embodiment of the application, the mybatisplus-based data source setting method further comprises: in the mybatisplus plug-in class, creating a ThreadLocal <boolean>A global variable of type isSelectDatasource, wherein the global variable isSelectDatasource is used to indicate a setting state of a data source of a current thread.
[0014] According to one embodiment of the present application, the mybatisplus-based data source setting method further comprises the following steps executed in the beforeQuery method and the beforeUpdate method of the mybatisplus plug-in class: obtaining the data source of the current thread by using a DynamicDataSourceContextHolder.peek() method; if the data source of the current thread is empty, obtaining the full path of the mapper interface class according to a parameter MappedStatement of the beforeQuery method or the beforeUpdate method, and intercepting the name of the folder where the mapper interface class is located; setting the name of the folder where the mapper interface class is located as the data source selected by the current thread by using a DynamicDataSourceContextHolder.push() method, and setting the global variable isSelectDatasource to true.
[0015] According to one embodiment of the present application, the mybatisplus-based data source setting method further comprises the following steps executed in the beforePrepare method of the mybatisplus plug-in class: if the global variable isSelectDatasource is true, setting the global variable isSelectDatasource to false, and clearing the data source selected by the current thread by using a DynamicDataSourceContextHolder.poll() method.
[0016] According to one embodiment of the present application, a configuration class is created and an InitializingBean interface is implemented, comprising the following steps executed in a method afterPropertiesSet: creating a List <innerinterceptor>The type of empty plug-in list list is created by the new keyword, an object of the mybatisplus plug-in class is created and added to the first position of the plug-in list list, the mybatisplus existing plug-in list is obtained by the MybatisPlusInterceptor.getInterceptors() method and added to the tail of the plug-in list list, and the MybatisPlusInterceptor.setInterceptors(list) method is called to register all the plug-ins in the plug-in list list.
[0017] To achieve the above purpose, the second aspect embodiment of the present application provides a computer readable storage medium, which stores a mybatisplus-based data source setting program, and the program is executed by a processor to realize the above-mentioned mybatisplus-based data source setting method.
[0018] The computer readable storage medium according to the embodiment of the present application can separate the code for setting the data source from the business code by executing the mybatisplus-based data source setting program stored thereon, avoid the intrusion of the data source setting code on the business, reduce the coupling degree of the program, and enable a business request to use multiple data sources at a time, thereby improving the efficiency of the business request.
[0019] To achieve the above purpose, the third aspect embodiment of the present application provides a mybatisplus-based data source setting device, which comprises: a data source construction module, configured to define a data source and create a mapper interface class according to the requirement of a dynamic-datasource-spring-boot-starter component; a data source setting module, configured to create a mybatisplus plug-in class and implement an InnerInterceptor interface, so that the application program can intercept the database operation through the InnerInterceptor interface; and a mybatisplus class registration module, configured to create a configuration class and implement an InitializingBean interface, so that the application program can register the mybatisplus plug-in class, so that the application program can set the data source of the current thread through the mybatisplus plug-in class.
[0020] According to the mybatisplus-based data source setting device provided by the embodiment of the application, the mybatisplus plug-in class is created, the InnerInterceptor interface is implemented, the configuration class is created, the InitializingBean interface is implemented, the mybatisplus plug-in class is registered by the application, the data source of the current thread is set by the mybatisplus plug-in class according to the corresponding relationship between the data source and the mapper interface class, so that the code for setting the data source is separated from the business code, the invasion of the business is avoided, the coupling degree of the program is reduced, meanwhile, one business request can use multiple data sources, and the efficiency of the business request is improved.
[0021] To achieve the above object, the service management platform provided by the fourth aspect of the embodiment of the application comprises the mybatisplus-based data source setting device of the foregoing embodiment.
[0022] According to the service management platform provided by the embodiment of the application, the mybatisplus-based data source setting device is used, so that the code for setting the data source is separated from the business code, the invasion of the business is avoided, the coupling degree of the program is reduced, meanwhile, one business request can use multiple data sources, and the efficiency of the business request is improved.
[0023] Additional aspects and advantages of the application will be described in the following description, become apparent from the following description, or be learned by practice of the application. BRIEF DESCRIPTION OF DRAWINGS
[0024] Figure 1 The flowchart of the mybatisplus-based data source setting method according to the embodiment of the application is shown in the figure;
[0025] Figure 2 The flowchart of creating the data source index according to the embodiment of the application is shown in the figure;
[0026] Figure 3 The flowchart of setting the data source in the beforeQuery method and the beforeUpdate method according to the embodiment of the application is shown in the figure;
[0027] Figure 4 The flowchart of registering the mybatisplus plug-in class in the afterPropertiesSet method according to the embodiment of the application is shown in the figure;
[0028] Figure 5 The block diagram of the mybatisplus-based data source setting device according to the embodiment of the application is shown in the figure;
[0029] Figure 6 A block schematic diagram of a service management platform according to an embodiment of the present application. DETAILED DESCRIPTION
[0030] Embodiments of the present application are described in detail below with reference to the accompanying drawings, in which like or similar elements or components are denoted by like reference numbers throughout the entire specification. The embodiments described below are examples intended to explain the present application, and are not intended to be limiting of the present application.
[0031] A mybatisplus-based data source setting method, a computer readable storage medium, a mybatisplus-based data source setting device, and a service management platform according to embodiments of the present application are described below with reference to the accompanying drawings.
[0032] Figure 1 A flowchart of a mybatisplus-based data source setting method according to an embodiment of the present application.
[0033] Specifically, in some embodiments of the present application, referring to Figure 1 As shown, the mybatisplus-based data source setting method includes:
[0034] S110, defining a data source and creating a mapper interface class according to the requirement of a dynamic-datasource-spring-boot-starter component.
[0035] It can be understood that when a dynamic-datasource is used to specify a database, a data source is first defined according to the requirement of a dynamic-datasource-spring-boot-starter component, and each data source can correspond to one or a group of databases. The dynamic-datasource sets a data source for database operation according to the data source name, and then specifies a database for database operation. In addition, each database contains one or more data tables. In a mybatisplus application, a mapper interface class is usually created for each data table to facilitate the application to more conveniently operate the database through the mapper interface class. In each mapper interface class, one or more mapper methods are included for performing specific operations such as adding, deleting, modifying, and inquiring on the corresponding data table.
[0036] Specifically, in this embodiment of the present application, when a service needs to operate a database, a service request is sent to the application program, and one or more mapper methods and their parameters are contained in the service request to indicate the specific database operation that the service request wants to perform. It should be understood that the above-mentioned data source, database, data table, mapper interface class and mapper method are uniquely determined, so that when the application program executes the mapper method in the service request, the data source to be accessed by the service request can be determined and set according to the correspondence between the data source, database, data table, mapper interface class and mapper method.
[0037] S120, a mybatisplus plug-in class is created and the InnerInterceptor interface is implemented, so that the application program can intercept the database operation through the InnerInterceptor interface.
[0038] It can be understood that mybatisplus can intercept the database operation of the application program and allow new logic to be inserted in the process of the database operation of the application program through the InnerInterceptor interface, so as to check or adjust the database operation. For example, there are various interception methods in the InnerInterceptor interface, which will be called at different times in the process of the database operation of the application program, for example, the beforeQuery method will be called when the application program is about to perform a database query operation, and for example, the beforeUpdate method will be called when the application program is about to perform a database update operation. Therefore, in this embodiment of the present application, by creating a mybatisplus plug-in class and implementing the InnerInterceptor interface, the interception methods of the InnerInterceptor interface can be used to intercept each database operation, so as to determine and set the data source to be accessed.
[0039] Specifically, each of the interception methods of the InnerInterceptor interface contains a parameter of type MappedStatement, which represents the current intercepted database operation. Each MappedStatement parameter uniquely corresponds to a mapper method, so that by means of the mybatisplus plug-in class, the data source to be accessed by the current intercepted database operation can be determined and set according to the correspondence between the data source, database, data table, mapper interface class, mapper method and MappedStatement parameter.
[0040] In addition, in the embodiment of the application, the application program uses multiple threads to process business requests and perform database operations therein, different database operations can be performed by different threads, and thus when a database operation is intercepted, the data source of the current thread needs to be set, wherein the current thread refers to the thread performing the currently intercepted database operation.
[0041] Thus, by creating the mybatisplus plug-in class and implementing the InnerInterceptor interface, the application program sets the data source of the current thread through the mybatisplus plug-in class to intercept each database operation, and postpones and disperses the timing of judging and setting the data source to when each specific database operation of the business request is about to be performed, so that the business request does not need to be intercepted and the business parameters do not need to be parsed, avoiding the intrusion into the business, and at the same time, the data source is set in units of database operations instead of business requests, so that one business request can use multiple data sources, improving the efficiency of the business request.
[0042] S130, create a configuration class and implement the InitializingBean interface, so that the application program registers the mybatisplus plug-in class to set the data source of the current thread through the mybatisplus plug-in class.
[0043] It can be understood that after the mybatisplus plug-in class is created, the mybatisplus plug-in class is activated by registering the mybatisplus plug-in class in the application program, wherein registering the mybatisplus plug-in class refers to creating an object of the mybatisplus plug-in class in the application program and adding it to the plug-in list of mybatisplus. In addition, in the spring framework, the InitializingBean interface is a callback interface used to perform some custom initialization logic, such as data initialization, resource loading, etc., after the Spring container instantiates the Bean.
[0044] Thus, by creating the configuration class and implementing the InitializingBean interface, and registering the mybatisplus plug-in class in the configuration class, the mybatisplus plug-in class can be activated at the beginning of the application program running, so that the application program can set the data source of the current thread through the mybatisplus plug-in class.
[0045] Further, in some embodiments of the present application, after defining the data source and creating the mapper interface class according to the dynamic-datasource-spring-boot-starter component requirement, the data source setting method based on mybatisplus further comprises: creating a data source index, the data source index being used to indicate the correspondence between the mapper method in the mapper interface class and the data source.
[0046] It can be understood that, in this embodiment of the present application, the data source index is used to indicate the correspondence between the mapper method in the mapper interface class and the data source, so that the application program can directly determine the data source to be accessed according to the mapper method through the data source index, without the need to find the data source step by step according to the correspondence between the data source, the database, the data table, the mapper interface class and the mapper method, thereby simplifying the logic of determining the data source and improving the running efficiency of the application program. It should be understood that the data source index can be in various forms, for example, the correspondence between the mapper method and the data source can be directly stored through a specific table file, or the correspondence between the mapper method and the data source can be indirectly represented through a reasonable file structure.
[0047] Optionally, in some embodiments of the present application, creating the data source index comprises:
[0048] S210, obtaining the unique name of each data source.
[0049] It can be understood that, in dynamic-datasource, the data source name is the unique identifier of the data source, and the data source name is uniquely named when the data source is defined, and therefore, in this embodiment of the present application, the application program needs to set the data source for the database operation according to the data source name through dynamic-datasource.
[0050] S220, creating a folder corresponding to each data source, wherein each folder is named with the unique name of each data source.
[0051] It can be understood that, in this embodiment of the present application, by creating a folder corresponding to each data source and naming each data source with the unique name, the correspondence between the folder and the data source can be established, so that the application program can set the data source according to the folder name through dynamic-datasource.
[0052] S230, setting the mapper interface class in the folder corresponding to the target data source.
[0053] It can be understood that in this embodiment of the application, the mapper interface class is arranged in the folder corresponding to the target data source, the corresponding relationship between the mapper interface class and the folder is established, and then the corresponding relationship between the mapper interface class and the data source is established, so that the application program can judge and set the data source according to the mapper interface class through the dynamic-datasource.
[0054] Therefore, when the mapper method is executed, since the mapper interface class to which the mapper method belongs is in the folder corresponding to the data source name, the name of the data source to which the mapper method belongs can be obtained by obtaining the full path of the mapper method file, so that the application program can judge and set the data source according to the mapper method through the dynamic-datasource, so as to simplify the logic of judging the data source and improve the running efficiency of the application program.
[0055] Further, in some embodiments of the application, the data source setting method based on mybatisplus further comprises: in the mybatisplus plug-in class, creating a ThreadLocal <boolean>a global variable of type isSelectDatasource, wherein the global variable isSelectDatasource is used to indicate the setting state of the data source of the current thread.
[0056] It can be understood that ThreadLocal is a class in Java, and ThreadLocal provides a separate variable copy for each thread, so that each thread can independently operate its own variable copy without affecting the copy of other threads, and therefore, in this embodiment of the application, the ThreadLocal <boolean>The global variable of the type isSelectDatasource indicates the setting state of the data source of the current thread, and can ensure that different threads do not interfere with each other. Specifically, isSelectDatasource includes two states of true and false, which respectively represent two setting states of the data source of the current thread. Among them, the true state indicates that the data source of the current thread has been set, and the false state indicates that the data source of the current thread has not been set. Thus, by creating ThreadLocal <boolean>The global variable of the type isSelectDatasource indicates the setting state of the data source of the current thread, can avoid repeatedly setting the data source of the current thread, prevents errors during application running, and thus improves the robustness of the application.
[0057] Further, in some embodiments of the application, the mybatisplus-based data source setting method further includes the following steps executed in the beforeQuery method and the beforeUpdate method of the mybatisplus plug-in class:
[0058] S310, obtaining the data source of the current thread through the DynamicDataSourceContextHolder.peek() method;
[0059] It can be understood that in this embodiment of the application, the data source of the current thread is obtained through the DynamicDataSourceContextHolder.peek() method, which can determine whether the data source of the current thread is empty, and if the data source of the current thread is not empty, it indicates that the data source of the current thread has been set, so it is not necessary to repeatedly set the data source of the current thread to prevent errors during application running.
[0060] S320, if the data source of the current thread is empty, obtaining the full path of the mapper interface class according to the parameter MappedStatement of the beforeQuery method or the beforeUpdate method, and intercepting the name of the folder where the mapper interface class is located.
[0061] It can be understood that in this embodiment of the application, if the data source of the current thread is not empty, the data source needs to be set for the current thread. Specifically, in the mybatisplus application, the beforeQuery method and the beforeUpdate method are both interception methods in the InnerInterceptor interface, and the interception methods in the InnerInterceptor interface both contain a parameter of the MappedStatement type, which represents the current intercepted database operation, each Mapped Statement parameter uniquely corresponds to a mapper method, and the full path of the mapper method can be obtained through the MappedStatement.getId() method, and correspondingly, the full path of the mapper interface class to which the mapper method belongs can also be obtained. Since the mapper interface class is in the folder corresponding to the data source name, the name of the folder in which the mapper interface class is located can be obtained by intercepting the name of the folder in which the mapper interface class is located in the full path of the mapper interface class, so as to determine the data source name that needs to be set for the current thread.
[0062] In S330, the name of the folder in which the mapper interface class is located is set as the data source selected by the current thread through the DynamicDataSourceContextHolder.push() method, and the global variable isSelectDatasource is set to true.
[0063] It can be understood that in this embodiment of the application, the name of the folder in which the mapper interface class is located is the same as the name of the data source to which the mapper interface class belongs, so that the name of the folder in which the mapper interface class is located is set as the data source selected by the current thread through the DynamicDataSourceContextHolder.push() method, so that the data source to which the mapper interface class belongs can be set as the data source of the current thread, so that the application can access the correct data source.
[0064] In addition, in this embodiment of the application, by setting the global variable isSelectDatasource to true, it can be indicated that the data source of the current thread has been set, so that subsequent repeated setting of the data source of the current thread can be avoided, so as to prevent errors during application running and improve the robustness of the application.
[0065] Further, in some embodiments of the present application, the mybatisplus-based data source setting method further includes: in the beforePrepare method of the mybatisplus plug-in class, if the global variable isSelectDatasource is true, setting the global variable isSelectDatasource to false, and clearing the data source selected by the current thread using the DynamicDataSourceContextHolder.poll() method.
[0066] It should be understood that in this embodiment of the present application, the DynamicDataSourceContextHolder.poll() method can set the data source of the current thread to null. In the mybatisplus application, the beforePrepare method is an interception method in the InnerInterceptor interface, which is used to allow adjustment of database operations in the preparation phase of database operations. Compared with the beforeQuery method and the beforeUpdate method, the beforePrepare method is executed earlier. Therefore, if the global variable isSelectDatasource is true, the global variable isSelectDatasource is set to false in the beforePrepare method, and the data source selected by the current thread is cleared using the DynamicDataSourceContextHolder.poll() method, so that the data source of the current thread is set to null. Further, the setting state of the data source of the current thread is reset to the state that the current thread has not set the data source before the data source of the current thread is set through the beforeQuery method and the beforeUpdate method, so as to eliminate the influence of the previous database operation on the data source of the current thread. Thus, the application error during the running of the application is prevented, and the robustness of the application is improved.
[0067] Further, in some embodiments of the present application, the mybatisplus-based data source setting method further includes: in the beforePrepare method of the mybatisplus plug-in class, if the global variable isSelectDatasource is true, setting the global variable isSelectDatasource to false, and clearing the data source selected by the current thread using the DynamicDataSourceContextHolder.poll() method.
[0068] S410, create a List <innerinterceptor>The type of the empty plug-in list list.
[0069] It can be understood that the afterPropertiesSet method is defined by the InitializingBean interface, and the method is automatically called after the Spring container completes the initialization of the properties of the Bean. Therefore, in this embodiment of the application, by registering the mybatisplus plug-in class in the afterPropertiesSet method, it can be ensured that the mybatisplus plug-in class takes effect at the beginning of the application running, so that the application can set the data source of the current thread through the mybatisplus plug-in class.
[0070] In addition, in this embodiment of the application, the mybatisplus plug-in class is registered through the MybatisPlusInterceptor.setInterceptors() method, and the method accepts a List <innerinterceptor>The type of variable, therefore, needs to be created a List <innerinterceptor>The variable of the type is named list and is used as a parameter of the MybatisPlusInterceptor. setInterceptors() method.
[0071] In S420, an object of the mybatisplus plug-in class is created by using the new keyword and is added to the head of the plug-in list list.
[0072] It can be understood that in this embodiment of the application, the object of the mybatisplus plug-in class is created by using the new keyword and is added to the head of the plug-in list list, so that after the list is used as a parameter to execute the MybatisPlusInterceptor. setInterceptors() method, the mybatisplus plug-in class is enabled.
[0073] In S430, the existing plug-in list of mybatisplus is obtained by using the MybatisPlusInterceptor. getInterceptors() method and is added to the tail of the plug-in list list.
[0074] It can be understood that in this embodiment of the application, the existing plug-in list of mybatisplus is obtained by using the MybatisPlusInterceptor. getInterceptors() method and is added to the tail of the plug-in list list, so that after the list is used as a parameter to execute the MybatisPlusInterceptor. setInterceptors() method, the original mybatisplus plug-in is not disabled.
[0075] In S440, all plug-ins in the plug-in list list are registered by using the MybatisPlusInterceptor. setInterceptors(list) method.
[0076] It can be understood that in this embodiment of the application, all plug-ins in the plug-in list list are registered by using the MybatisPlusInterceptor. setInterceptors(list) method, so that the original mybatisplus plug-in is not disabled and the mybatisplus plug-in class is enabled, thereby enabling the application to set the data source of the current thread by using the mybatisplus plug-in class.
[0077] In summary, according to the mybatisplus-based data source setting method of the embodiment of the present application, the mybatisplus plug-in class is created and the InnerInterceptor interface is implemented, the configuration class is created and the InitializingBean interface is implemented, the application registers the mybatisplus plug-in class, the data source of the current thread is set by the mybatisplus plug-in class according to the corresponding relationship between the data source and the mapper interface class, the code for setting the data source is separated from the business code, the invasion of the business is avoided, the coupling degree of the program is reduced, at the same time, one business request can use multiple data sources, and the efficiency of the business request is improved.
[0078] Based on the mybatisplus-based data source setting method of the aforementioned embodiment of the present application, the present embodiment further proposes a computer readable storage medium, which stores a mybatisplus-based data source setting program. When the program is executed by a processor, the mybatisplus-based data source setting method of the aforementioned embodiment of the present application is realized.
[0079] It should be understood that the specific embodiments of the computer readable storage medium of the present application can refer to the specific embodiments of the mybatisplus-based data source setting method of the aforementioned embodiment of the present application. In order to reduce redundancy, it will not be described here.
[0080] In summary, according to the computer readable storage medium of the embodiment of the present application, by executing the mybatisplus-based data source setting program stored thereon, the application can set the data source of the current thread according to the corresponding relationship between the data source and the mapper interface class through the mybatisplus plug-in class, so that the code for setting the data source is separated from the business code, the invasion of the business is avoided, the coupling degree of the program is reduced, at the same time, one business request can use multiple data sources, and the efficiency of the business request is improved.
[0081] Figure 5 The block diagram of the mybatisplus-based data source setting device according to the embodiment of the present application is shown in FIG. 1.
[0082] Specifically, in some embodiments of the present application, referring to FIG. 1, Figure 5 As shown in FIG. 1, the mybatisplus-based data source setting device 100 includes a data source construction module 10, a data source setting module 20 and a mybatisplus plug-in class registration module 30.
[0083] The data source construction module 10 is configured to define a data source and create a mapper interface class according to a requirement of a dynamic-datasource-spring-boot-starter component; the data source setting module 20 is configured to create a mybatisplus plug-in class and implement an InnerInterceptor interface, so that the application program intercepts the database operation through the InnerInterceptor interface; and the mybatisplus plug-in class registration module 30 is configured to create a configuration class and implement an InitializingBean interface, so that the application program registers the mybatisplus plug-in class, so that the application program sets the data source of the current thread through the mybatisplus plug-in class.
[0084] Further, in some embodiments of the present application, the data source construction module 10 is specifically configured to create a data source index, and the data source index is used to indicate the corresponding relationship between the mapper method in the mapper interface class and the data source.
[0085] Further, in some embodiments of the present application, the data source construction module 10 is specifically configured to obtain a unique name of each data source; create a folder corresponding to each data source, wherein each folder is named by the unique name of each data source; and set the mapper interface class in the folder corresponding to the data source to which the mapper interface class belongs.
[0086] Further, in some embodiments of the present application, the data source setting module 20 is specifically configured to create a ThreadLocal <boolean>a global variable of type isSelectDatasource, wherein the global variable isSelectDatasource is used to indicate a setting state of a data source of a current thread.
[0087] Further, in some embodiments of the present application, the data source setting module 20 is specifically configured to perform the following steps in the beforeQuery method and the beforeUpdate method of the mybatisplus plug-in class: obtaining the data source of the current thread through the DynamicDataSource ContextHolder.peek() method; if the data source of the current thread is empty, obtaining the full path of the mapper interface class according to the parameter MappedStatement of the beforeQuery method or the beforeUpdate method, and intercepting the name of the folder where the mapper interface class is located; setting the name of the folder where the mapper interface class is located as the data source selected by the current thread through the DynamicDataSourceContextHolder.push() method, and setting the global variable isSelectDatasource to true.
[0088] Further, in some embodiments of the present application, the data source setting module 20 is specifically configured to perform the following steps in the beforePrepare method of the mybatisplus plug-in class: if the global variable isSelectDatasource is true, setting the global variable isSelectDatasource to false, and using the DynamicDataSourceContextHolder.poll() method to clear the data source selected by the current thread.
[0089] Further, in some embodiments of the present application, the mybatisplus plug-in class registration module 30 is specifically configured to perform the following steps in the method afterPropertiesSet: creating a List <innerinterceptor>The type of empty plug-in list list is created, an object of the mybatisplus plug-in class is created through the new keyword, and is added to the first position of the plug-in list list. The existing plug-in list of mybatisplus is obtained through the MybatisPlusInterceptor.getInterceptors() method, and is added to the tail of the plug-in list list. The MybatisPlusInterceptor.setInterceptors(list) method is called to register all plug-ins in the plug-in list list.
[0090] It should be understood that the specific implementation of the mybatisplus-based data source setting device of the application can refer to the specific implementation of the mybatisplus-based data source setting method of the application embodiment described above. To reduce redundancy, it will not be repeated here.
[0091] In summary, according to the mybatisplus-based data source setting device of the application embodiment, by creating a mybatisplus plug-in class and implementing an InnerInterceptor interface, and by creating a configuration class and implementing an InitializingBean interface to make the application register the mybatisplus plug-in class, the application can set the data source of the current thread according to the correspondence between the data source and the mapper interface class through the mybatisplus plug-in class, so that the code for setting the data source is separated from the business code, avoiding the intrusion of the business, reducing the coupling degree of the program, and at the same time, allowing a business request to use multiple data sources, improving the efficiency of the business request.
[0092] Figure 6 The block diagram of the service management platform according to the application embodiment is shown.
[0093] Specifically, in some embodiments of the application, referring to Figure 6 As shown in the figure, the service management platform 1000 includes the mybatisplus-based data source setting device 100 of the application embodiment described above.
[0094] It should be understood that the specific implementation of the service management platform 1000 of the application can refer to the specific implementation of the mybatisplus-based data source setting method of the application embodiment described above. To reduce redundancy, it will not be repeated here.
[0095] In summary, according to the service management platform of the embodiment of the present application, the aforementioned service management platform device is adopted, so that the code of setting data source is separated from the business code, the invasion of the business is avoided, the coupling degree of the program is reduced, meanwhile, one business request can use multiple data sources, and the efficiency of the business request is improved.
[0096] It should be noted that the logic and / or steps represented in the flowcharts or otherwise described herein, for example, can be considered as a list of executable instructions for implementing logic functions, and can be embodied in any computer-readable medium for use by or in connection with an instruction execution system, apparatus or device, such as a computer-based system, a system that includes a processor, or other system that can fetch the instructions from the instruction execution system, apparatus or device and execute the instructions, or in conjunction with these instruction execution systems, apparatus or devices. For the purpose of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transport a program for use by or in connection with an instruction execution system, apparatus or device, or in conjunction with these instruction execution systems, apparatus or devices. More specific examples (non-exhaustive list) of computer-readable medium include the following: electrical connections having one or more wires (electronic devices), portable computer diskette (magnetic devices), random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), fiber optics devices, and portable compact disc read-only memory (CDROM). In addition, the computer-readable medium can even be paper or other suitable medium on which the program can be printed, as the program can be electronically obtained, for example, by optical scanning of the paper or other medium, followed by electronic conversion of the obtained program into a computer-readable medium, and then storing the program in a computer memory if necessary.
[0097] It should be understood that parts of the present application can be realized in hardware, software, firmware or a combination thereof. In the above-described embodiments, a plurality of steps or methods can be realized by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if realized in hardware, and as in another embodiment, any one or a combination of the following technologies known in the art can be used: discrete logic circuit with logic gates for implementing logic functions on data signals, application specific integrated circuit with suitable combination logic gates, programmable gate array (PGA), field programmable gate array (FPGA), etc.
[0098] In the description of the application, the description of the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in connection with the embodiment or example are included in at least one embodiment or example of the application. In the description of the application, the illustrative description of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described can be combined in any one or more embodiments or examples in a suitable manner.
[0099] In the description of the application, it should be understood that the orientation or positional relationship indicated by the terms "center", "longitudinal", "transverse", "length", "width", "thickness", "upper", "lower", "front", "rear", "left", "right", "vertical", "horizontal", "top", "bottom", "inner", "outer", "clockwise", "counterclockwise", "axial", "radial", "circumferential" and the like are based on the orientation or positional relationship shown in the drawings, and are only for the purpose of facilitating the description of the application and simplifying the description, and do not indicate or imply that the device or element referred to must have a particular orientation, be constructed and operated in a particular orientation, and therefore cannot be understood as a limitation of the application.
[0100] In addition, the terms "first", "second" are only for the purpose of description, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Therefore, the features defined with "first", "second" can explicitly or implicitly include at least one of the features. In the description of the application, the meaning of "a plurality of" is at least two, for example, two, three, etc., unless otherwise specifically limited.
[0101] In the present application, unless otherwise specifically defined and limited, the terms "mounting", "connection", "connection", "fixing" and the like should be broadly understood, for example, it can be fixed connection, or detachable connection, or integral; it can be mechanical connection, or electrical connection; it can be directly connected, or indirectly connected through an intermediate medium, it can be the internal communication of two elements or the interaction relationship between two elements, unless otherwise specifically limited. For those skilled in the art, the specific meaning of the above terms in the present application can be understood according to the specific circumstances.
[0102] In the present application, unless otherwise explicitly specified and limited, the first feature is "on" or "under" the second feature can be that the first and second features are in direct contact, or the first and second features are in indirect contact through an intermediate medium. Moreover, the first feature is "above", "over" and "on top of" the second feature can be that the first feature is directly above or obliquely above the second feature, or simply means that the first feature is horizontally higher than the second feature. The first feature is "under", "below" and "underneath" the second feature can be that the first feature is directly below or obliquely below the second feature, or simply means that the first feature is horizontally lower than the second feature.
[0103] Although the embodiments of the present application have been shown and described above, it should be understood by those skilled in the art that the above-described embodiments are exemplary and are not to be construed as limiting the present application, and those skilled in the art can make changes, modifications, replacements and variations to the above-described embodiments within the scope of the present application.< / innerinterceptor> < / boolean> < / innerinterceptor> < / innerinterceptor> < / innerinterceptor> < / boolean> < / boolean> < / boolean> < / innerinterceptor> < / boolean>
Claims
1. A mybatisplus-based data source setting method, characterized in that, The method comprises: According to the dynamic-datasource-spring-boot-starter component requirement, define the data source and create the mapper interface class; Create a mybatisplus plug-in class and implement the InnerInterceptor interface, so that the application intercepts the database operation through the InnerInterc eptor interface; Create a configuration class and implement the InitializingBean interface, so that the application registers the mybatisplus plug-in class, so that the application sets the data source of the current thread through the mybatisplus plug-in class.
2. The mybatisplus-based data source setting method according to claim 1, characterized in that, After defining the data source according to the dynamic-datasource-spring-boot-starter component requirement and creating the mapper interface class, the method further comprises: Create a data source index, which is used to indicate the corresponding relationship between the mapper method in the mapper interface class and the data source.
3. The mybatisplus-based data source setting method according to claim 2, characterized in that, The method further comprises: Obtain the unique name of each data source; Create a folder corresponding to each data source, wherein each folder is named with the unique name of each data source; Set the mapper interface class in the folder corresponding to the data source to which it belongs.
4. The mybatisplus-based data source setting method according to claim 3, characterized in that, The method further comprises: In the mybatisplus plug-in class, create ThreadLocal <boolean>A global variable isSelectDat asource of type, wherein the global variable isSelectDatasource is used to indicate the setting state of the data source of the current thread.< / boolean> 5. The mybatisplus-based data source setting method according to claim 4, characterized in that, The method further comprises: In the beforeQuery method and the beforeUpdate method of the mybatisplus plug-in class, the following steps are performed: Obtain the data source of the current thread through the DynamicDataSourceContextHolder.peek() method; If the data source of the current thread is empty, obtain the full path of the mapper interface class according to the parameter MappedStatement of the beforeQuery method or the beforeUpdate method, and intercept the name of the folder where the mapper interface class is located; Set the name of the folder where the mapper interface class is located as the data source selected by the current thread through the DynamicDataSourceContextHolder.push() method, and set the global variable isSelectDatasource to true.
6. The mybatisplus-based data source setting method according to claim 5, characterized in that, The method further comprises: In the beforePrepare method of the mybatisplus plug-in class, the following steps are performed: If the global variable isSelectDatasource is true, the global variable isSelectDatasource is set to false, and the current thread selected data source is cleared by using the DynamicDataSourceContextHolder.poll() method.
7. The mybatisplus-based data source setting method according to claim 1, characterized in that, The creating configuration class and implementing InitializingBean interface, including: The following steps are performed in the method afterPropertiesSet: Create a List <innerinterceptor>An empty plug-in list list of the type;< / innerinterceptor> An object of the mybatisplus plug-in class is created by the new keyword, and is added to the first position of the plug-in list list; The existing plug-in list of mybatisplus is obtained by the MybatisPlusInterceptor.getInterceptors() method, and is added to the tail of the plug-in list list; The MybatisPlusInterceptor.setInterceptors(list) method is called to register all plug-ins in the plug-in list list.
8. A computer-readable storage medium, characterized in that, The processor executes the mybatisplus-based data source setting program stored thereon to implement the mybatisplus-based data source setting method of any one of claims 1-7.
9. A mybatisplus-based data source setting device, characterized by, The device comprises: A data source construction module for defining data sources and creating mapper interface classes according to the requirements of dynamic-datasource-spring-boot-starter components; A data source setting module for creating a mybatisplus plug-in class and implementing an InnerInterceptor interface, so that the application program intercepts database operations through the InnerInterceptor interface; A mybatisplus plug-in class registration module for creating a configuration class and implementing an InitializingBean interface, so that the application program registers the mybatisplus plug-in class, so that the application program sets the data source of the current thread through the mybatisplus plug-in class.
10. A service management platform, characterized by, The service management platform comprises the mybatisplus-based data source setting device of claim 9.