Adaptive database type and development environment automatic maintenance method and application thereof

By using a custom class BaseEntity and automatically generating SQL statements from database metadata, the system solves the maintenance difficulties and database type mismatch issues when the database table structure changes, achieving efficient automatic database maintenance.

CN115905183BActive Publication Date: 2026-02-13CITY CLOUD TECH HANGZHOU CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211667844.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-22
Publication Date
2026-02-13
Estimated Expiration
2042-12-22

AI Technical Summary

Technical Problem

Existing technologies are difficult to maintain and prone to errors when database table structures change, and there are problems with data field type mismatch and SQL statement incompatibility when switching between different databases.

Method used

By using a custom class BaseEntity to indicate whether a database table is mapped, the database driver type and metadata are automatically obtained, and SQL statements are generated based on the mapping relationship for automatic maintenance.

Benefits of technology

It enables precise maintenance across different development environments and databases, reducing the workload of manually maintaining database tables and modifying SQL fields, and improving development and deployment efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115905183B_ABST
    Figure CN115905183B_ABST
Patent Text Reader

Abstract

The application provides an automatic maintenance method of self-adapting database type and development environment and application thereof, including the following steps: newly defining a custom abstract class Base Entity to define common fields and define abstract methods; if it is needed to be mapped to a database, an entity class is newly defined and the Base Entity class is inherited through an extends keyword; different database dialects are selected according to different types of database drivers, and the mapping between database field types and Java basic types is established respectively; when an application to be maintained is started, all classes inheriting the Base Entity class and corresponding attributes are acquired, the class name is converted into a data table table name, the attribute name is converted into a field name and the Java basic type corresponding to the attribute, comparison is made according to the acquired results, corresponding SQL language for maintaining the data table is generated and executed. Automatic maintenance can be realized.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of java database, in particular to an automatic maintenance method of adaptive database type and development environment and application thereof. BACKGROUND

[0002] In the daily development process of Java programmers, new function test is usually completed in the local development environment and then the new function is merged into the formal production environment after passing. Generally speaking, new function means that there is a change in the data table in the database supporting the function, which may be adding several data tables, or the fields of the original data table are changed, etc. In today's increasingly strict localization, there may also be a problem of automatic adaptation to different databases before switching.

[0003] When the data table and the data table field change, the existing technology generally synchronizes the database table structure by recording the SQL statement. This way is very troublesome to maintain, and it is very easy to forget, and often only when the production environment has a problem, it is found that the program is wrong because of missing a certain field. Secondly, in today's database localization, more and more applications need to adapt to domestic databases such as Dream Database and Renmin University Gold Warehouse Database. When the application is deployed on different databases, there will be a mismatch between the data field types of different databases, and the SQL statement is incompatible.

[0004] Therefore, an automatic maintenance method of adaptive database type and development environment is urgently needed to solve the problems existing in the prior art. SUMMARY

[0005] The embodiment of the present application provides an automatic maintenance method of adaptive database type and development environment and application thereof, which solves the problem that the prior art is prone to errors.

[0006] The core technology of the present application is to create a custom class BaseEntity, which is used to mark whether to automatically map to the database table name; for the class that needs to be one-to-one mapped with the database table, it needs to inherit the custom class BaseEntity, if not, it does not need to; automatically obtain the database driver type loaded by the application, so as to obtain the database dialect corresponding to each kind of database; obtain the database meta information corresponding to the application; obtain the data table information and the data table field information from the database meta information; obtain all classes in the application that inherit the custom class BaseEntity, associate the default table name and the class name according to the mapping mode, and load the corresponding database dialect according to the database driver type, and automatically check the database table and the data table field. Thus, the automatic maintenance of the database is realized.

[0007] In a first aspect, the application provides an automatic maintenance method for self-adapting to database types and development environments, comprising the following steps:

[0008] S00, a custom abstract class Base Entity is newly created to define common fields and define an abstract method for judging whether to be mapped to a database;

[0009] S10, if it is needed to be mapped to a database, an entity class is newly created and the Base Entity class is inherited through an extends keyword;

[0010] S20, different database dialects are selected according to different types of database drivers, and a mapping between database field types and Java basic types is established respectively;

[0011] S30, when an application to be maintained is started, all classes inheriting the Base Entity class and corresponding attributes are acquired, the class name is converted into a data table table name, the attribute name is converted into a field name and a Java basic type corresponding to the attribute;

[0012] S40, a SQL language for maintaining a data table is generated according to the acquired result and is executed to realize automatic maintenance.

[0013] Further, in the step S10, whether it is needed to be mapped to a database is judged through a return value after the abstract method is rewritten.

[0014] Further, in the step S20, a database dialect corresponding to each type of database is acquired through a database driver type loaded by the application to be maintained.

[0015] Further, in the step S20, a database type used by the application to be maintained and database metadata are acquired, and all table names in the database metadata are stored into a table name set.

[0016] Further, in the step S20, the table name and all fields of the corresponding data table are stored into a Map of a key-Value structure, taking the table name as the key and the corresponding all fields as the Value, to form a meta field list.

[0017] Further, in the step S30, the class name is converted into a data table table name and the attribute is converted into a field name through a default camel case naming rule.

[0018] In a second aspect, the application provides an automatic maintenance device for self-adapting to database types and development environments, comprising:

[0019] A definition module is configured to newly create a custom abstract class Base Entity to define common fields and define an abstract method for judging whether to be mapped to a database.

[0020] The processing module is configured to create a new entity class and inherit the BaseEntity class through an extends keyword when mapping to a database is required; and configured to obtain all classes inheriting the BaseEntity class and corresponding attributes when an application to be maintained is started, and convert the class name into a data table table name, convert the attribute name into a field name and a Java basic type corresponding to the attribute;

[0021] The mapping module is configured to select different database dialects according to different types of database drivers, and establish a mapping between a database field type and a Java basic type.

[0022] The maintenance module is configured to compare the obtained results to generate corresponding SQL language for maintaining a data table and perform the SQL language to realize automatic maintenance.

[0023] In a third aspect, the present application provides an electronic device including a memory and a processor, the memory storing a computer program, and the processor being configured to run the computer program to execute the automatic maintenance method of the adaptive database type and development environment.

[0024] In a fourth aspect, the present application provides a readable storage medium storing a computer program, the computer program including program code for controlling a process to execute the process, and the process including the automatic maintenance method of the adaptive database type and development environment.

[0025] The main contributions and innovations of the present application are as follows: 1. Compared with the prior art, the present application can realize accurate maintenance of a database in different development environments, and can switch between different databases without affecting the running of an application program.

[0026] 2. Compared with the prior art, the present application can discard the traditional maintenance method through a sql statement. Especially in team development, team members only need to maintain the code, and no longer need to maintain the sql modification record of the database table and the field, greatly improving the development efficiency and the efficiency of application deployment and upgrading.

[0027] The details of one or more embodiments of the present application are set forth in the following drawings and description to make other features, objects and advantages of the present application more clear. BRIEF DESCRIPTION OF DRAWINGS

[0028] The drawings described herein are used to provide further understanding of the present application, and form a part of the present application. The schematic embodiments of the present application and the description thereof are used to explain the present application, and do not constitute an improper limitation on the present application. In the drawings:

[0029] Figure 1is a flow of an adaptive database type and automatic maintenance method of a development environment according to an embodiment of the present application;

[0030] Figure 2 is a schematic diagram of one embodiment of the present application;

[0031] Figure 3 is an example of one embodiment of the present application Figure 1 ;

[0032] Figure 4 is an example of one embodiment of the present application Figure 2 ;

[0033] Figure 5 is a schematic diagram of a hardware structure of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0034] The exemplary embodiments will be described in detail herein below with reference to the drawings. In the following description, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments are not meant to represent all embodiments consistent with one or more embodiments of the present description. Rather, they are merely examples of apparatuses and methods consistent with some aspects of one or more embodiments of the present description as detailed in the appended claims.

[0035] It should be noted that the steps of the methods in other embodiments are not necessarily performed in the order shown and described in the present description. In some other embodiments, the steps of the methods can be more or less than those described in the present description. Furthermore, a single step described in the present description can be broken down into multiple steps in other embodiments; and multiple steps described in the present description can be combined into a single step in other embodiments.

[0036] The current technology has the problems of maintenance trouble and error-prone, and cannot change for different database switching, data table and data table field change.

[0037] Based on this, the present application is based on an adaptive database type and a database automatic maintenance method of a development environment to solve the problems of the prior art.

[0038] Embodiment one

[0039] The application aims to propose a database automatic maintenance method of adaptive database type and development environment, which can realize accurate maintenance of the database in different development environments, and can switch between different databases without affecting the running of the application. In particular, this way can abandon the traditional maintenance method through sql statements. Especially in team development, team members only need to maintain the code, and no longer need to maintain the sql modification record of the database table and field, which greatly improves the development efficiency and the efficiency of application deployment and upgrade.

[0040] Specifically, the embodiment of the application provides an automatic maintenance method of adaptive database type and development environment, referring to Figure 1 , the method comprises the following steps:

[0041] S00, a custom abstract class Base Entity is newly created (in order to let the abstract method of the subclass have a default value) to define a public field, and an abstract method is defined to judge whether to be mapped to a database;

[0042] In the embodiment, a primary key field id, an AddTime field, an UpdateTime field and an abstract method mappingToDb for judging whether to be mapped to a database are newly created. That is, a class that does not need to be mapped needs to be judged according to whether the table name is in the TableNameList set. If yes, the SQL statement is assembled for maintenance; if not, it is directly ended.

[0043] S10, if it needs to be mapped to a database, an entity class (each private attribute of the entity class corresponds to a field of a data table, and the naming rule is also according to the camel case naming rule) is newly created and the Base Entity class is inherited through the extends keyword;

[0044] In the embodiment, the entity class is newly created and the Base Entity class is inherited through the extends keyword, and the abstract method mappingToDb in the parent class is rewritten, and it is judged according to the return value of the method whether to be mapped to the database.

[0045] Among them, the public attribute can be extracted for reuse through Base Entity, improving the efficiency; and through the Java inheritance mechanism, it is equivalent to labeling each subclass, no matter which package the subclass is in, so that the subclass can be quickly found when the system starts. This is the biggest difference from the traditional annotation method, and the annotation method has strong code intrusion and is not convenient for code maintenance.

[0046] S20, different database dialects are selected according to different types of database drivers, and the mapping between the database field types and the Java basic types is established respectively;

[0047] In the embodiment, a mapping DBMap of database types and database dialects is loaded into the memory. That is, different table creation statements, modification statements and database types are used according to different types of databases.

[0048] Preferably, all subclasses of the BaseEntity class using the extends keyword in the application are acquired, and the abstract method mappingToDb in the subclasses is acquired to determine whether the entity class needs to be mapped to the database. All private attributes in the subclasses are acquired by reflection, and a mapping of the subclass name and the FieldList set of the subclass attributes is established, wherein the subclass attribute name is also automatically converted based on the camel case naming rule.

[0049] S30, when the application to be maintained is started, all classes inheriting the BaseEntity class and corresponding attributes are acquired, and the class name is converted into a data table table name, the attribute name is converted into a field name, and the Java basic type corresponding to the attribute is acquired;

[0050] In the embodiment, the database type DBType and database metadata used in the application are acquired, mainly including table information and field information of the database, and all table names of the database are stored in a collection named TableNameList, and the table name and all fields corresponding to the table are stored in a Key-Value structure Map, wherein the Key is the table name, and the Value is the MetaFieldList set of all fields corresponding to the table.

[0051] Preferably, the database dialect is acquired from the DBMap according to the database type DBType. The data table name tableName is automatically converted according to the class name and the camel case naming rule of the database table acquired in the foregoing step, and it is determined whether the tableName exists in the TableNameList set in step 5. If not, it is indicated that the table does not exist in the database at present, and it is determined according to the value of the mappingToDb method. If the value of the mappingToDb method is true, the table creation statement is established according to the database dialect and the mapping of the subclass name and the subclass attribute set in step 104. If the value of the mappingToDb method is false, the table is removed from the database, as shown in the following table. Figure 2

[0052] S40, according to the acquired result, comparison is performed to generate corresponding maintenance data table SQL language and execute to realize automatic maintenance.

[0053] ​Preferably, to further illustrate the technical solution of this application, the following example is provided. For instance, there is an entity class called UserEntity that inherits from BaseEntity using the extends keyword, and the application database is MySQL. Figure 3 and Figure 4 As shown, according to the UserEntity class, the mappingToDb method has a value of true.

[0054] So, according to the specific steps of this application or Figure 2 The steps reveal that the database table name is `user_entity`, and the field set `FieldList` contains six fields: username, password, nickname, id, add_time, and update_time. First, it's necessary to check if `user_entity` is in `TableNameList`. If it doesn't exist, the table needs to be created in the database. Since the database type is MySQL, the MySQL dialect is loaded. Based on the table name, field set, and database type, a table creation statement is assembled:

[0055] CREATE TABLE `user_entity`(

[0056] "id"int(11)NOT NULL AUTO_INCREMENT COMMENT'Primary Key',

[0057] "username"varchar(255)DEFAULT NOT NULL,

[0058] "password"varchar(255)DEFAULT NOT NULL,

[0059] "nickname"varchar(255)DEFAULT NOT NULL,

[0060] "add_time"datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation Time',

[0061] "update_time" datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Modified time');

[0062] If the data table needs to be deleted from the database, a delete table statement is assembled according to the table name and the database type:

[0063] DROP TABLE IF EXISTS`user_entity`;

[0064] When the UserEntity class adds a phone field, a modification statement is assembled according to the table name, the field set, and the database type:

[0065] ALTER TABLE`user_entity`ADD COLUMN`phone`int(11)NULL;

[0066] Similarly, when the database type is Oracle or other types of databases, different database dialects are loaded to complete the assembly of SQL statements. Finally, the assembled statements are executed to complete the automatic maintenance of the database in different database types and different development environments.

[0067] In this way, the adaptive database type and development environment database automatic maintenance method of the present application is completed.

[0068] Among them, Figure 2 The specific implementation process of the method of the present application is as shown in the figure:

[0069] Step one, obtain the database table name tableName according to the entity class name (the default is to adopt the camel case naming rule);

[0070] Step two, judge whether the tableName is in the TableNameList set (the TableNameList set is created last time);

[0071] Step three, if yes, output true or false through the default abstract method mappingToDb; if no (it is indicated that the table does not exist in the current database), output true or false through the default abstract method mappingToDb;

[0072] Step four, if true, execute the field set MetaFieldList and the entity class attribute set FieldList, the difference set of MetaFieldList and FieldList, the field deletion and field addition statement assembly, and the execution of all SQL statements in sequence; if false, assemble a deletion statement according to the table name and the database type, and re-execute step one; (corresponding to if yes, output true or false through the default abstract method mappingToDb)

[0073] If the output is true, a table building statement is assembled according to the table name, the field set and the database type; if the output is false, step one is re-executed.

[0074] Embodiment two

[0075] Based on the same concept, the application further provides an automatic maintenance device for adaptive database types and development environments, comprising:

[0076] A definition module is configured to newly define a custom abstract class Base Entity to define common fields and define an abstract method for judging whether to be mapped to a database.

[0077] A processing module is configured to newly define an entity class and inherit the Base Entity class through an extends keyword when the entity class needs to be mapped to a database, and to acquire all classes inheriting the Base Entity class and corresponding attributes when an application to be maintained is started, and convert the class name into a data table table name, convert the attribute name into a field name and a Java basic type corresponding to the attribute.

[0078] A mapping module is configured to select different database dialects according to different types of database drivers, and establish a mapping between database field types and Java basic types.

[0079] A maintenance module is configured to compare the acquired results to generate a corresponding SQL language for maintaining a data table and execute the SQL language to realize automatic maintenance.

[0080] Embodiment three

[0081] The embodiment further provides an electronic device, referring to Figure 5 , comprising a memory 404 and a processor 402, the memory 404 stores a computer program, and the processor 402 is configured to run the computer program to execute the steps in any of the method embodiments.

[0082] Specifically, the processor 402 can include a central processing unit (CPU), or a specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the application.

[0083] The memory 404 can include mass storage for data or instructions. By way of example, and not limitation, the memory 404 can include a Hard Disk Drive (HDD), a floppy disk drive, a solid-state drive (SSD), flash memory, a USB drive, a magneto-optical disk, optical disks such as CD-ROMs or DVDs, magnetic tapes, or other types of storage media, which can be removable or non-removable (or fixed). The memory 404 can include a combination of two or more of these types of storage media. In suitable cases, the memory 404 can be internal or external to the data processing apparatus. In particular embodiments, the memory 404 is a Non-Volatile memory. In particular embodiments, the memory 404 includes Read-Only Memory (ROM) and Random-Access Memory (RAM). In suitable cases, the ROM can be a mask-programmed ROM, a Programmable ROM (PROM), an Erasable PROM (EPROM), an Electrically EPROM (EEPROM), an Electrically Alterable ROM (EAROM), or a FLASH, or a combination of two or more of these. In suitable cases, the RAM can be a Static Random-Access Memory (SRAM) or a Dynamic Random-Access Memory (DRAM), which can be a Fast Page Mode Dynamic Random-Access Memory (FPMDRAM), an Extended Data Out Dynamic Random-Access Memory (EDODRAM), a Synchronous Dynamic Random-Access Memory (SDRAM), or the like.

[0084] The memory 404 can be used to store or buffer various data files needed for processing and / or communication, and possible computer program instructions executed by the processor 402.

[0085] The processor 402 reads and executes the computer program instructions stored in the memory 404 to implement any of the adaptive database type and automatic maintenance method of development environment in the above embodiments.

[0086] Optionally, the electronic device can further include a transmission device 406 connected to the processor 402 and an input / output device 408 connected to the processor 402.

[0087] The transmission device 406 can be used to receive or send data via a network. The network can include a wired or wireless network provided by a communication provider of the electronic device. In one example, the transmission device includes a network adapter (NIC) which can be connected to other network devices through a base station to communicate with the Internet. In one example, the transmission device 406 can be a radio frequency (RF) module for communicating with the Internet through wireless means.

[0088] The input / output device 408 is used to input or output information. In the embodiment, the input information can be an automatic maintenance start instruction, and the output information can be an automatic maintenance result.

[0089] Embodiment four

[0090] The embodiment also provides a readable storage medium, and the readable storage medium stores a computer program. The computer program includes program codes for controlling a process to execute the process. The process includes the automatic maintenance method of the adaptive database type and the development environment according to the first embodiment.

[0091] It should be noted that the specific examples in the embodiment can refer to the examples described in the above embodiments and optional implementation manners, which will not be described herein again.

[0092] In general, the various embodiments can be implemented in hardware or special purpose circuits, software, logic or any combination thereof. Some aspects of the application can be implemented in hardware, while other aspects can be implemented in firmware or software which can be executed by a controller, microprocessor or other computing device, but the application is not limited thereto. While various aspects of the application can be illustrated and described as block diagrams, flow charts, or using some other pictorial representation, it is well understood that these blocks, apparatus, systems, techniques or methods described herein can be implemented in, as non-limiting examples, hardware, software, firmware, special purpose circuits or logic, general purpose hardware or controler or other computing devices, or some combination thereof.

[0093] Embodiments of the application can be implemented by computer software executable by a data processor of the mobile device such as in the processor entity, or by hardware, or by a combination of software and hardware. Computer software or program, also called program product, including software routines, applets and / or macros, can be stored in any apparatus-readable data storage medium and they comprise program instructions to perform particular tasks. The program product can include one or more computer-executable components such as by the program instructions of one or more applications, or portions thereof. The one or more computer-executable components can be one or more of initial applications, utility programs, background services, system services, program libraries, and so on.The one or more computer-executable components can be stored on the physical media as physical media computer instructions, or loaded into memory from another computer readable medium. The one or more computer-executable components can include, but are not limited to, software for implementation of the embodiments. The program instructions can be implemented in a high level procedural or object oriented programming language, or in assembly or machine language. The software can be

[0094] It should be apparent to those skilled in the art that the above-described embodiments of the present application can be modified in various ways without departing from the scope of the present application. Accordingly, the above-described embodiments should be considered illustrative only and not restrictive in all aspects. The scope of the present application should be considered in the light of the appended claims.

[0095] The above embodiments are only some of the embodiments of the present application, which are described in detail and specifically, but should not be understood as limiting the scope of the present application. It should be noted that for those skilled in the art, some modifications and improvements can be made without departing from the concept of the present application, and these modifications and improvements should be considered within the scope of the present application. Therefore, the scope of the present application should be determined by the appended claims.

Claims

1. A method for automatic maintenance of adaptive database types and development environments, characterized in that, Includes the following steps: S00. Create a new custom abstract class BaseEntity to define public fields and abstract methods to determine whether to map to the database. S10. If mapping to a database is required, create a new entity class and inherit the BaseEntity class using the extends keyword; S20. Select different database dialects according to different types of database drivers, and establish mappings between database field types and Java basic types respectively; S30. When the application to be maintained starts, obtain all classes that inherit from the BaseEntity class and their corresponding attributes, and convert the class names into table names and the attribute names into field names and the corresponding Java basic types. S40. Compare the obtained results to generate the corresponding SQL language for maintaining the data tables and execute it to achieve automatic maintenance.

2. The method for automatic maintenance of adaptive database types and development environments of claim 1, wherein, In step S10, the return value of the rewritten abstract method is used to determine whether it needs to be mapped to the database.

3. The method for automatic maintenance of adaptive database types and development environments of claim 1, wherein, In step S20, the database dialect corresponding to each database is obtained by obtaining the database driver type loaded by the application to be maintained.

4. The method for automatic maintenance of adaptive database types and development environments of claim 1, wherein, In step S20, by obtaining the database type and database metadata used by the application to be maintained, all table names in the database metadata are stored in a table name set.

5. The method for automatic maintenance of adaptive database types and development environments of claim 4, wherein, In step S20, the table name and all fields of the corresponding data table are stored in a key-value structure Map, with the table name as the key and all corresponding fields as the value, forming a meta-field list.

6. The method for automatic maintenance of adaptive database types and development environments of claim 1, wherein, In step S30, class names are converted into table names and attributes are converted into field names using the default camelCase naming convention.

7. An automatic maintenance device of adaptive database types and development environments, characterized by, include: Define the module to create a new custom abstract class BaseEntity to define public fields and abstract methods to determine whether to map to the database; The processing module is used to create new entity classes and inherit the BaseEntity class through the extends keyword when mapping to the database is required; it is used to obtain all classes that inherit the BaseEntity class and their corresponding attributes when the application to be maintained starts, and convert the class names into table names and the attribute names into field names and the corresponding Java basic types. The mapping module is used to select different database dialects based on different types of database drivers and to establish mappings between database field types and Java primitive types respectively; The maintenance module is used to compare the obtained results, generate the corresponding SQL language for maintaining the data tables, and execute it to achieve automatic maintenance. 8.An electronic device comprising a memory and a processor, the electronic device comprising: The memory stores a computer program, and the processor is configured to run the computer program to perform the automatic maintenance method for adaptive database types and development environments as described in any one of claims 1 to 6.

9. A readable storage medium, characterized by, The readable storage medium stores a computer program, the computer program including program code for controlling a process to execute the process, the process including an automatic maintenance method for adaptive database types and development environments according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Database architecture method and frame

    CN107818146A

  • Database interaction method and device, equipment and storage medium

    CN114328467A