Automatic code generation method and device, computer equipment and storage medium

Through automated code generation methods, the database table structure is parsed and the target code is generated, which solves the problems of inefficiency and consistency of manual code writing, and realizes efficient and accurate code development.

CN119512561APending Publication Date: 2025-02-25成都赛美特信息科技有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411581650.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-07
Publication Date
2025-02-25

AI Technical Summary

Technical Problem

During the software development process, manually writing database code is inefficient and error-prone, especially when the database table structure is complex, resulting in inconsistent with the database table structure, reducing development efficiency and ensuring the effectiveness and accuracy of the code.

Method used

It provides an automated code generation method, which generates database table information files by analyzing the database table structure, extracts database table modeling information and field information, and generates target codes that comply with coding specifications based on this information to ensure the consistency between the code and the database table structure.

Benefits of technology

Automatic code generation methods significantly improve development efficiency, reduce errors caused by manual code writing, ensure the validity and accuracy of the code, and enhance the readability of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119512561A_ABST
    Figure CN119512561A_ABST
Patent Text Reader

Abstract

The invention provides an automatic code generation method and device, computer equipment and a storage medium, a database table information file is generated based on a database table structure, and the database table information file comprises database table modeling information and database table field information. The database table field information comprises a full-write name and an abbreviation name of a database field; analyzing the database table information file to obtain database table modeling information and database table field information; target codes are generated on the basis of the database table modeling information and the database table field information, and abbreviation names of database fields appear in entity classes of the target codes in the form of full-write names of the abbreviation names. By adopting the method, the code development efficiency is improved, and the effectiveness and accuracy of codes are ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software development, and in particular to an automatic code generation method, device, computer equipment and storage medium. Background Art

[0002] In the software development process, database design and code implementation are two closely linked links. The traditional approach is to first complete the design of the database table in the database design tool, and then the programmer manually writes the corresponding code entity.

[0003] However, the study found that this manual writing method is not only inefficient but also prone to errors, especially when the database table structure is complex and has many fields. It is more likely to cause inconsistencies between the code entity and the database table structure, thereby reducing the efficiency of code development and failing to ensure the effectiveness and accuracy of the code. Summary of the invention

[0004] In view of this, an object of the present invention is to provide an automatic code generation method, apparatus, computer equipment and storage medium to improve code development efficiency and ensure the effectiveness and accuracy of the code.

[0005] In a first aspect, an embodiment of the present application provides an automatic code generation method, the method comprising:

[0006] Generate a database table information file based on the database table structure, wherein the database table information file includes database table modeling information and database table field information, and the database table field information includes the full name and abbreviated name of the database field;

[0007] Parsing the database table information file to obtain the database table modeling information and the database table field information;

[0008] A target code is generated based on the database table modeling information and the database table field information, wherein the abbreviated names of the database fields appear in the form of their full names in the entity class of the target code.

[0009] Optionally, the parsing the database table information file to obtain the database table modeling information and the database table field information includes:

[0010] Write parsing programs;

[0011] The database table information file is parsed by the parsing program.

[0012] Optionally, generating the target code based on the database table modeling information and the database table field information includes:

[0013] Configure the path of the database table information file, the basic package name of the target code and the database table;

[0014] The target code is generated based on the path of the database table information file, the basic package name of the target code and the database table execution code modeling main method.

[0015] In a second aspect, an embodiment of the present application provides an automatic code generation device, the device comprising:

[0016] A database table information file generation module, used to generate a database table information file based on the database table structure, wherein the database table information file includes database table modeling information and database table field information, and the database table field information includes the full name and abbreviated name of the database field;

[0017] A database table information file parsing module, used for parsing the database table information file to obtain the database table modeling information and the database table field information;

[0018] A target code generation module is used to generate target code based on the database table modeling information and the database table field information, wherein the abbreviated name of the database field appears in the entity class of the target code in the form of its full name.

[0019] Optionally, the parsing the database table information file to obtain the database table modeling information and the database table field information includes:

[0020] Write parsing programs;

[0021] The database table information file is parsed by the parsing program.

[0022] Optionally, generating the target code based on the database table modeling information and the database table field information includes:

[0023] Configure the path of the database table information file, the basic package name of the target code and the database table;

[0024] The target code is generated based on the path of the database table information file, the basic package name of the target code and the database table execution code modeling main method.

[0025] In a third aspect, an embodiment of the present application provides a computer device, comprising: a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the computer device is running, the processor and the memory communicate through the bus, and when the machine-readable instructions are executed by the processor, the steps of the automatic code generation method described in any optional implementation manner of the first aspect above are performed.

[0026] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the steps of the automatic code generation method described in any optional implementation manner of the first aspect above are executed.

[0027] The technical solution provided by this application includes but is not limited to the following beneficial effects:

[0028] The automatic code generation method provided in this application can automatically parse the .pdm file generated by Power Designer, extract the database table structure and field information therein, and generate a code entity that complies with the coding specification based on this information. By automatically generating code entities, the tedious process of manually writing code can be avoided, and the development efficiency is significantly improved. The automatic generation of code entities can ensure the consistency of the code entity with the database table structure, reducing the errors caused by manual code writing. In addition, the abbreviations of the database fields are presented in full in the code, which enhances the readability of the code and enables developers to more intuitively understand the meaning and purpose of the fields.

[0029] By adopting the automated code development method provided by this application, developers do not need to frequently regenerate entity code and front-end code due to changes in database fields during the project development process, and manually modify the entity fields in the code to full write form, thereby avoiding this cumbersome and error-prone process. This application automatically generates code based on Power Designer, which not only simplifies the code modeling process, but also cleverly meets the dual needs of database field abbreviations and full write of code entity fields. This method significantly improves programming efficiency and enhances the readability of the code. It can quickly and completely generate code that meets project requirements, greatly improving the efficiency and quality of code development.

[0030] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.

[0032] Figure 1 A flowchart of an automatic code generation method provided by Embodiment 1 of the present invention is shown;

[0033] Figure 2 A flowchart of a method for performing code modeling based on Power Designer provided in the first embodiment of the present invention is shown;

[0034] Figure 3 A flowchart of an information file parsing method provided by Embodiment 1 of the present invention is shown;

[0035] Figure 4 A flow chart of a method for generating target code provided by the first embodiment of the present invention is shown;

[0036] Figure 5 A flowchart of a code generation method based on Power Designer provided in the first embodiment of the present invention is shown;

[0037] Figure 6 A schematic diagram of the structure of an automatic code generation device provided by Embodiment 2 of the present invention is shown;

[0038] Figure 7 A schematic diagram of the structure of a computer device provided in Embodiment 3 of the present invention is shown. DETAILED DESCRIPTION

[0039] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. The components of the embodiments of the present invention generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the claimed invention, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present invention.

[0040] Embodiment 1

[0041] To facilitate understanding of this application, Figure 1 The flowchart of an automatic code generation method provided by the first embodiment of the present invention is shown to describe the contents of the first embodiment of the present application in detail.

[0042] See also Figure 1 As shown, Figure 1 A flowchart of an automatic code generation method provided by Embodiment 1 of the present invention is shown, wherein the method comprises steps S101 to S103:

[0043] S101: Generate a database table information file based on a database table structure, wherein the database table information file includes database table modeling information and database table field information, and the database table field information includes a full name and an abbreviated name of a database field.

[0044] Specifically, the database table structure is constructed, and the database table is designed using the Power Designer tool based on the database table structure. After the Power Designer modeling is completed, two files will be generated, with the suffixes .pdb and .pdm. The file with the suffix .pdm is an XML file, which contains the complete modeling information of the tbl_employee_info database table, that is, the database table modeling information, as well as the full text and abbreviation of the fields, that is, the database table field information.

[0045] In the database table information file generated by the Power Designer tool, the English name of the database table can be obtained from the following nodes in the xml file:

[0046] <o:talbe> <a:code> tbl_employee_info< / a:code>

[0047] The full field name can be obtained from the following fields:

[0048] <c:columns> <a:name> employee_code< / a:name> < / c:columns>

[0049] The field abbreviation name can be obtained from the following fields:

[0050] <c:columns> <a:code> empl_cd< / a:code> < / c:columns>

[0051] S102: Parse the database table information file to obtain the database table modeling information and database table field information.

[0052] Specifically, the above database table information file is parsed to obtain the database table modeling information and database table field information, and stored in List <tableinfo>List <tableinfo>A data structure in Java, representing a list of multiple TableInfo objects.

[0053] S103: Generate target code based on the database table modeling information and the database table field information, wherein the abbreviated name of the database field appears in the entity class of the target code in the form of its full name.

[0054] Specifically, configure the path where .pdm is located, the basic package name of the generated code, and specify the table name "tbl_employee_info" for which code is to be generated, execute the code modeling main method, and start generating code.

[0055] The abbreviated field Code, i.e. the abbreviated name, is used to create database tables to comply with database naming standards and character length restrictions; while the full field Name, i.e. the full name, is used to generate model templates, controllers, services, XML configuration files, and front-end page codes. In the generated code, all code entity fields are presented in full form, which can enhance the readability and maintainability of the code. This ensures that the full fields are presented in the code, while the data in the database remains in the abbreviated fields, meeting the requirements of specific projects.

[0056] Since both the abbreviations of database fields and the full-text requirements of code entities are taken into consideration, all code entity fields are presented in full text in the generated code, which significantly enhances the readability of the code and enables developers to understand the meaning and purpose of the fields more intuitively.

[0057] See also Figure 2 As shown, Figure 2 The flowchart of a method for code modeling based on Power Designer provided in the first embodiment of the present invention is shown, wherein the table is first designed using Power Designer, and after reading the .pdm file, the model, controller, service, xml code and front-end page code are modeled and generated based on the modeling tool "using code generation tool class" to complete code generation. After the table is designed using Power Designer in this application, the SQL script can also be exported, the database can be created, and the SQL script can be imported into the database, so that the code can be generated directly based on the Power Designer modeling tool instead of directly based on the database structure.

[0058] In an alternative embodiment, see Figure 3 As shown, Figure 3 A flowchart of an information file parsing method provided in the first embodiment of the present invention is shown, wherein the database table information file is parsed to obtain the database table modeling information and database table field information, including steps S301 to S302:

[0059] S301: Write a parsing program.

[0060] S302: parsing the database table information file through the parsing program.

[0061] Specifically, write a parsing program, the processing logic of the parsing program is as follows:

[0062] First, a public class named PowerDesignerPdmXmlParse is defined to encapsulate the logic of parsing .pdm files. This class has two private member variables: xmlPath is used to store the path of the XML file, and document is used to store the parsed XML document object. Next, the XML document is read through the getPdmXmlDocument method. This method accepts a string parameter xmlPath, which represents the path of the .pdm file, and creates a new Document object to read and parse the content of the XML file, and then returns this Document object to achieve the reading of the XML document.

[0063] Then, the getXmlTableInformation method is used to parse the XML file and extract the table information. This method accepts two parameters, where xmlPath represents the path of the .pdm file and tableNames is a string array representing the table name to be parsed (multiple table names are supported). This method returns a List<T> <tableinfo>Object, which contains the English name, Chinese name, primary key, field information, etc. of the specified table.

[0064] In the specific implementation, parse the mes.pdm file to obtain the English name, Chinese name, primary key, full name of the field, abbreviation of the field, field type, field remarks and other information of the specified table; encapsulate the field information obtained by the above analysis into the TableColumn entity, which is a custom class used to store detailed information of the field; encapsulate TableColumn into TableInfo, which is a custom class used to store detailed information of the table, including information of all fields; because multiple tables may be processed at the same time, TableInfo is encapsulated into List <tableinfo>Add the parsed TableInfo object; return List <tableinfo>When using it, you can get all the information related to the table through the table name. The above Document, TableInfo and TableColumn classes are all custom classes. In actual applications, you can define these classes according to the structure of the XML file and actual needs, and write the corresponding parsing logic.

[0065] In an alternative embodiment, see Figure 4 As shown, Figure 4 A flow chart of a method for generating a target code provided by the first embodiment of the present invention is shown, wherein the method for generating the target code based on the database table modeling information and the database table field information comprises steps S401 to S402:

[0066] S401: Configure the path of the database table information file, the basic package name of the target code and the database table.

[0067] Specifically, first, a public class named CodeGeneratorMain is defined, which is the entry point of the program. In the class, three private static string variables are declared: BASE_CODE_PACKAGE, which is used to store the base package name of the generated code, and its value is "com.lsa.nes"; PDM_PATH, which is used to store the path of the Power Designer (PDM) file, and its value is "C: / PoverDesigner / nes.pdn; OUTPUT_PATH, which is used to store the output directory of the generated code file, and its value is "D: / CodeOutput".

[0068] S402: Generate the target code based on the path of the database table information file, the basic package name of the target code and the database table execution code modeling main method.

[0069] Specifically, in the CodeGeneratorMain class, a main method is defined as the entry point of the program. This method first deletes all files in the output directory (to ensure that the latest code is generated each time), then parses the .pdm file, and generates the corresponding Java code based on the parsed table information.

[0070] See also Figure 5 As shown, Figure 5 The flowchart of a code generation method based on Power Designer provided by the first embodiment of the present invention is shown, wherein a database table is designed by Power Designer and an XML file with the suffix of ".pdm" is generated. Next, you can enter process one, that is, export the SQL script, create a database, and import the SQL script into the database. Or enter process two, create a custom parsing tool class, use dom4 to parse the data in the mes.pdm file, obtain the table name, field and other information from mes.pdm, and encapsulate the parsed data into List<T> <tablelnfo>Named List <tablelnfo>There are two entity classes encapsulated in the list, namely Tablelnfo and TableColumn. These two entity classes are used to store information about table names and column names. Then define the model template, controller, service, xml and front-end page code templates, write the code generation modeling tool class based on Tablelnfo and TableColumn, and specify the tableName to be generated to execute the code modeling main method, output the model template, controller, service, xml code and front-end page code to the specified location, and end the code generation process.

[0071] Embodiment 2

[0072] Embodiment 2 of the present invention provides an automatic code generation device, see Figure 6 As shown, Figure 6 A schematic diagram of the structure of an automatic code generation device provided by Embodiment 2 of the present invention is shown, wherein the device comprises:

[0073] A database table information file generating module 601 is used to generate a database table information file based on a database table structure, wherein the database table information file includes database table modeling information and database table field information, and the database table field information includes the full name and abbreviated name of the database field;

[0074] A database table information file parsing module 602 is used to parse the database table information file to obtain the database table modeling information and database table field information;

[0075] The target code generation module 603 is used to generate target code based on the database table modeling information and the database table field information, wherein the abbreviated name of the database field appears in the entity class of the target code in the form of its full name.

[0076] In an optional implementation manner, the parsing of the database table information file to obtain the database table modeling information and database table field information includes:

[0077] Write parsing programs;

[0078] The database table information file is parsed by the parsing program.

[0079] In an optional embodiment, the generating target code based on the database table modeling information and the database table field information includes:

[0080] Configure the path of the database table information file, the basic package name of the target code and the database table;

[0081] The target code is generated based on the path of the database table information file, the basic package name of the target code and the database table execution code modeling main method.

[0082] Embodiment 3

[0083] Based on the same application concept, see Figure 7 As shown, Figure 7 FIG. 4 shows a schematic diagram of the structure of a computer device provided by Embodiment 3 of the present invention, wherein: Figure 7 As shown, a computer device 700 provided in Embodiment 3 of the present application includes:

[0084] A processor 701, a memory 702 and a bus 703, wherein the memory 702 stores machine-readable instructions executable by the processor 701. When the computer device 700 is running, the processor 701 communicates with the memory 702 via the bus 703, and the machine-readable instructions are executed by the processor 701 to execute the steps of the automatic code generation method shown in the first embodiment.

[0085] Embodiment 4

[0086] Based on the same application concept, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the automatic code generation method described in any one of the above embodiments are executed.

[0087] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system and device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.

[0088] The computer program product for automatic code generation provided in the embodiments of the present invention includes a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the methods described in the previous method embodiments. The specific implementation can be found in the method embodiments and will not be repeated here.

[0089] The automatic code generation device provided in the embodiment of the present invention can be specific hardware on the device or software or firmware installed on the device. The implementation principle and technical effects of the device provided in the embodiment of the present invention are the same as those of the aforementioned method embodiment. For the sake of brief description, the parts not mentioned in the device embodiment can refer to the corresponding contents in the aforementioned method embodiment. It can be clearly understood by technicians in the relevant field that for the convenience and simplicity of description, the specific working processes of the systems, devices and units described above can all refer to the corresponding processes in the aforementioned method embodiment, and will not be repeated here.

[0090] In the embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.

[0091] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0092] In addition, each functional unit in the embodiment provided by the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0093] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc., which can store program codes.

[0094] It should be noted that similar numbers and letters represent similar items in the following figures. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures. In addition, the terms "first", "second", "third", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.

[0095] Finally, it should be noted that the above-described embodiments are only specific implementations of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The protection scope of the present invention is not limited thereto. Although the present invention is described in detail with reference to the above-described embodiments, those skilled in the art should understand that any person skilled in the art can still modify the technical solutions described in the above-described embodiments within the technical scope disclosed by the present invention, or can easily think of changes, or perform equivalent replacements on some of the technical features thereof; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention. They should all be covered within the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.< / tablelnfo> < / tablelnfo> < / tableinfo> < / tableinfo> < / tableinfo> < / tableinfo> < / tableinfo> < / o:talbe>

Claims

1. An automatic code generation method, characterized in that: The method comprises: Generate a database table information file based on the database table structure, wherein the database table information file includes database table modeling information and database table field information, and the database table field information includes the full name and abbreviated name of the database field; Parsing the database table information file to obtain the database table modeling information and the database table field information; A target code is generated based on the database table modeling information and the database table field information, wherein the abbreviated names of the database fields appear in the form of their full names in the entity class of the target code.

2. The method according to claim 1, characterized in that The step of parsing the database table information file to obtain the database table modeling information and the database table field information includes: Write a parsing program; The database table information file is parsed by the parsing program.

3. The method according to claim 1, characterized in that The generating target code based on the database table modeling information and the database table field information includes: Configure the path of the database table information file, the basic package name of the target code and the database table; The target code is generated based on the path of the database table information file, the basic package name of the target code and the database table execution code modeling main method.

4. An automatic code generation device, characterized in that: The device comprises: A database table information file generation module, used to generate a database table information file based on the database table structure, wherein the database table information file includes database table modeling information and database table field information, and the database table field information includes the full name and abbreviated name of the database field; A database table information file parsing module, used for parsing the database table information file to obtain the database table modeling information and the database table field information; A target code generation module is used to generate target code based on the database table modeling information and the database table field information, wherein the abbreviated name of the database field appears in the entity class of the target code in the form of its full name.

5. The device according to claim 4, characterized in that The step of parsing the database table information file to obtain the database table modeling information and the database table field information includes: Write a parsing program; The database table information file is parsed by the parsing program.

6. The device according to claim 4, characterized in that The generating target code based on the database table modeling information and the database table field information includes: Configure the path of the database table information file, the basic package name of the target code and the database table; The target code is generated based on the path of the database table information file, the basic package name of the target code and the database table execution code modeling main method.

7. A computer device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the computer device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the automatic code generation method as described in any one of claims 1 to 3 are performed.

8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which, when executed by a processor, executes the steps of the automatic code generation method according to any one of claims 1 to 3.