CN-122018951-A - Database entity refreshing method, system and storage medium
Abstract
Monitoring the change of a table structure of a database to obtain the structural metadata of each data table and the corresponding last change time stamp; the method comprises the steps of comparing each last change time stamp with a corresponding refreshing time stamp of a local cache, judging a target data table with a changed structure based on comparison, triggering a dynamic code generation process aiming at the target data table in response to the target data table, dynamically generating entity class codes and data access layer component codes corresponding to the target data table based on target structure metadata of the target data table and a predefined code generation template, loading the entity class codes and the data access layer component codes into an application running environment through a dynamic class loading mechanism to replace or register corresponding class definitions, and updating refreshing time stamp information corresponding to the target data table in the local cache after the dynamic class loading is completed. The application can maintain the database efficiently and at low cost.
Inventors
- XUAN YULONG
- WANG WEIDONG
- LI AIZHU
Assignees
- 杭州紫钺科技有限公司
Dates
- Publication Date
- 20260512
- Application Date
- 20260129
Claims (10)
- 1. A method for refreshing a database entity, the method comprising: monitoring the table structure change of the database to obtain the structure metadata of each data table and the corresponding last change time stamp; Comparing each last change time stamp with a corresponding refresh time stamp of a local cache, and judging a target data table with a changed structure based on the comparison; Triggering a dynamic code generation process for the target data table in response to the target data table; Dynamically generating entity class codes and data access layer component codes corresponding to the target data table based on the target structure metadata of the target data table and a predefined code generation template; loading the entity class code and the data access layer component code into an application running environment through a dynamic class loading mechanism to replace or register corresponding class definitions; And updating refreshing time stamp information corresponding to the target data table in the local cache after the dynamic class loading is completed.
- 2. The method of claim 1, wherein the predefined code generation template comprises a predefined entity class code template and a predefined data access layer component template, and wherein dynamically generating entity class codes and data access layer component codes corresponding to the target data table based on target structure metadata of the target data table and the predefined code generation template comprises: analyzing the target structure metadata to obtain table names, field types and constraint condition information of the target data table; combining the table name, field type and constraint condition information with a predefined entity class code template to generate an entity class code which accords with the JavaBean specification and contains corresponding field definitions and notes; Generating a data access layer component code integrated from a generic base mapper interface and corresponding to the target data table based on the entity class code and a predefined data access layer component template, wherein the data access layer component code contains a generic parameter declaration based on the entity class code.
- 3. The method of claim 2, wherein loading the entity class code and data access layer component code into an application runtime environment via a dynamic class loading mechanism to replace or register a corresponding class definition comprises: compiling and loading the entity class code and the data access layer component code by using a Groovy class loader to generate a corresponding first class object; Registering the first class object in a Spring application context to generate a corresponding Bean definition; Judging whether a second class object corresponding to the target data table exists in the Spring application context or not, wherein the second class object is a registered class object before triggering the dynamic code generation process; If the first class object exists, replacing the second class object by the version control mechanism, and updating the Bean definition in the Spring application context to maintain the dependency injection relation; And if not, the first class object is used as a new Bean definition to finish registration and dependency injection.
- 4. The method of claim 1, wherein updating refresh timestamp information in a local cache corresponding to the target data table after completion of the dynamic class loading comprises: Acquiring the identification information of the target data table, and inquiring a database system table based on the identification information to obtain the latest change time stamp of the target data table; And replacing the original refreshing time stamp record corresponding to the target data table in the local cache by the latest change time stamp through an atomic write operation.
- 5. The method of claim 1, wherein monitoring table structure changes of the database to obtain structure metadata and corresponding last change timestamps for each data table comprises: Periodically querying a database system table to obtain a version information set containing the last modified time stamp of each data table; Comparing the version information set with the version information cached in the previous period to screen out a data table updated by the last modified timestamp as a table to be checked; Obtaining the current complete structural metadata of the to-be-checked table, and performing difference comparison with the pre-stored historical structural metadata to identify a structural change type; and when the structure change type is identified, the last modified timestamp is used as a valid last change timestamp.
- 6. The method according to claim 1, wherein the method further comprises: Reading a system configuration parameter set comprising a database connection parameter set, a code generation parameter set, a cache management parameter set and a performance tuning parameter set from a predefined configuration source; initializing a database connection manager instance based on the database connection parameter set; Initializing instantiation parameters of the predefined code generation template based on the set of code generation parameters; initializing a storage policy and an invalidation policy of the local cache based on the cache management parameter set; Initializing a thread pool and batch processing parameters related to the monitoring, dynamic code generation and dynamic class loading processes based on the performance tuning parameter set; And applying the initialized manager instances and parameter configuration to corresponding modules.
- 7. The method of claim 3, wherein replacing the first type of object with the second type of object via a version control mechanism comprises: generating a new version identifier for the first class object and reserving a history version identifier for the second class object; Establishing parallel Bean definition registration in a Spring application context based on the new version identifier and the history version identifier, and routing a newly received service request to a first type object corresponding to the new version identifier for processing; maintaining the stock service request which is received but not processed by the second class object to be continuously processed until the second class object is completed; And after the storage service requests are all processed and the first row of objects run stably, removing the Bean definition corresponding to the second class from the Spring application context and recovering the memory resources occupied by the Bean definition.
- 8. The method according to claim 2, wherein the method further comprises: Automatically assembling a complete CRUD operation interface corresponding to the target data table based on the entity class code and the data access layer component code; wherein the automatic assembly comprises: Analyzing the entity class code to obtain an entity class type, and unlocking the data access layer component code to obtain a mapper type integrated with a universal basic mapper interface; Based on the entity class type and the mapper type, performing type binding through the declared Java generic parameters in the data access layer component code to generate a service layer component code with type safety, wherein the service layer component code encapsulates calling logic of a predefined add-delete-modify-check method in the mapper type; and loading and registering the service layer component codes into a Spring application context through the dynamic class loading mechanism to form a complete data access link from the entity layer, the mapping layer to the service layer.
- 9. A database entity refreshing system is characterized by comprising a monitoring module, a processing module, a response module and a refreshing module, wherein, The monitoring module is used for monitoring the table structure change of the database to obtain the structure metadata of each data table and the corresponding last change time stamp; the processing module is used for comparing each last change time stamp with a corresponding refresh time stamp of the local cache respectively and judging a target data table with a changed structure based on the comparison; the response module is used for responding to the target data table to trigger a dynamic code generation process aiming at the target data table; The refreshing module is used for dynamically generating entity class codes and data access layer component codes corresponding to the target data table based on the target structure metadata of the target data table and a predefined code generation template, loading the entity class codes and the data access layer component codes into an application running environment through a dynamic class loading mechanism to replace or register corresponding class definitions, and updating refreshing time stamp information corresponding to the target data table in a local cache after the dynamic class loading is completed.
- 10. A computer readable storage medium having stored thereon a computer program executable on a processor, wherein the computer program when executed by the processor implements a database entity refreshing method according to any of claims 1 to 8.
Description
Database entity refreshing method, system and storage medium Technical Field The present application relates to the field of database management and application development technologies, and in particular, to a method, a system, and a storage medium for refreshing a database entity. Background In database driven application development, changes in database table structure are common scenarios as business needs change. In the traditional development mode, when the database table structure is changed, a developer needs to manually modify the corresponding entity class, the data access layer and related business logic codes, and then recompile, package and restart the application to realize the consistency of the codes and the database structure. The method is low in efficiency, human errors are easy to introduce, and the application service terminal can be caused to influence the usability of the system. To reduce the impact of the restart, several improvements have emerged in the prior art, such as using hot deployment tools to implement runtime updating of portions of code, or using reflection mechanisms to dynamically handle field changes. However, hot deployment tools often require additional payment and are complex to configure and difficult to use in a wide variety of environments. The reflection-based mode has certain flexibility, but has poor performance, lacks type security check at compile time and is easy to cause abnormal running. In addition, the method can not automatically generate complete codes of the data access layer and the service layer, manual intervention is still needed, and the problems of low development efficiency and high maintenance cost can not be fundamentally solved. Disclosure of Invention To enable efficient and low cost maintenance of databases. The embodiment of the application provides a database entity refreshing method, a system and a medium. In a first aspect, a method for refreshing a database entity is provided, the method comprising: monitoring the table structure change of the database to obtain the structure metadata of each data table and the corresponding last change time stamp; Comparing each last change time stamp with a corresponding refresh time stamp of a local cache, and judging a target data table with a changed structure based on the comparison; Triggering a dynamic code generation process for the target data table in response to the target data table; Dynamically generating entity class codes and data access layer component codes corresponding to the target data table based on the target structure metadata of the target data table and a predefined code generation template; loading the entity class code and the data access layer component code into an application running environment through a dynamic class loading mechanism to replace or register corresponding class definitions; And updating refreshing time stamp information corresponding to the target data table in the local cache after the dynamic class loading is completed. In some embodiments, the predefined code generation template includes a predefined entity class code template and a predefined data access layer component template, and the dynamically generating entity class code and data access layer component code corresponding to the target data table based on the target structure metadata of the target data table and the predefined code generation template includes: analyzing the target structure metadata to obtain table names, field types and constraint condition information of the target data table; combining the table name, field type and constraint condition information with a predefined entity class code template to generate an entity class code which accords with the JavaBean specification and contains corresponding field definitions and notes; Generating a data access layer component code integrated from a generic base mapper interface and corresponding to the target data table based on the entity class code and a predefined data access layer component template, wherein the data access layer component code contains a generic parameter declaration based on the entity class code. In some of these embodiments, the loading the entity class code and data access layer component code into the application runtime environment to replace or register the corresponding class definition by a dynamic class loading mechanism includes: compiling and loading the entity class code and the data access layer component code by using a Groovy class loader to generate a corresponding first class object; Registering the first class object in a Spring application context to generate a corresponding Bean definition; Judging whether a second class object corresponding to the target data table exists in the Spring application context or not, wherein the second class object is a registered class object before triggering the dynamic code generation process; If the first class object exists, replacing the second class