Code generation method and device, equipment, storage medium and product
By acquiring the database mapping model code and parsing the tag information to generate target code, the problem of tedious and repetitive database storage development is solved, achieving efficient automatic code generation and automated management of database operation interfaces, thus improving development efficiency.
Patent Information
- Application Number
- CN202410583263.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-11
- Publication Date
- 2025-11-11
AI Technical Summary
Existing technologies cannot efficiently solve the tedious and repetitive problems that arise during the development process of database storage.
By acquiring the database mapping model code, parsing the tagging information, and generating target code that meets the code generation requirements, manual development steps are reduced, and database tables and CRUD operation interfaces are automatically generated using the code generation plugin.
It improves the efficiency of database development, reduces manpower and time costs, enhances development speed and compatibility, and adapts to various development business needs.
Smart Images

Figure CN120929073A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing technology, and in particular to code generation methods, apparatus, devices, storage media, and products. Background Technology
[0002] Database storage is essential for application development. As companies' application development businesses grow, existing technologies can no longer meet their development and design needs. Database storage also involves tedious and repetitive processes. Therefore, improving the development efficiency of database storage has become an urgent technical problem to be solved.
[0003] Application content
[0004] The main purpose of this application is to provide a code generation method, apparatus, device, storage medium and product, which aims to solve the technical problem of tedious and repetitive development in database storage.
[0005] To achieve the above objectives, this application proposes a code generation method, which includes:
[0006] Obtain the database mapping model code, and determine the code generation requirements and database operation fields based on the database mapping model code;
[0007] Parse the tag information in the database mapping model code;
[0008] Based on the tagging information and the database operation fields, target code that meets the code generation requirements is generated.
[0009] Optionally, the step of generating target code that meets the code generation requirements based on the tagging information and the database operation fields includes:
[0010] Generate a database table based on the tagging information and the database operation fields;
[0011] Target code is generated based on the database tables and the code generation requirements.
[0012] Optionally, the step of generating a database table based on the tag information and the database operation field includes:
[0013] The status information of the database operation field is determined based on the tagging information;
[0014] A database table is generated based on the status information and the database operation fields.
[0015] Optionally, the code generation requirements include at least one of the following: predefined CRUD code, generation of data storage interface, and generation of CRUD API interface.
[0016] Optionally, after the step of generating target code that meets the code generation requirements based on the tagging information and the database operation fields, the method further includes:
[0017] Save the target code to the database operation directory.
[0018] Optionally, before the step of obtaining the database mapping model code and determining the code generation requirements and database operation fields based on the database mapping model code, the method further includes:
[0019] Get the project root directory;
[0020] Create a database operation directory based on the project root directory.
[0021] Furthermore, to achieve the above objectives, this application also proposes a code generation apparatus, the code generation apparatus comprising:
[0022] The acquisition module is used to acquire the database mapping model code and determine the code generation requirements and database operation fields based on the database mapping model code.
[0023] The parsing module is used to parse the tag information in the database mapping model code;
[0024] The code generation module is used to generate target code that meets the code generation requirements based on the tagging information and the database operation fields.
[0025] In addition, to achieve the above objectives, this application also proposes a code generation device, the device comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the code generation method as described above.
[0026] In addition, to achieve the above objectives, this application also proposes a storage medium, which is a computer-readable storage medium, on which a computer program is stored, and which, when executed by a processor, implements the steps of the code generation method described above.
[0027] In addition, to achieve the above objectives, this application also provides a computer program product, which includes a computer program that, when executed by a processor, implements the steps of the code generation method described above. Attached Figure Description
[0028] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0029] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0030] Figure 1 This is a flowchart illustrating the code generation method of this application in Embodiment 1;
[0031] Figure 2 This is a flowchart illustrating Embodiment 2 of the code generation method of this application;
[0032] Figure 3 This is a schematic diagram of the code generation plugin provided in Embodiment 2 of this application;
[0033] Figure 4 This is a schematic diagram of the module structure of the code generation device according to an embodiment of this application;
[0034] Figure 5 This is a schematic diagram of the device structure of the hardware operating environment involved in the code generation method in this application embodiment.
[0035] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0036] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of this application and are not intended to limit this application.
[0037] To better understand the technical solution of this application, a detailed description will be provided below in conjunction with the accompanying drawings and specific implementation methods.
[0038] The main solution of this application embodiment is: to obtain database mapping model code, determine code generation requirements and database operation fields based on the database mapping model code; parse the tag information in the database mapping model code; and generate target code that meets the code generation requirements based on the tag information and the database operation fields.
[0039] This application generates target code that meets code generation requirements based on the database mapping model code. Compared to existing methods where developers must manually develop all database operation code, this application's method reduces the manpower and time costs associated with database development.
[0040] It should be noted that the executing entity in this embodiment can be a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, or mobile phone, or an electronic device or code generation device capable of performing the above functions. The following description uses the code generation device as an example to illustrate this embodiment and the subsequent embodiments.
[0041] Based on this, embodiments of this application provide a code generation method, referring to... Figure 1 , Figure 1 This is a flowchart illustrating the first embodiment of the code generation method of this application.
[0042] In this embodiment, the code generation method includes steps S10 to S30:
[0043] Step S10: Obtain the database mapping model code, and determine the code generation requirements and database operation fields based on the database mapping model code;
[0044] It should be noted that the code generation device integrates a code generation plugin, which uses Gradle plugin development technology and runs on a Gradle-based IDE. In this embodiment, all target code is automatically generated by the code generation plugin.
[0045] It should be noted that the database mapping model code can be user-developed code. The code generation device identifies the user's needs based on the developed code and its corresponding tags within the database mapping model code, and then generates the corresponding database operation code. For example, the database mapping model code could be:
[0046]
[0047]
[0048] The database operation fields determined according to the aforementioned database mapping model code can be `private inttype` and `private String command`. The code generation requirement can be to create a new database table named `Command`, containing the fields `type` and `command`. These database operation fields can be fields from the database mapping model code.
[0049] Step S20: Parse the tag information in the database mapping model code;
[0050] It should be noted that the marking information can be annotation information in the database mapping model code. Taking the database mapping model code mentioned above as an example, the marking information in the database mapping model code can be @Table, @TableField on private int type, and @TableField on private String command. Specifically, @TableField is used to mark that a database table needs to be generated based on the field type and command. @Table is used to mark that a database table named Command should be generated.
[0051] Step S30: Generate target code that meets the code generation requirements based on the tagging information and the database operation fields.
[0052] It should be noted that generating target code that meets the code generation requirements based on the tagging information and the database operation fields can be a general CRUD (Create, Read, Update, Delete) code generated based on the tagging information and the database operation fields. Specifically, it can be based on the tagging information to determine which fields in the database operation fields are used as conditions for CRUD operations, and then generate CRUD code for the database table based on these conditions and the database table. For example, the generated target code could be:
[0053]
[0054]
[0055] This embodiment uses an IDE plugin as its core, supplemented by IDE compile-time annotation technology, and employs database object model mapping technology to map the code object model to the database. Simultaneously, it injects tedious and repetitive database storage processes into the project code through automatic code generation during IDE compilation. This automatically generates basic interfaces for relevant CRUD operations and pre-defined interfaces by developers, and provides an adaptation model, enabling all projects within the company that use the database to utilize it. This significantly improves development speed and efficiency, and saves the company's related manpower and time costs.
[0056] This embodiment provides a code generation method, including: obtaining database mapping model code; determining code generation requirements and database operation fields based on the database mapping model code; parsing the marker information in the database mapping model code; and generating target code that meets the code generation requirements based on the marker information and the database operation fields. Since this embodiment generates target code that meets the code generation requirements based on the database mapping model code, compared to existing methods where database operation code requires manual development by developers, this method can reduce the manpower and time costs associated with database development.
[0057] Based on the first embodiment of this application, in the second embodiment of this application, the content that is the same as or similar to that in Embodiment 1 above can be referred to the above description, and will not be repeated hereafter. Based on this, please refer to... Figure 2 Step S30 further includes steps S301 to S302:
[0058] Step S301: Generate a database table based on the tagging information and the database operation fields;
[0059] It should be noted that generating a database table based on the tag information and the database operation fields can be done by generating the database table based on the database operation fields and the tags corresponding to the database operation fields. For example, if there are database operation fields A, B, and C, and the database operation fields A and B are tagged as fields in the generated database table, then a database table containing fields A and B will be generated.
[0060] Furthermore, in order to generate a database table that meets user requirements, step S301 may include: determining the status information of the database operation field based on the tag information;
[0061] A database table is generated based on the status information and the database operation fields.
[0062] It should be noted that the status information may include whether the field is used to generate a database table and whether the field serves as a primary key in the database table. Generating the database table based on the status information and the database operation fields may be done by generating the database table according to the database operation fields and the status information corresponding to each field within those fields.
[0063] Step S302: Generate target code based on the database table and the code generation requirements.
[0064] It should be noted that the code generation requirements include at least one of the following: predefined CRUD (Create, Read, Update, Delete) code, generation of data storage interfaces, generation of CRUD API interfaces, etc. The generation of target code based on the database table and the code generation requirements can be achieved by a code generation plugin parsing the database mapping model code to determine the user's code generation requirements. For example, if the user's code generation requirement is to generate target code for querying based on field A, then the code generation plugin will generate target code for querying based on field A based on the database table containing field A and other field information in that database table.
[0065] Furthermore, to reduce the workload of developers and improve project development efficiency, after the step of generating target code that meets the code generation requirements based on the tagging information and the database operation fields, the method further includes:
[0066] Save the target code to the database operation directory.
[0067] Before the steps of obtaining the database mapping model code and determining the code generation requirements and database operation fields based on the database mapping model code, the method further includes:
[0068] Get the project root directory;
[0069] Create a database operation directory based on the project root directory.
[0070] In practice, after the code generation plugin starts, it locates the project root directory and generates a database operation directory based on it. After generating the target code for database operations, the generated target code is automatically saved in the corresponding database operation directory within the project code. Developers can then directly call the generated target code to operate the database.
[0071] In specific implementation, it can be referred to Figure 3 , Figure 3 This is a schematic diagram of the code generation plugin provided in Embodiment 2 of this application. Figure 3 The code generation plugin can automatically generate annotated API interfaces, predefined APIs, and data storage APIs. The core of the plugin includes annotation parsing and code generation, database mapping, and data storage. This implementation's code generation plugin was designed with all possible development business needs in mind, adapting to different product development requirements and possessing excellent compatibility. Because almost all data-related functional code is automatically generated by the plugin, it greatly improves the development speed and efficiency of developers. Its database functionality and the company's business compatibility are unmatched by any existing technology on the market.
[0072] The code generation plugin provided in this embodiment is a Gradle plugin, supporting all Gradle-based IDEs. Installing this plugin in the IDE and referencing it during project development enables automated database storage management. If a project requires special storage interfaces, developers can predefine them using database mapping model code and annotations. This plugin covers database mapping technology and existing database functionalities, such as stored procedures and triggers, and automatically generates corresponding interfaces. Therefore, developers do not need to invest excessive effort in data storage during development; they only need to annotate the database mapping model code with annotations.
[0073] This application also provides a code generation apparatus, please refer to... Figure 4 The code generation device includes:
[0074] The acquisition module 10 is used to acquire the database mapping model code and determine the code generation requirements and database operation fields based on the database mapping model code.
[0075] Parsing module 20 is used to parse the tag information in the database mapping model code;
[0076] The code generation module 30 is used to generate target code that meets the code generation requirements based on the tagging information and the database operation fields.
[0077] This embodiment obtains database mapping model code, determines code generation requirements and database operation fields based on the database mapping model code, parses the tag information in the database mapping model code, and generates target code that meets the code generation requirements based on the tag information and the database operation fields. Since this embodiment generates target code that meets the code generation requirements based on the database mapping model code, compared to existing methods where all database operation code needs to be manually developed by developers, this method can reduce the manpower and time costs associated with database development.
[0078] The code generation apparatus provided in this application, employing the code generation method described in the above embodiments, can solve the technical problem of tedious and repetitive development in database storage. Compared with the prior art, the beneficial effects of the code generation apparatus provided in this application are the same as those of the code generation method described in the above embodiments, and other technical features in the code generation apparatus are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.
[0079] This application provides a code generation device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the code generation method in Embodiment 1 above.
[0080] The following is for reference. Figure 5 The diagram illustrates a structural schematic of a code generation device suitable for implementing embodiments of this application. The code generation device in the embodiments of this application may include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Description), PMPs (Portable Media Players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 5 The code generation device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.
[0081] like Figure 5 As shown, the code generation device may include a processing unit 1001 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1002 or a program loaded from a storage device 1003 into a random access memory (RAM) 1004. The RAM 1004 also stores various programs and data required for the operation of the code generation device. The processing unit 1001, ROM 1002, and RAM 1004 are interconnected via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to I / O interface 1006: input devices 1007 including, for example, touchscreens, touchpads, keyboards, mice, image sensors, microphones, accelerometers, gyroscopes, etc.; output devices 1008 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 1003 including, for example, magnetic tapes, hard disks, etc.; and communication devices 1009. Communication device 1009 allows the code generation device to communicate wirelessly or wiredly with other devices to exchange data. Although code generation devices with various systems are shown in the figures, it should be understood that it is not required to implement or possess all the systems shown. More or fewer systems can be implemented alternatively.
[0082] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from storage device 1003, or installed from ROM 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of the embodiments disclosed in this application.
[0083] The code generation device provided in this application, employing the code generation method described in the above embodiments, can solve the technical problem of code generation. Compared with the prior art, the beneficial effects of the code generation device provided in this application are the same as those of the code generation method described in the above embodiments, and other technical features of the code generation device are the same as those disclosed in the method of the previous embodiment, and will not be repeated here.
[0084] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples.
[0085] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
[0086] This application provides a computer-readable storage medium having computer-readable program instructions (i.e., a computer program) stored thereon, the computer-readable program instructions being used to execute the code generation method described in the above embodiments.
[0087] The computer-readable storage medium provided in this application may be, for example, a USB flash drive, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to: electrical connections having one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, system, or device. The program code contained on the computer-readable storage medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination thereof.
[0088] The aforementioned computer-readable storage medium may be included in the code generation device; or it may exist independently and not assembled into the code generation device.
[0089] The aforementioned computer-readable storage medium carries one or more programs, which, when executed by the code generation device, cause the code generation device to generate code.
[0090] Computer program code for performing the operations of this application can be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, and C++, and conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a Local Area Network (LAN) or a Wide Area Network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0091] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0092] The modules described in the embodiments of this application can be implemented in software or hardware. The names of the modules do not necessarily limit the functionality of the unit itself.
[0093] The readable storage medium provided in this application is a computer-readable storage medium that stores computer-readable program instructions (i.e., a computer program) for executing the above-described code generation method, and is capable of solving the technical problem of code generation. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as the beneficial effects of the code generation method provided in the above embodiments, and will not be repeated here.
[0094] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the code generation method described above.
[0095] The computer program product provided in this application can solve the technical problem of code generation. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as the beneficial effects of the code generation method provided in the above embodiments, and will not be repeated here.
[0096] The above description is only a part of the embodiments of this application and does not limit the patent scope of this application. All equivalent structural transformations made under the technical concept of this application and using the contents of the specification and drawings of this application, or direct / indirect applications in other related technical fields, are included in the patent protection scope of this application.
Claims
1. A code generation method, characterized in that, The code generation method includes the following steps: Obtain the database mapping model code, and determine the code generation requirements and database operation fields based on the database mapping model code; Parse the tag information in the database mapping model code; Based on the tagging information and the database operation fields, target code that meets the code generation requirements is generated.
2. The code generation method as described in claim 1, characterized in that, The step of generating target code that meets the code generation requirements based on the tagging information and the database operation fields includes: Generate a database table based on the tagging information and the database operation fields; Target code is generated based on the database tables and the code generation requirements.
3. The code generation method as described in claim 2, characterized in that, The step of generating a database table based on the tagging information and the database operation fields includes: The status information of the database operation field is determined based on the tagging information; A database table is generated based on the status information and the database operation fields.
4. The code generation method as described in claim 1, characterized in that, The code generation requirements include at least one of the following: predefined CRUD code, generation of data storage interface, and generation of CRUD API interface.
5. The code generation method according to any one of claims 1-4, characterized in that, After the step of generating target code that meets the code generation requirements based on the tagging information and the database operation fields, the method further includes: Save the target code to the database operation directory.
6. The code generation method as described in claim 5, characterized in that, Before the steps of obtaining the database mapping model code and determining the code generation requirements and database operation fields based on the database mapping model code, the method further includes: Get the project root directory; Create a database operation directory based on the project root directory.
7. A code generation device, characterized in that, The code generation device includes: The acquisition module is used to acquire the database mapping model code and determine the code generation requirements and database operation fields based on the database mapping model code. The parsing module is used to parse the tag information in the database mapping model code; The code generation module is used to generate target code that meets the code generation requirements based on the tagging information and the database operation fields.
8. A code generation device, characterized in that, The device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the code generation method as described in any one of claims 1 to 6.
9. A storage medium, characterized in that, The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, it implements the steps of the code generation method as described in any one of claims 1 to 6.
10. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the steps of the code generation method as described in any one of claims 1 to 6.