Backend code generation method and apparatus

By obtaining variable properties and method types from the front-end page and combining them with template engine technology, back-end code is automatically generated based on DDL statements. This solves the problems of insufficient flexibility and ease of use of existing tools and realizes a flexible code generation process.

CN115454415BActive Publication Date: 2026-04-14BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIJING WODONG TIANJUN INFORMATION TECH CO LTD
Filing Date
2022-09-15
Publication Date
2026-04-14

AI Technical Summary

Technical Problem

Existing backend source code generation tools are not very flexible or easy to use, requiring the pre-creation of database table structures and the generation of fixed project formats.

Method used

By obtaining variable properties and method types from the code template from the visual front-end page, using the template engine to replace them with placeholders, and determining the database table structure information based on the user-input Data Definition Language (DDL) statements, code based on the back-end language is generated.

Benefits of technology

It achieves improved flexibility and ease of use, can dynamically adjust the format of generated target code, eliminates the need to pre-create database table structures, reduces operational complexity, and improves development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115454415B_ABST
    Figure CN115454415B_ABST
Patent Text Reader

Abstract

The application discloses a backend code generation method and device, and relates to the technical field of computers. A specific implementation manner of the method comprises the following steps: obtaining variable attributes and a used method type in a code template from a preset front-end page; replacing attribute values of the variable attributes in a code framework based on a backend language into a placeholder form, and writing a method code block indicated by the method type and encapsulated in advance based on a template engine into the code framework to form a code template based on the template engine; determining structure information and field information of a database table associated with a to-be-processed item according to a DDL statement for the to-be-processed item, writing the determined structure information and field information into a method code block in a code template selected by a user, and replacing attribute values in the placeholder form in the code template with attribute values of the variable attributes of the to-be-processed item to generate a code of the to-be-processed item based on the backend language. The implementation manner has high flexibility and ease of use.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a backend code generation method and apparatus. Background Technology

[0002] Currently, commonly used backend source code generation tools (such as Mybatis Generator) work by reading existing tables in the database, executing DDL (Data Definition Language) scripts to obtain the table structure definitions, converting these definitions into corresponding entity attribute data, and finally generating the corresponding source code project. This process requires modifying configuration files to control the generated target code, pre-creating the database table structure, and using a fixed project format for the generated source code, resulting in poor flexibility and ease of use. Summary of the Invention

[0003] In view of this, embodiments of the present invention provide a backend code generation method and apparatus, which have high flexibility and ease of use.

[0004] To achieve the above objectives, according to one aspect of the present invention, a backend code generation method is provided.

[0005] The backend code generation method of this invention includes: obtaining variable attributes and method types from a preset frontend page in a code template; replacing the attribute values ​​of the variable attributes in a preset code framework based on a backend language with placeholders; writing method code blocks, pre-encapsulated by a template engine and indicating the method type, into the code framework to form the code template based on the template engine; determining the structure information and field information of a target database table according to a Data Definition Language (DDL) statement for the project to be processed; wherein the DDL statement is input by the user, and the target database table is the database table associated with the project to be processed; writing the determined structure information and field information into the method code block in the code template selected by the user; and replacing the attribute values ​​in placeholder form in the code template with the attribute values ​​of the variable attributes of the project to be processed input by the user, to generate code for the project to be processed based on the backend language.

[0006] Optionally, the method further includes: after obtaining the method type, replacing the method name corresponding to the method type in the code framework with a placeholder; after obtaining the DDL statement, generating the method name corresponding to the method type in the project to be processed based on the DDL statement, and replacing the placeholder method name in the code template selected by the user with the generated method name.

[0007] Optionally, the method further includes: obtaining condition value data from the method code block indicated by the method type from the front-end page; and writing the method code block indicated by the method type, which is pre-encapsulated based on the template engine, into the code frame, including: writing the obtained condition value data into the method code block, and writing the method code block containing the condition value data into the code frame.

[0008] Optionally, the method further includes: before forming the code template based on the template engine, modifying the file extension of the code framework file containing the method code block to the file extension corresponding to the template engine; and before generating the code of the project to be processed, modifying the file extension of the code template containing the structure information, the field information, and the attribute values ​​of the variable attributes of the project to be processed to the file extension corresponding to the backend language.

[0009] Optionally, the method further includes: after forming the code template based on the template engine, generating a configuration file containing path information of the code template for locating the code template when generating the code of the project to be processed.

[0010] Optionally, after writing the determined structure information and field information into the method code block of the code template selected by the user, the naming style in the code template is modified to the naming style of the backend language, and the data type in the code template is modified to the data type of the backend language; the code configuration file format is further obtained from the frontend page so as to generate a configuration file of the same format corresponding to the code when generating the code of the project to be processed; and the attribute values ​​of the DDL statement and the variable attributes of the project to be processed are input by the user on the frontend page; the variable attributes include at least one of the following: organization identifier, project name, project identifier, package name under the project; the method type includes at least one of the following: add, delete, modify, query, upload, download.

[0011] To achieve the above objectives, according to another aspect of the present invention, a backend code generation apparatus is provided.

[0012] The backend code generation apparatus of this invention may include: a template data acquisition unit, configured to acquire variable attributes and method types used in a code template from a preset frontend page; a template construction unit, configured to replace the attribute values ​​of the variable attributes in a preset code framework based on a backend language with placeholders, and write method code blocks pre-encapsulated by a template engine and indicating the method types into the code framework to form the code template based on the template engine; and a code generation unit, configured to: determine the structure information and field information of a target database table according to a Data Definition Language (DDL) statement for the project to be processed; wherein the DDL statement is input by a user, and the target database table is a database table associated with the project to be processed; write the determined structure information and field information into a method code block in a code template selected by the user, and replace the attribute values ​​in placeholder form in the code template with the attribute values ​​of the variable attributes of the project to be processed input by the user, so as to generate code for the project to be processed based on the backend language.

[0013] Optionally, the template data acquisition unit is further configured to: acquire condition value data from the method code block indicated by the method type from the front-end page; the template construction unit is further configured to: write the acquired condition value data into the method code block, and write the method code block containing the condition value data into the code framework; after acquiring the method type, replace the method name corresponding to the method type in the code framework with a placeholder; the code generation unit is further configured to: after obtaining the DDL statement, generate the method name corresponding to the method type in the project to be processed according to the DDL statement, and replace the placeholder method name in the code template selected by the user with the generated method name.

[0014] To achieve the above objectives, according to another aspect of the present invention, an electronic device is provided.

[0015] An electronic device according to the present invention includes: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the back-end code generation method provided by the present invention.

[0016] To achieve the above objectives, according to another aspect of the present invention, a computer-readable storage medium is provided.

[0017] The present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the program, when executed by a processor, implements the back-end code generation method provided by the present invention.

[0018] According to the technical solution of the present invention, the embodiments described above have the following advantages or beneficial effects:

[0019] First, the server retrieves the variable attributes and method types from the code template on the visual front-end page. Next, the server modifies the code framework based on backend languages ​​such as Java, replacing the variable attribute values ​​with placeholders. It then writes pre-encapsulated method code blocks, based on template engines like Freemarker and containing the method type indicators, into the code framework. After modifying the file extension, a template engine-based code template is generated. Subsequently, backend code can be automatically generated for specific projects based on this template. Specifically, the user first inputs the DDL statement for the project to be processed, the attribute values ​​of the variable attributes, and the selected code template on the front-end page. After obtaining this data, the server determines the structure and field information of the database table associated with the project based on the DDL statement. This structure and field information is then written into the method code blocks in the user-selected code template. The server replaces the placeholder attribute values ​​in the code template with the variable attribute values ​​of the project to be processed. Finally, after modifying the file extension, the code for the project based on the backend language is generated. In the above steps, relevant data is collected through a visual page rather than a configuration file. DDL statements are directly parsed to obtain database table information without the need to pre-create the database table structure. Furthermore, the generated target code is not in a fixed format and can be dynamically adjusted according to the template format, thus providing greater flexibility and ease of use.

[0020] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description

[0021] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein:

[0022] Figure 1 This is a schematic diagram of the main steps of the backend code generation method in an embodiment of the present invention;

[0023] Figure 2 This is a schematic diagram illustrating the specific execution steps of the backend code generation method in an embodiment of the present invention;

[0024] Figure 3 This is a schematic diagram of the components of the backend code generation device in an embodiment of the present invention;

[0025] Figure 4 This is an exemplary system architecture diagram that can be applied thereto according to embodiments of the present invention;

[0026] Figure 5This is a schematic diagram of the electronic device structure used to implement the backend code generation method in the embodiments of the present invention. Detailed Implementation

[0027] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.

[0028] It should be noted that, unless otherwise specified, the embodiments of the present invention and the technical features thereof can be combined with each other.

[0029] Figure 1 This is a schematic diagram illustrating the main steps of the backend code generation method according to an embodiment of the present invention.

[0030] like Figure 1 As shown, the backend code generation method of this embodiment is executed by the server, and the specific steps are as follows:

[0031] Step S101: Obtain the variable properties and method types used in the code template from the preset front-end page.

[0032] This step involves collecting relevant data to create a code template, which then automatically generates the project's backend code. In the computer and internet fields, the frontend refers to the website's front-end, the page displayed to the user on a PC or mobile browser or application. The backend refers to the actual working logic of the website or application, determining which data to retrieve from the database and send back to the frontend to support it. Backend developers also participate in the system architecture, deciding how to organize the system logic to ensure proper maintenance and operation. In this step, developers first input the variable attributes and method types (functions) they want to include in the generated code template on a visual frontend page. The server then retrieves this data to generate the code template. Variable attributes refer to mutable properties or metadata within the code template. This means that when generating the project's backend code using the code template, these variable attributes will dynamically adjust depending on the project; the specific values ​​of these variable attributes need to be determined based on the specific circumstances of each project. For example, the above variable attributes may include at least one of the following: organization identifier (GroupID), project name (ItemName), project identifier (ItemID), and package name under the project. The organization identifier can be used to construct the path information of the code template; the above method types may include at least one of the following: add, delete, modify, query, upload, and download.

[0033] As a preferred option, developers can also input conditional values ​​related to the above method types (such as thresholds required when executing if / else conditional statements) on the aforementioned front-end page for later use. Developers can also specify the code configuration file format on the aforementioned front-end page, such as JSON (JavaScript Object Notation) or XML (Extensible Markup Language) format; the final generated configuration file for the project's back-end code will then be in the above format. In practical applications, after collecting the above data from the page, further data processing can be performed, such as converting Chinese characters to English characters.

[0034] Step S102: Replace the attribute values ​​of variable attributes in the preset code framework based on the backend language with placeholders, and write the method code block with method type indication pre-encapsulated based on the template engine into the code framework to form a code template based on the template engine.

[0035] In this step, the server first obtains a pre-built code framework based on a backend language (such as Java, C++, etc., with Java as an example below). This code framework contains the fixed code structure of the backend language, such as the fixed code of classes, as well as the attribute values ​​of various project properties (including the aforementioned variable properties) and the method names corresponding to various method types. It should be noted that the code framework does not contain the specific implementation code of each method. Next, the server firstly replaces the attribute values ​​of the variable properties in the code framework (i.e., the variable properties entered by the developer on the front-end page) with placeholders, such as replacing the project name with ${ItemName} and the package name with ${comonModule}. Secondly, it writes method code blocks, pre-encapsulated based on a template engine (such as Freemarker) and with method type indicators, into the code framework. These method code blocks can be written and encapsulated using various directives of the template engine, such as Freemarker's include, if / elseif / else, etc.

[0036] Preferably, in the second aspect above, the server can pre-obtain the conditional value data from the method code block indicated by the method type on the front-end page, i.e., the conditional value data entered by the developer on the front-end page. Subsequently, the obtained conditional value data is written into the method code block indicated by the method type, and the method code block containing the conditional value data is written into the code frame, thereby ensuring the logical integrity of the method code. It can be understood that the steps in the first and second aspects above can be executed in any order, or simultaneously.

[0037] In an optional technical solution, the server can also replace the method names corresponding to method types in the code framework with placeholders to facilitate the replacement of specific method names during subsequent code generation. Afterward, the server changes the file extension of the code framework file containing the method code blocks to the extension corresponding to the template engine, such as .ftl for Freemarker, thus obtaining a template-based code template and its corresponding configuration file. This configuration file contains the path information of the code template, which is used to locate the code template during subsequent code generation. In practical applications, after the server generates the code template and its configuration file, it can upload the configuration file to the backend. The backend obtains the code template by parsing the path information in the configuration file and stores the code template in the database for later use.

[0038] Step S103: Determine the structure and field information of the target database table based on the Data Definition Language (DDL) statement for the project to be processed; the DDL statement is input by the user, and the target database table is the database table associated with the project to be processed; write the determined structure and field information into the method code block in the code template selected by the user, and replace the placeholder attribute values ​​in the code template with the attribute values ​​of the variable attributes of the project to be processed input by the user, so as to generate code for the project to be processed based on the backend language.

[0039] In this step, the user first enters the DDL (Data Definition Language) statements and variable attribute values ​​for the project to be processed on the visual front-end page, and selects the required code template according to the template description. These DDL statements describe the entities to be stored in the database and mainly include keywords such as create, alter, drop, and truncate. The server, on one hand, determines the structure and field information of the database table associated with the project based on the DDL statements, and writes this information into the method code block of the user-selected code template. The structure information refers to the database table's name, description, and other metadata, while the field information refers to the field names, descriptions, and data types. This writing can involve replacing the corresponding placeholders in the method code block with the structure and field information. On the other hand, the server replaces the placeholder attribute values ​​in the code template with the user-input variable attribute values ​​of the project. For example, the item name placeholder ${ItemName} in the code template is replaced with the project name "mydemo" entered on the front-end page. It can be understood that these two steps can be executed in any order or simultaneously.

[0040] In this embodiment of the invention, after the server writes the determined structure information and field information into the method code block in the code template, it can modify the naming style in the code template to the naming style of the backend language, for example, changing the database underscore naming convention to Java's camelCase naming convention; it can also modify the data type in the code template to the data type of the backend language, for example, converting the database type to a Java type.

[0041] In practical applications, after obtaining the above DDL statements, the server can generate method names corresponding to the method types in the project to be processed, i.e., the method names used by the project. The generated method names replace the placeholder method names in the code template. For example, the placeholder method name ${create} in the code template is replaced with the specific method name "mycreate" entered on the front-end page. Finally, the server modifies the file extension of the code template to the extension corresponding to the backend language, such as .java, thus forming the backend source code of the project based on the backend language. Furthermore, the server can also generate a configuration file corresponding to the backend source code of the project to be processed to store the database, cache, and other configurations of the project. The format of this configuration file is the same as the code configuration file format obtained in step S101. It can be seen that in the above code generation process, the user does not need to pre-create the actual database tables associated with the project; only the DDL statements are needed to extract the required information and generate the backend source code, thus providing greater convenience and practicality.

[0042] Figure 2 This is a schematic diagram illustrating the specific execution steps of the backend code generation method in an embodiment of the present invention. See [link / reference]. Figure 2In step S201, the server obtains the variable attributes, method types, code configuration file format, and conditional value data of the code template input by the developer from the visual front-end page. In step S202, the server replaces the attribute values ​​of the variable attributes in the preset code framework with placeholders. In step S203, the server determines the method code block indicated by the method type. In step S204, the server writes the obtained conditional value data into the method code block. In step S205, the server writes the method code block containing the conditional value data into the code framework. In step S206, the server replaces the method name corresponding to the method type in the code framework with a placeholder. The order of the variable attribute replacement in step S202, the method code writing in steps S203 to S205, and the method name replacement in step S206 can be arbitrarily adjusted, or they can be executed simultaneously. In step S207, the server modifies the file extension of the code framework to the file extension corresponding to the template engine; in step S208, a code template and its configuration file are generated; in step S209, the server obtains the DDL statement of the project to be processed and the attribute values ​​of the variable attributes, as well as the code template selected by the user, from the front-end page; in step S210, the server uses the DDL statement to determine the structure and field information of the database table associated with the project to be processed; in step S211, the server writes the determined structure and field information of the database table into the method code block in the code template selected by the user; in step S212, the server converts the naming style and data type in the code template; in step S213, the server replaces the corresponding attribute values ​​in placeholder form in the code template with the attribute values ​​of the variable attributes of the project to be processed; in step S214, the server generates the method name corresponding to the method type in the project to be processed according to the DDL statement; in step S215, the server replaces the method name in placeholder form in the code template with the generated method name. The execution order of steps S210 to S211 (database information import), step S213 (variable attribute replacement), and steps S214 to S215 (method name replacement) can be arbitrarily adjusted, or they can be executed simultaneously. In step S216, the server modifies the file extension of the code template to the file extension corresponding to the backend language; in step S217, the backend code of the project to be processed and the configuration file in the code configuration file format are generated.

[0043] In the technical solution of this invention, a visual page is combined with template engine technology to visualize the backend source code generation process, reducing operational complexity, improving system usability, and unifying the standard for automatic code generation through a high-quality template engine. Furthermore, visualization of the generation process for commonly used methods is added. In practical applications, source code files containing corresponding methods can be selectively generated according to requirements. The template-based code generation method also reduces redundant code in the source code project, thereby improving development efficiency and code quality.

[0044] It should be noted that, for the sake of ease of description, the foregoing method embodiments are described as a series of actions. However, those skilled in the art should understand that the present invention is not limited to the described order of actions, and some steps may actually be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions and modules involved are not necessarily essential for implementing the present invention.

[0045] To facilitate better implementation of the above-described solutions of the embodiments of the present invention, related apparatus for implementing the above-described solutions is also provided below.

[0046] Please see Figure 3 As shown, the backend code generation device 300 provided in this embodiment of the invention may include: a template data acquisition unit 301, a template construction unit 302, and a code generation unit 303.

[0047] The template data acquisition unit 301 can be used to acquire variable attributes and method types from a preset front-end page in a code template; the template construction unit 302 can be used to replace the attribute values ​​of the variable attributes in a preset code framework based on a backend language with placeholders, and write the method code blocks, which are pre-encapsulated based on the template engine and indicate the method types, into the code framework to form the code template based on the template engine; the code generation unit 303 can be used to: determine the structure information and field information of the target database table according to the Data Definition Language (DDL) statement for the project to be processed; the DDL statement is input by the user, and the target database table is the database table associated with the project to be processed; write the determined structure information and field information into the method code block in the code template selected by the user, and replace the attribute values ​​in the placeholder form in the code template with the attribute values ​​of the variable attributes of the project to be processed input by the user, so as to generate the code of the project to be processed based on the backend language.

[0048] In this embodiment of the invention, the template data acquisition unit 301 can be further configured to: acquire condition value data in the method code block indicated by the method type from the front-end page; the template construction unit 302 can be further configured to: write the acquired condition value data into the method code block, and write the method code block containing the condition value data into the code framework; after acquiring the method type, replace the method name corresponding to the method type in the code framework with a placeholder; the code generation unit 303 can be further configured to: after obtaining the DDL statement, generate the method name corresponding to the method type in the project to be processed according to the DDL statement, and replace the placeholder method name in the code template selected by the user with the generated method name.

[0049] As a preferred embodiment, the template construction unit 302 can be further configured to: modify the file extension of the code framework file containing the method code block to the file extension corresponding to the template engine before forming the code template based on the template engine; the code generation unit 303 can be further configured to: modify the file extension of the code template containing the structure information, the field information and the attribute values ​​of the variable attributes of the project to be processed to the file extension corresponding to the backend language before generating the code of the project to be processed.

[0050] Preferably, the template building unit 302 can be further used to: after the code template based on the template engine is formed, generate a configuration file containing the path information of the code template, for locating the code template when generating the code of the project to be processed.

[0051] Furthermore, in this embodiment of the invention, the code generation unit 303 may be further configured to: after writing the determined structure information and field information into the method code block of the code template selected by the user, modify the naming style in the code template to the naming style of the backend language, and modify the data type in the code template to the data type of the backend language; the template data acquisition unit 301 may be further configured to: further acquire the code configuration file format from the front-end page, so as to generate a configuration file of the format corresponding to the code when generating the code of the project to be processed; and the attribute values ​​of the DDL statement and the variable attributes of the project to be processed are input by the user on the front-end page; the variable attributes include at least one of the following: organization identifier, project name, project identifier, package name under the project; the method type includes at least one of the following: add, delete, modify, query, upload, download.

[0052] According to the technical solution of this invention, a visual page is combined with template engine technology to visualize the backend source code generation process, reducing operational complexity, improving system usability, and unifying the standard for automatic code generation through a high-quality template engine. Furthermore, it adds visualization of the generation process for commonly used methods. In practical applications, source code files containing corresponding methods can be selectively generated according to needs. The template-based code generation method also reduces redundant code in the source code project, thereby improving development efficiency and code quality.

[0053] Figure 4 An exemplary system architecture 400 is shown that can be applied to the backend code generation method or backend code generation apparatus of the present invention.

[0054] like Figure 4 As shown, system architecture 400 may include terminal devices 401, 402, and 403, network 404, and server 405 (this architecture is merely an example; the components included in a specific architecture may be adjusted according to the specific application). Network 404 serves as the medium for providing a communication link between terminal devices 401, 402, and 403 and server 405. Network 404 may include various connection types, such as wired or wireless communication links or fiber optic cables.

[0055] Users can use terminal devices 401, 402, and 403 to interact with server 405 via network 404 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 401, 402, and 403, such as code generation applications (for example only).

[0056] Terminal devices 401, 402, and 403 can be various electronic devices with displays that support web browsing, including but not limited to smartphones, tablets, laptops, and desktop computers.

[0057] Server 405 can be a server that provides various services, such as a backend server that supports code generation applications operated by users using terminal devices 401, 402, and 403 (for example only). The backend server can process received code generation requests for pending projects and feed back the processing results (such as automatically generated backend code for pending projects - for example only) to terminal devices 401, 402, and 403.

[0058] It should be noted that the backend code generation method provided in the embodiments of the present invention is generally executed by server 405, and correspondingly, the backend code generation device is generally set in server 405.

[0059] It should be understood that Figure 4The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.

[0060] The present invention also provides an electronic device. The electronic device according to an embodiment of the present invention includes: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the back-end code generation method provided by the present invention.

[0061] The following is for reference. Figure 5 It shows a schematic diagram of the structure of a computer system 500 suitable for implementing an electronic device according to embodiments of the present invention. Figure 5 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of use of the embodiments of the present invention.

[0062] like Figure 5 As shown, the computer system 500 includes a central processing unit (CPU) 501, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 502 or programs loaded from storage section 508 into random access memory (RAM) 503. The RAM 503 also stores various programs and data required for the operation of the computer system 500. The CPU 501, ROM 502, and RAM 503 are interconnected via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.

[0063] The following components are connected to I / O interface 505: an input section 506 including a keyboard, mouse, etc.; an output section 507 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 508 including a hard disk, etc.; and a communication section 509 including a network interface card such as a LAN card, modem, etc. The communication section 509 performs communication processing via a network such as the Internet. A drive 510 is also connected to I / O interface 505 as needed. A removable medium 511, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 510 as needed so that computer programs read from it can be installed into storage section 508 as needed.

[0064] In particular, according to the embodiments disclosed in this invention, the processes described in the above main step diagrams can be implemented as computer software programs. For example, embodiments of this invention 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 main step diagrams. In the above embodiments, the computer program can be downloaded and installed from a network via communication section 509, and / or installed from removable medium 511. When the computer program is executed by central processing unit 501, it performs the functions defined in the system of this invention.

[0065] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, 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 device, magnetic storage device, or any suitable combination thereof. In this invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium may be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.

[0066] 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 the present invention. 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 a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may 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.

[0067] The units described in the embodiments of the present invention can be implemented in software or hardware. The described units can also be housed in a processor; for example, a processor can be described as including a template data acquisition unit, a template construction unit, and a code generation unit. The names of these units do not necessarily limit the specific unit; for example, the template data acquisition unit can also be described as "a unit that provides variable attributes and method types to the template construction unit."

[0068] In another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs, and when the device executes the one or more programs, the steps performed by the device include: obtaining variable attributes and method types from a preset front-end page in a code template; replacing the attribute values ​​of the variable attributes in a preset code framework based on a back-end language with placeholder forms; writing method code blocks, pre-encapsulated based on a template engine and indicating the method types, into the code framework to form the code template based on the template engine; determining the structure information and field information of a target database table according to a Data Definition Language (DDL) statement for the project to be processed; wherein the DDL statement is input by the user, and the target database table is the database table associated with the project to be processed; writing the determined structure information and field information into a method code block in a code template selected by the user; and replacing the attribute values ​​in placeholder forms in the code template with the attribute values ​​of the variable attributes of the project to be processed input by the user, to generate code for the project to be processed based on the back-end language.

[0069] In the technical solution of this invention embodiment, firstly, the server obtains the variable attributes and method types used in the code template from the visual front-end page; then, the server modifies the code framework based on backend languages ​​such as Java, replacing the attribute values ​​of the variable attributes with placeholders, and writes the pre-encapsulated method code blocks based on template engines such as Freemarker, which indicate the above method types, into the code framework. After modifying the file extension, a code template based on the template engine is formed. Subsequently, backend code can be automatically generated for specific projects to be processed based on the code template. Specifically, the user first inputs the DDL statement of the project to be processed, the attribute values ​​of the variable attributes, and the selected code template on the front-end page. After obtaining this data, the server determines the structure and field information of the database table associated with the project to be processed based on the DDL statement, writes the determined structure and field information into the method code blocks in the code template selected by the user, and replaces the placeholder attribute values ​​in the code template with the attribute values ​​of the variable attributes of the project to be processed. Finally, after modifying the file extension, the code for the project to be processed based on the backend language is formed. In the above steps, relevant data is collected through a visual page rather than a configuration file. DDL statements are directly parsed to obtain database table information without the need to pre-create the database table structure. Furthermore, the generated target code is not in a fixed format and can be dynamically adjusted according to the template format, thus providing greater flexibility and ease of use.

[0070] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.

Claims

1. A back-end code generation method, characterized by, include: Retrieve the variable properties and method types used from the pre-defined front-end page code template; The variable attribute values ​​in the preset code framework based on the backend language are replaced with placeholders, and the method code block pre-encapsulated based on the template engine and indicated by the method type is written into the code framework to form the code template based on the template engine. Determine the structure and field information of the target database table based on the Data Definition Language (DDL) statements for the project to be processed; The DDL statement is input by the user, and the target database table is the database table associated with the project to be processed. The determined structure information and field information are written into the method code block of the code template selected by the user, and the attribute values ​​in the placeholder form in the code template are replaced with the attribute values ​​of the variable attributes of the project to be processed input by the user, so as to generate the code of the project to be processed based on the backend language.

2. The back-end code generation method of claim 1, wherein, The method further includes: After obtaining the method type, replace the method name corresponding to the method type in the code framework with a placeholder. After obtaining the DDL statement, the method name corresponding to the method type in the project to be processed is generated according to the DDL statement, and the generated method name is used to replace the placeholder method name in the code template selected by the user.

3. The backend code generation method according to claim 1, characterized in that, The method further includes: obtaining condition value data from the method code block indicated by the method type from the front-end page; and... The step of writing the method code block, which is pre-encapsulated based on the template engine and indicates the method type, into the code framework includes: writing the obtained condition value data into the method code block, and writing the method code block containing the condition value data into the code framework.

4. The backend code generation method according to claim 1, characterized in that, The method further includes: Before forming the code template based on the template engine, the file extension of the code framework file containing the method code block is changed to the file extension corresponding to the template engine. Before generating the code for the project to be processed, the file extension of the code template containing the structure information, the field information, and the attribute values ​​of the variable attributes of the project to be processed is modified to the file extension corresponding to the backend language.

5. The backend code generation method according to claim 1, characterized in that, The method further includes: After the code template based on the template engine is formed, a configuration file containing the path information of the code template is generated, which is used to locate the code template when generating the code of the project to be processed.

6. The backend code generation method according to any one of claims 1-5, characterized in that, The method further includes: After the determined structure information and field information are written into the method code block of the code template selected by the user, the naming style in the code template is modified to the naming style of the backend language, and the data type in the code template is modified to the data type of the backend language. The code configuration file format is further obtained from the front-end page so that a configuration file of the corresponding format is generated when the code for the project to be processed is generated; and, The DDL statement and the variable attribute values ​​of the project to be processed are entered by the user on the front-end page; the variable attributes include at least one of the following: organization identifier, project name, project identifier, package name under the project; the method type includes at least one of the following: add, delete, modify, query, upload, download.

7. A backend code generation device, characterized in that, include: The template data acquisition unit is used to retrieve variable attributes and method types from a pre-defined front-end page in the code template. The template building unit is used to replace the attribute values ​​of the variable attributes in the preset code framework based on the backend language with placeholders, and to write the method code block, which is pre-encapsulated based on the template engine and indicates the method type, into the code framework to form the code template based on the template engine. The code generation unit is used to: determine the structure and field information of the target database table based on the Data Definition Language (DDL) statement for the project to be processed; the DDL statement is input by the user, and the target database table is the database table associated with the project to be processed; The determined structure information and field information are written into the method code block of the code template selected by the user, and the attribute values ​​in the placeholder form in the code template are replaced with the attribute values ​​of the variable attributes of the project to be processed input by the user, so as to generate the code of the project to be processed based on the backend language.

8. The apparatus according to claim 7, characterized in that, The template data acquisition unit is further configured to: acquire condition value data from the method code block indicated by the method type from the front-end page; The template construction unit is further configured to: write the obtained condition value data into the method code block, write the method code block containing the condition value data into the code framework; and after obtaining the method type, replace the method name corresponding to the method type in the code framework with a placeholder. The code generation unit is further configured to: after obtaining the DDL statement, generate a method name corresponding to the method type in the project to be processed based on the DDL statement, and replace the placeholder method name in the code template selected by the user with the generated method name.

9. An electronic device, comprising: include: One or more processors; Storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1-6.

10. A computer-readable storage medium having stored thereon a computer program, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-6.

Citation Information

Patent Citations

  • Code generation method and device, storage medium and electronic device

    CN114253546A

  • Method and device for automatically generating codes, electronic equipment and storage medium

    CN114721641A