A method and system for generating database table creation statements based on JAVA annotations

By using annotations in Java code to design the database and generate database table building statements, the problem of traditional slow development speed is solved, and the effect of rapid development and convenient modification is achieved.

CN113742337BActive Publication Date: 2025-09-02SI-TECH INFORMATION TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202110880827.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-02
Publication Date
2025-09-02
Estimated Expiration
2041-08-02

AI Technical Summary

Technical Problem

During the development process of existing traditional databases, the development speed is slow and the development efficiency is low, which cannot meet the rapid development needs of docker and microservice Java projects.

Method used

In Java code, the database is designed by annotating and annotating, and the Java reflection function is used to generate database table building statements, including annotating class and field information with @TableDesc and @TableFieldDesc, and the database table and field information are generated through template code scanning, and finally the table building statement is generated.

Benefits of technology

It realizes the direct design of the database during Java code development, saves developers time, improves development efficiency, supports the rapid generation of table building statements, and can easily compare differences after the code is modified.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113742337B_ABST
    Figure CN113742337B_ABST
Patent Text Reader

Abstract

The present invention provides a method and system for generating database table creation statements based on JAVA annotations, relating to the technical field of database development. The method comprises: sequentially marking classes requiring database table generation and methods requiring database table field generation in Java code; writing template code to scan the Java code; generating database table information based on the marked classes requiring database table generation; generating table field information based on the marked methods requiring database table field generation; and generating table creation statements based on the database table information and table field information. During the Java code writing process, the present invention directly designs the database, marking classes requiring database table generation and methods requiring database table field generation on fields within the classes in the form of annotations in the Java code. Furthermore, after development is complete, the Java reflection function is utilized to directly obtain database table creation statements by scanning the Java code, thereby significantly saving developer time.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of database development, and in particular to a method and system for generating database table creation statements based on JAVA annotations. Background Art

[0002] As database-based Java projects become more and more abundant, Java projects are moving towards Dockerization and microservices, so there are higher requirements for rapid development.

[0003] The current traditional development process is to design the database structure first and then develop the code based on the database structure. This serial development process makes the development speed slow and the development efficiency low. Summary of the Invention

[0004] In response to the above problems, the present invention provides a method and system for generating database table creation statements based on JAVA annotations. During the process of writing Java code, the database is designed at the same time. After the Java code development is completed, the database table creation statements can be obtained by annotation marking, and then the database can be obtained, which saves a lot of developer time.

[0005] To achieve the above objectives, the present invention provides a source code depth monitoring method for JAVA components, comprising:

[0006] Mark the classes that need to generate database tables and the methods that need to generate database table fields in the Java code in sequence;

[0007] Write template code to scan the Java code;

[0008] Generate database table information according to the marked class that needs to generate database table;

[0009] Generate table field information according to the method for generating database table fields as marked;

[0010] Generate a table creation statement according to the database table information and the table field information.

[0011] As a further improvement of the present invention, the @TableDesc annotation is used to mark the class for which a database table needs to be generated; and the @TableFieldDesc annotation is used to mark the method for which a database table field needs to be generated.

[0012] As a further improvement of the present invention, the @TableDesc annotation includes three configurations: Name, Node and isPublic;

[0013] Among them, Name represents the name of the database table, Node represents the Chinese name or remarks of the database table, and isPublic indicates whether a synonym needs to be created.

[0014] As a further improvement of the present invention, the @TableFieldDesc annotation includes four configurations: Index, Type, Length and Note;

[0015] Among them, Index indicates whether an index needs to be created, or the type of index; Type indicates the type of database field; Length indicates the length of the database field; Note indicates the Chinese name or remarks of the database field.

[0016] As a further improvement of the present invention, the classes that need to be compared with the database table and the methods that need to be compared with the database table fields are marked in the Java code in sequence;

[0017] Write template code to scan the Java code;

[0018] Generate database table classes and methods that need to generate database table fields according to the marked needs, and generate database table related information according to the classes and methods that need to compare differences with database tables;

[0019] The relevant information of the database table is compared with the relevant information of the generated table in the database.

[0020] The present invention also provides a system for generating database table creation statements based on JAVA annotations, comprising: a marking module, a scanning module, a database table information generation module, a table field generation module and a table creation statement generation module;

[0021] The marking module is used to:

[0022] Mark the classes that need to generate database tables and the methods that need to generate database table fields in the Java code in sequence;

[0023] The scanning module is used to:

[0024] Write template code to scan the Java code;

[0025] The database table information generation module is used to:

[0026] Generate database table information according to the marked class that needs to generate database table;

[0027] The database table information generation module is used to:

[0028] Generate table field information according to the method for generating database table fields as marked;

[0029] The table creation statement generation module is used to:

[0030] Generate a table creation statement according to the database table information and the table field information.

[0031] As a further improvement of the present invention, the @TableDesc annotation is used to mark the class for which a database table needs to be generated; and the @TableFieldDesc annotation is used to mark the method for which a database table field needs to be generated.

[0032] As a further improvement of the present invention, the @TableDesc annotation includes three configurations: Name, Node and isPublic;

[0033] Among them, Name represents the name of the database table, Node represents the Chinese name or remarks of the database table, and isPublic indicates whether a synonym needs to be created.

[0034] As a further improvement of the present invention, the @TableFieldDesc annotation includes four configurations: Index, Type, Length and Note;

[0035] Among them, Index indicates whether an index needs to be created, or the type of index; Type indicates the type of database field; Length indicates the length of the database field; Note indicates the Chinese name or remarks of the database field.

[0036] As a further improvement of the present invention, a comparison module is also included;

[0037] The marking module is further used to:

[0038] In the Java code, mark the classes that need to be compared with the database table and the methods that need to be compared with the database table fields.

[0039] The database table information generation module is further used to:

[0040] Generate database table information according to the marked classes for which database tables need to be generated and the classes for which differences are to be compared with the database tables;

[0041] The database table information generation module is used to:

[0042] Methods for generating database table fields according to the needs of annotation and methods for comparing differences with database table fields to generate relevant information of the database table;

[0043] The comparison module is used to:

[0044] The relevant information of the database table is compared with the relevant information of the generated table in the database.

[0045] Compared with the prior art, the present invention has the following beneficial effects:

[0046] The present invention directly designs the database during the Java code writing process, annotates the classes that need to generate database tables in the Java code and annotates the methods that need to generate database table fields on the fields in the class through annotations, and then uses the Java reflection function after the development is completed to directly obtain the database table creation statement by scanning the Java code, and then obtain the database, thereby saving a lot of developer time and realizing the rapid generation of table creation statements according to the Java code.

[0047] In the present invention, after the Java code is written, in the process of modifying and adding the Java code, the classes that need to be compared with the database table are marked, and the fields in the class are marked with the methods that need to be compared with the database table fields. Then, after the Java code is modified and added, the modified database table information is obtained, which is convenient for comparison with the generated table information in the database, thereby helping to modify the database table. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] Figure 1 A flow chart of a method for generating database table creation statements based on JAVA annotations disclosed in one embodiment of the present invention;

[0049] Figure 2 A schematic diagram of a system for generating database table creation statements based on JAVA annotations disclosed in an embodiment of the present invention;

[0050] Figure 3 A detailed flow chart of a method for generating database table creation statements based on JAVA annotations disclosed in one embodiment of the present invention;

[0051] Figure 4 A detailed flow chart of comparing database table information in code with generated table information in the database based on JAVA annotations disclosed in one embodiment of the present invention. DETAILED DESCRIPTION

[0052] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.

[0053] The present invention is described in further detail below with reference to the accompanying drawings:

[0054] like Figure 1 、 3 As shown, the present invention provides a method for generating database table creation statements based on JAVA annotations, including:

[0055] S1. Mark the classes that need to generate database tables and the methods that need to generate database table fields in the Java code in sequence;

[0056] in,

[0057] Use @TableDesc annotation to mark the class that needs to generate database table; use @TableFieldDesc annotation to mark the method that needs to generate database table field.

[0058] Further,

[0059] The @TableDesc annotation includes three configurations: Name, Node, and isPublic; Name represents the name of the database table, Node represents the Chinese name or remarks of the database table, and isPublic indicates whether a synonym needs to be created.

[0060] The @TableFieldDesc annotation includes four configurations: Index, Type, Length, and Note. Index indicates whether an index needs to be created, or the type of index. Type indicates the type of database field. Length indicates the length of the database field. Note indicates the Chinese name or note of the database field.

[0061] S2. Write template code to scan Java code;

[0062] S3. Generate database table information according to the class of the database table required by the annotation;

[0063] Among them, include:

[0064] Get the name of the database table;

[0065] S4. Generate table field information according to the method of generating database table fields according to the needs of annotation;

[0066] Among them, include:

[0067] Get the field name, type, and length of the database table.

[0068] S5. Generate a table creation statement based on the database table information and table field information.

[0069] Among them, according to acquisition needs, basic operation SQL can also be obtained based on database table information and table field information.

[0070] In the present invention, the classes that need to be compared with the database table and the methods that need to be compared with the database table fields are marked in the Java code in sequence;

[0071] Write template code to scan Java code;

[0072] like Figure 4 As shown, the class of the database table needs to be generated according to the @TableDesc annotation, the method of the database table field needs to be generated according to the @TableFieldDesc annotation, the class of the database table needs to be compared with the difference of the database table according to the @TableDesc annotation, and the method of the database table field needs to be compared with the difference of the database table according to the @TableFieldDesc annotation to generate the relevant information of the database table;

[0073] At the same time, the dictionary of the relevant data tables in the database is loaded, the dictionary of the relevant data fields in the database is loaded, and the relevant information of the generated table in the database is obtained;

[0074] Compare the relevant information of the database table with the relevant information of the generated table in the database; if the comparison results are inconsistent, provide modification suggestions, such as adding the missing difference content to the relevant information of the generated table in the database.

[0075] like Figure 2 As shown, the present invention also provides a system for generating database table creation statements based on JAVA annotations, including: a marking module, a scanning module, a database table information generation module, a table field generation module, a table creation statement generation module and a comparison module;

[0076] Annotation module for:

[0077] In the Java code, mark the class that needs to generate the database table, the method that needs to generate the database table field, the class that needs to be compared with the database table, and the method that needs to be compared with the database table field.

[0078] in,

[0079] Use @TableDesc annotation to mark the class that needs to generate database table; use @TableFieldDesc annotation to mark the method that needs to generate database table field.

[0080] The @TableDesc annotation includes three configurations: Name, Node, and isPublic; Name represents the name of the database table, Node represents the Chinese name or remarks of the database table, and isPublic indicates whether a synonym needs to be created.

[0081] The @TableFieldDesc annotation includes four configurations: Index, Type, Length, and Note;

[0082] Index indicates whether an index needs to be created, or the type of index; Type indicates the type of database field; Length indicates the length of the database field; Note indicates the Chinese name or remarks of the database field.

[0083] Scanning module for:

[0084] Write template code to scan Java code;

[0085] Database table information generation module, used for:

[0086] When there is no mark in the Java code that needs to be compared with the database table, the database table information is generated according to the marked class;

[0087] When the Java code also has classes marked with differences that need to be compared with the database table, the database table class is generated according to the marked needs, and the database table information is generated for the classes that need to be compared with the database table;

[0088] Database table information generation module, used for:

[0089] When there is no method in the Java code that needs to be compared with the database table field, the table field information is generated according to the method of generating the database table field required by the annotation;

[0090] When the Java code is marked with a method that needs to be compared with the database table field, the method of generating the database table field and the method that needs to be compared with the database table field are generated according to the marked needs;

[0091] Table creation statement generation module, used for:

[0092] Generate table creation statements based on database table information and table field information.

[0093] Comparison module for:

[0094] Compare the relevant information of the database table with the relevant information of the generated table in the database;

[0095] If the comparison results are inconsistent, modification suggestions will be given, such as adding the missing difference content to the relevant information of the generated table in the database.

[0096] Advantages of the present invention:

[0097] (1) In the process of writing Java code, the database is designed directly. The classes that need to generate database tables and the methods that need to generate database table fields are marked in the Java code through annotations. After the development is completed, the Java reflection function is used to directly obtain the database table creation statement by scanning the Java code, and then the database is obtained. This saves a lot of developer time and realizes the rapid generation of table creation statements based on Java code.

[0098] (2) After the Java code is written, in the process of modifying and adding the Java code, the classes that need to be compared with the database table are marked, and the methods that need to be compared with the database table fields are marked on the fields in the class. Then, after the Java code is modified and added, the modified database table information is obtained, which is convenient for comparison with the generated table information in the database, helping to modify the database table.

[0099] The above are merely preferred embodiments of the present invention and are not intended to limit the present invention. Those skilled in the art will readily appreciate that various modifications and variations of the present invention are possible. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of the present invention shall be included within the scope of protection of the present invention.

Claims

1. A method for generating database table creation statements based on JAVA annotations, characterized in that: include: In the Java code, mark the class that needs to generate the database table and the method that needs to generate the database table field in sequence; among them, use the @TableDesc annotation to mark the class that needs to generate the database table; use the @TableFieldDesc annotation to mark the method that needs to generate the database table field; the @TableDesc annotation includes three configurations: Name, Node and isPublic; among them, Name represents the name of the database table, Node represents the Chinese name or remarks of the database table, and isPublic indicates whether a synonym needs to be established; the @TableFieldDesc annotation includes four configurations: Index, Type, Length and Note; among them, Index indicates whether an index needs to be established, or the type of index; Type indicates the type of the database field; Length indicates the length of the database field; Note indicates the Chinese name or remarks of the database field; Write template code to scan the Java code; Generate database table information according to the marked class that needs to generate database table; Generate table field information according to the method for generating database table fields as marked; Generate a table creation statement according to the database table information and the table field information; as well as, In the Java code, mark the classes that need to be compared with the database table and the methods that need to be compared with the database table fields. Write template code to scan the Java code; Generate database table classes and methods that need to generate database table fields according to the marked needs, and generate database table related information according to the classes and methods that need to compare differences with database tables; Comparing the relevant information of the database table with the relevant information of the generated table in the database; If the comparison results are inconsistent, modification suggestions will be given, such as adding the missing difference content to the relevant information of the generated table in the database.

2. A system for implementing the method according to claim 1, characterized in that: include: Annotation module, scanning module, database table information generation module, table field generation module and table creation statement generation module; The marking module is used to: Mark the classes that need to generate database tables and the methods that need to generate database table fields in the Java code in sequence; The scanning module is used to: Write template code to scan the Java code; The database table information generation module is used to: Generate database table information according to the marked class that needs to generate database table; The database table information generation module is used to: Generate table field information according to the method for generating database table fields as marked; The table creation statement generation module is used to: Generate a table creation statement according to the database table information and the table field information.

3. The system according to claim 2, characterized in that Use @TableDesc annotation to mark the class that needs to generate database table; use @TableFieldDesc annotation to mark the method that needs to generate database table field.

4. The system according to claim 3, wherein: The @TableDesc annotation includes three configurations: Name, Node, and isPublic; Among them, Name represents the name of the database table, Node represents the Chinese name or remarks of the database table, and isPublic indicates whether a synonym needs to be created.

5. The system according to claim 4, characterized in that: The @TableFieldDesc annotation includes four configurations: Index, Type, Length, and Note; Among them, Index indicates whether an index needs to be created, or the type of index; Type indicates the type of database field; Length indicates the length of the database field; Note indicates the Chinese name or remarks of the database field.

6. The system according to claim 2, characterized in that: Also includes a comparison module; The marking module is further used to: In the Java code, mark the classes that need to be compared with the database table and the methods that need to be compared with the database table fields. The database table information generation module is further used to: Generate database table information according to the marked classes for which database tables need to be generated and the classes for which differences are to be compared with the database tables; The database table information generation module is used to: Methods for generating database table fields according to the needs of annotation and methods for comparing differences with database table fields to generate relevant information of the database table; The comparison module is used to: The relevant information of the database table is compared with the relevant information of the generated table in the database.

Citation Information

Patent Citations

  • Method and configuration for generating query condition in annotation way

    CN103020064A