A code generation method and apparatus, an electronic device, and a medium

CN116185389BActive Publication Date: 2026-10-27INSPUR GENERSOFT CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202310033586.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-01-10
Publication Date
2026-10-27
Estimated Expiration
2043-01-10

AI Technical Summary

Technical Problem

[0004]本申请实施例提供了一种代码生成方法、装置、电子设备及介质,用于解决如下技术问题:现有的代码生成方法重复操作过多,导致代码生成过程繁琐,代码生成效率较低

Benefits of technology

[0016]与现有技术相比,本申请实施例提供的一种代码生成方法、装置、电子设备及介质,通过预先配置的数据库链接信息从数据库获取待生成代码的数据库表,根据数据库表生成数据模型,根据数据模型和预设的代码模板生成代码,能够将待生成代码的数据库表转换为数据模型,并用预设的代码模板生成代码,将节省开发人员的很多精力,减少人工错误,使得开发人员不必浪费过多精力在数据库的相关操作上,从而提高代码生成的效率。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116185389B_ABST
    Figure CN116185389B_ABST
Patent Text Reader

Abstract

The application discloses a code generation method and device, electronic equipment and medium, and belongs to the technical field of computers, and is used for solving the technical problem that the existing code generation method has too many repeated operations, leading to a complicated code generation process and low code generation efficiency. The method comprises the following steps: acquiring a database table to be generated according to pre-configured database connection information; generating a corresponding data model according to the database table; and generating the code corresponding to the database table according to the data model and a preset code template. The method can generate a complete set of corresponding codes according to actual needs, improves the development efficiency of the system, and is simple and fast to operate. In this way, the software output efficiency of an enterprise can be improved, engineers can pay more attention to business logic, greater social value can be created, much effort of a developer can be saved, manual errors can be reduced, and the developer does not have to waste too much effort on the related operation of the database, so that the code generation efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a code generation method, apparatus, electronic device, and medium. Background Technology

[0002] In software development, various code generation tools have been widely used to increase system stability, reduce software development and maintenance costs, and improve development efficiency. However, writing code often involves repetitive tasks, and repeatedly processing large amounts of identical code is tedious and laborious. Furthermore, copying and pasting cannot guarantee code consistency and standardization, nor can it guarantee the correctness of the copied code.

[0003] Existing code generation methods require users to select repetitive content multiple times during the code generation process, and may even require users to input corresponding commands via keyboard to generate code. The code generation process is cumbersome and inefficient. Summary of the Invention

[0004] This application provides a code generation method, apparatus, electronic device, and medium to solve the following technical problem: existing code generation methods involve too many repetitive operations, resulting in a cumbersome code generation process and low code generation efficiency.

[0005] The embodiments of this application adopt the following technical solutions:

[0006] On one hand, embodiments of this application provide a code generation method, the method comprising: obtaining a database table for which code to be generated is to be obtained according to pre-configured database connection information; generating a corresponding data model according to the database table; and generating code corresponding to the database table according to the data model and a preset code template.

[0007] In one feasible implementation, the database table for which the code to be generated is obtained according to pre-configured database connection information, specifically including: connecting to the corresponding database according to the pre-configured database connection information; wherein the database connection information includes at least the database name, database type, username, and user password; searching the database table for which the code to be generated is obtained in the database; obtaining the row data and metadata of the database table; wherein the row data is the data included in each row of the table; and the metadata is used to describe the attribute information of each row of data.

[0008] In one feasible implementation, a corresponding data model is generated based on the table information of the database table, specifically including: determining the metadata corresponding to each group of row data; wherein the metadata includes at least one keyword, the keyword including whether it is identification information, whether it is a primary key, field size, field type and field name; filling the row data into the row information, and filling the metadata into the column information of the corresponding row data to generate the data model.

[0009] In one feasible implementation, before generating the code corresponding to the database table based on the data model and the preset code template, the method further includes: determining the fixed fields of the target keyword; determining the variable fields of the target keyword and the positions of the variable fields in the fixed fields; setting the preset code template based on the fixed fields and the variable fields; wherein the preset code template corresponds to the target keyword.

[0010] In one feasible implementation, the code corresponding to the database table is generated based on the data model and the preset code template, specifically including: determining the keyword corresponding to each group of row information in the data model; matching the keyword corresponding to each group of row information with the target keyword of each preset code template to determine the preset code template corresponding to each group of row information; filling the row data corresponding to each group of row information into the variable field of the corresponding preset code template to obtain the corresponding row code; and combining all the generated row codes into the code corresponding to the database table.

[0011] In one feasible implementation, after generating the code corresponding to the database table based on the data model and the preset code template, the method further includes: determining the business type of the database table; determining the fixed business template corresponding to the business type; wherein the fixed business template defines a fixed code format corresponding to the business type; and adding the fixed business template to the generated code to obtain the standard code corresponding to the business type.

[0012] In one feasible implementation, after generating the code corresponding to the database table based on the data model and the preset code template, the method further includes: determining the translation type of the code; and converting the code into the format of the translation type.

[0013] Secondly, embodiments of this application also provide a code generation apparatus, the apparatus comprising: a database table acquisition unit, configured to acquire a database table for which code to be generated is to be acquired based on pre-configured database connection information; a data model generation unit, configured to generate a corresponding data model based on the table information of the database table; and a processing unit, configured to generate code corresponding to the database table based on the data model and a preset code template.

[0014] Thirdly, embodiments of this application also provide a code generation electronic device, the electronic device comprising: 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 to enable the at least one processor to execute a code generation method according to any of the above embodiments.

[0015] Fourthly, embodiments of this application also provide a computer-readable medium storing computer instructions, which, when executed by a processor, enable the processor to perform a code generation method according to any of the above embodiments.

[0016] Compared with the prior art, the code generation method, apparatus, electronic device and medium provided in this application embodiment obtains the database table of the code to be generated from the database through pre-configured database link information, generates a data model based on the database table, and generates code based on the data model and a preset code template. It can convert the database table of the code to be generated into a data model and generate code using a preset code template, which will save developers a lot of effort, reduce human error, and allow developers to avoid wasting too much effort on database-related operations, thereby improving the efficiency of code generation. Attached Figure Description

[0017] 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, the drawings described below are only some embodiments recorded in this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. In the drawings:

[0018] Figure 1 A flowchart of a code generation method provided in an embodiment of this application;

[0019] Figure 2 A flowchart illustrating a preset code template setting method provided in this application embodiment.

[0020] Figure 3 This is a schematic diagram of the structure of a code generation device provided in an embodiment of this application;

[0021] Figure 4 This is a schematic diagram of the structure of a code generation electronic device provided in an embodiment of this application. Detailed Implementation

[0022] To enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this specification, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this application.

[0023] This application provides a code generation method, such as... Figure 1 As shown, the code generation method specifically includes steps S110-S130:

[0024] S110. Obtain the database table for the code to be generated based on the pre-configured database connection information.

[0025] Specifically, the system first connects to the corresponding database based on the pre-configured database connection information. This database connection information contains the necessary data for downloading the database tables, primarily described by the database configuration data, and may include the database name, type, server username, password, and name, etc.

[0026] Furthermore, within the database, the database table for which the code to be generated is located, and the table's row data and metadata are retrieved. A database table represents the basic configuration of a table within the database. Row data consists of the data included in each row of the table; metadata describes the attribute information of each row.

[0027] As a feasible implementation, the database table may include, but is not limited to, the table's field information and metadata. The field information includes at least one of the following: field name, field type, and field attribute of each table field. The metadata includes at least one of the following: table name and primary / foreign key relationships. The database can be any type of database such as Oracle, MySQL, or SQL Server; this disclosure does not limit this type.

[0028] S120. Generate the corresponding data model based on the database tables.

[0029] Specifically, the data model describes the requirements of the system and serves as the foundational model for the automatic code generation framework. It transforms database tables into a data model within the framework, enabling rapid code generation. The data model framework includes row and column information.

[0030] First, determine the metadata corresponding to each group of row data. This metadata must include at least one key, such as whether it is identification information, whether it is a primary key, field size, field type, and field name. Then, fill the row data into the row information field and the metadata into the corresponding column information field to generate the data model.

[0031] Each row in a database table corresponds to a set of row data. The row data in a database table is configured to describe the content of the database, and the row data in each row describes the data included in that row. Metadata describes the relevant attributes of the row data.

[0032] In this embodiment of the invention, the metadata includes at least one keyword, which includes whether it is identification information, whether it is a primary key, size, type, and name. The structure of the database table and view is mainly described by the metadata generated by the database table and view, and described in the column information. For example, the m column information is "whether it is a primary key". If the data in the nth row is a primary key, then the data in the nth row of the database table is marked by the metadata to identify that the data in that row is a primary key.

[0033] S130. Generate the code corresponding to the database table based on the data model and the preset code template.

[0034] Specifically, the preset code template is created from pre-written sample code or selected sample code. The preset code template includes fixed content and content used to populate the data based on the data in the data model. First, the fixed fields of the target keyword are determined. Then, the variable fields of the target keyword and their positions within the fixed fields are determined. Finally, the preset code template is set based on the fixed and variable fields. The preset code template corresponds to the target keyword.

[0035] Further, the keywords corresponding to each group of row information in the data model are determined. Then, the keywords corresponding to each group of row information are matched with the target keywords of each preset code template to determine the preset code template corresponding to each group of row information. The row data corresponding to each group of row information is filled into the variable fields of the corresponding preset code template to obtain the corresponding row code. Finally, all the generated row codes are combined into the code corresponding to the database table.

[0036] During the code generation process, based on code templates, repetitive and fixed code does not need to be manually rewritten. The code can be obtained simply by filling the data in the data model into the code template, which ensures that the generated code is standardized and accurate, and effectively improves the efficiency of code generation.

[0037] Each keyword corresponds to a preset code template. The keyword corresponding to the row information is used to identify the preset code template required for that row of data. In this application, when determining the variable field corresponding to the difference content replacement identifier, the correct preset code template can be selected based on the keyword, thereby further ensuring the correctness of content filling and providing a prerequisite for the automatic generation of subsequent code.

[0038] Specifically, keywords can include: class name, one or more attributes of the class, attribute type, attribute name, package name, reference to a single class file, reference set of multiple class files, database table name, primary key, primary key type, column name in the database, attribute data type in the table, etc.

[0039] Furthermore, after generating the code, the method also includes the following steps:

[0040] Determine the business type of the database table, determine the corresponding fixed business template, and finally add the fixed business template to the generated code.

[0041] The business type represents the business scenario corresponding to the data table when it is converted into code. For example, in Java, the code needs to include certain fixed formats for that business type, such as fixed prefixes or suffixes. Therefore, based on the generated code, a fixed business template including the fixed format of the business type is added to the code to meet the needs of different business scenarios. Specifically, the fixed business template can be set according to the name and path of the ORM components and solution, the system namespace, and the development tools and their versions.

[0042] As a feasible implementation method, this application can also determine the code translation type and then convert the generated code into the format of that translation type. Code generation can be achieved through a code generation engine. The code generation engine is a standalone application that occupies a core position in the entire code generation framework. Based on the data model of the management information system, the code generation engine generates code according to different code templates to complete basic data query, modification, deletion, and addition operations. Furthermore, the code can be converted into different translation types to adapt to more usage scenarios.

[0043] This application provides a code generation method, apparatus, electronic device, and medium. It retrieves a database table containing the code to be generated from a database using pre-configured database link information, generates a data model based on the database table, and generates code based on the data model and a preset code template. The solution provided by this application can convert the database table containing the code to be generated into a data model and generate code using a preset code template. This saves developers a lot of effort, reduces human error, and eliminates the need for developers to waste excessive energy on database-related operations, thereby improving the efficiency of code generation.

[0044] Figure 2 A flowchart illustrating a preset code template setting method provided in this application embodiment is shown below. Figure 2 As shown, the method includes:

[0045] S210. Determine the fixed fields of the target keyword.

[0046] S220. Determine the variable fields of the target keyword and the position of the variable fields in the fixed fields.

[0047] S230. Set a preset code template based on fixed fields and variable fields.

[0048] Before constructing a preset code template, keywords and their meanings should be defined to define the preset code template, ensure that the style of the preset code template is consistent, and provide feasible conditions for the unified management of the preset code template.

[0049] Pre-defined code templates are essentially abstract code formats. Designers only need to pre-build a code template for a specific type and provide the program code to generate various concrete classes, thereby effectively improving programming efficiency. Code templates can generate concrete classes and their objects through instantiation of class templates.

[0050] During the code generation process, the variable fields corresponding to the replacement identifiers of the differences in the code generation template are determined, and data is populated in the variable fields to generate code based on the processed code template. This allows for automatic code generation by filling in and replacing the differences based on the code template.

[0051] As a feasible implementation method, fixed fields and variable fields can be distinguished by extracting differences and common content from the sample code. Variable fields refer to fields that need to be changed or adjusted during code generation, while fixed fields refer to common content that does not need to be modified during code generation. Specifically, the target sample code can be processed using the Velocity template engine, or alternatively, the Freemarker template engine or other custom template engines. Based on the differences and common content, code templates are created, ensuring efficient code template creation.

[0052] Figure 3 This is a schematic diagram of the structure of a code generation device provided in an embodiment of the present invention, such as... Figure 3 As shown, the device includes:

[0053] The database table acquisition unit 310 is used to acquire the database table of the code to be generated based on the pre-configured database connection information.

[0054] The data model generation unit 320 is used to generate a corresponding data model based on the table information of the database table;

[0055] The processing unit 330 is used to generate the code corresponding to the database table based on the data model and the preset code template.

[0056] Optionally, the database table acquisition unit 310 is configured to perform the following: connect to the corresponding database according to the pre-configured database connection information; wherein the database connection information includes at least the database name, database type, username, and user password; search for the database table of the code to be generated in the database; and acquire the row data and metadata of the database table; wherein the row data is the data included in each row of the table; and the metadata is used to describe the attribute information of each row of data.

[0057] Optionally, the data model generation unit 320 is configured to: determine the metadata corresponding to each group of row data; wherein the metadata includes at least one keyword, the keyword including whether it is identification information, whether it is a primary key, field size, field type and field name; fill the row data into the row information, fill the metadata into the column information of the corresponding row data, and generate the data model.

[0058] Correspondingly, such as Figure 3 As shown, the device further includes: a preset code template setting unit 340;

[0059] The preset code template setting unit 340 is used to set the preset code template in the following manner:

[0060] Determine the fixed fields of the target keyword;

[0061] Determine the variable fields of the target keyword and the positions of the variable fields within the fixed fields;

[0062] Based on the fixed field and the variable field, a preset code template is set; wherein the preset code template corresponds to the target keyword.

[0063] Optionally, the processing unit 330 is configured to perform the following: determine the keyword corresponding to each group of row information in the data model; match the keyword corresponding to each group of row information with the target keyword of each preset code template to determine the preset code template corresponding to each group of row information; fill the row data corresponding to each group of row information into the variable field of the corresponding preset code template to obtain the corresponding row code; and combine all the generated row codes into the code corresponding to the database table.

[0064] Optionally, the processing unit 330 is further configured to perform: determining the business type of the database table; determining the business fixed template corresponding to the business type; wherein the business fixed template defines a fixed code format corresponding to the business type; and adding the business fixed template to the generated code to obtain the standard code corresponding to the business type.

[0065] Optionally, the processing unit 330 is further configured to: determine the translation type of the code; and convert the code into the format of the translation type.

[0066] It is understood that the structures illustrated in the embodiments of the present invention do not constitute a specific limitation on the code generation apparatus. In other embodiments of the present invention, the code generation apparatus may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.

[0067] The information interaction and execution process between the various units in the above-mentioned device are based on the same concept as the method embodiment of the present invention, and the specific details can be found in the description of the method embodiment of the present invention, and will not be repeated here.

[0068] The code generation device can generate a complete set of code according to actual needs, improving the development efficiency of the system. It is simple and quick to operate, which can not only improve the software output efficiency of enterprises, but also allow engineers to focus more on business logic and create greater social value, rather than spending time on repetitive and simple work.

[0069] Figure 4A schematic diagram of a code generation electronic device 10, which can be used to implement embodiments of the present invention, is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable electronic devices (e.g., helmets, glasses, watches, etc.), and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the invention described and / or claimed herein.

[0070] like Figure 4 As shown, the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12 or a random access memory (RAM) 13, communicatively connected to the at least one processor 11. The memory stores computer programs executable by the at least one processor. The processor 11 can perform various appropriate actions and processes based on the computer program stored in the ROM 12 or loaded from storage unit 18 into the RAM 13. The RAM 13 may also store various programs and data required for the operation of the electronic device 10. The processor 11, ROM 12, and RAM 13 are interconnected via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.

[0071] Multiple components in electronic device 10 are connected to I / O interface 15, including: input unit 16, such as keyboard, mouse, etc.; output unit 17, such as various types of displays, speakers, etc.; storage unit 18, such as disk, optical disk, etc.; and communication unit 19, such as network card, modem, wireless transceiver, etc. Communication unit 19 allows electronic device 10 to exchange information / data with other electronic devices through computer networks such as the Internet and / or various telecommunications networks.

[0072] Processor 11 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. Processor 11 performs the various methods and processes described above, such as code generation methods.

[0073] In some embodiments, the code generation method may be implemented as a computer program tangibly contained in a computer-readable storage medium, such as storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or mounted on electronic device 10 via ROM 12 and / or communication unit 19. When the computer program is loaded into RAM 13 and executed by processor 11, one or more steps of the code generation method described above may be performed. Alternatively, in other embodiments, processor 11 may be configured to execute the code generation method by any other suitable means (e.g., by means of firmware).

[0074] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload-programmable logic electronic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.

[0075] Computer programs used to implement the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when executed by the processor, the computer programs cause the functions / operations specified in the flowcharts and / or block diagrams to be performed. The computer programs may be executed entirely on a machine, partially on a machine, or as a standalone software package, partially on a machine and partially on a remote machine, or entirely on a remote machine or server.

[0076] In the context of this invention, a computer-readable storage medium can be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, apparatus, or electronic device. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or electronic devices, or any suitable combination thereof. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. More specific examples of machine-readable storage media include electrical connections based on 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 fibers, portable compact disk read-only memory (CD-ROM), optical storage electronics, magnetic storage electronics, or any suitable combination thereof.

[0077] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).

[0078] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or computing systems that include middleware components (e.g., application servers), or computing systems that include frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., communication networks). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.

[0079] A computing system can include clients and servers. Clients and servers are generally located far apart and typically interact through communication networks. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other. The server can be a cloud server, also known as a cloud computing server or cloud host, which is a hosting product within the cloud computing service system to address the shortcomings of traditional physical hosts and VPS services, such as high management difficulty and weak business scalability.

[0080] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution of this invention can be achieved, and this is not limited herein.

[0081] The various embodiments in this application are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the embodiments for apparatus, electronic devices, and non-volatile computer storage media are basically similar to the method embodiments, so the descriptions are relatively simple, and relevant parts can be referred to the descriptions of the method embodiments.

[0082] The foregoing has described specific embodiments of this application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than that shown in the embodiments and may still achieve the desired results. Furthermore, the processes depicted in the drawings do not necessarily require the specific or sequential order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0083] The above description is merely an embodiment of this application and is not intended to limit this application. For those skilled in the art, various modifications and variations can be made to the embodiments of this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principle of the embodiments of this application should be included within the scope of the claims of this application.

Claims

1. A code generation method, characterized in that, The method includes: Based on pre-configured database connection information, the database table for which the code to be generated is obtained, specifically including: connecting to the corresponding database according to the pre-configured database connection information; wherein, the database connection information includes at least the database name, database type, username, and user password; searching the database table for which the code to be generated is to be found in the database; obtaining the row data and metadata of the database table; wherein, the row data is the data included in each row of the table; the metadata is used to describe the attribute information of each row of data; Based on the database table, a corresponding data model is generated, specifically including: determining the metadata corresponding to each group of row data; wherein, the metadata includes at least one keyword, the keyword including whether it is identification information, whether it is a primary key, field size, field type and field name; filling the row data into the row information, filling the metadata into the column information of the corresponding row data, and generating the data model; Based on the data model and the preset code template, the code corresponding to the database table is generated, specifically including: determining the keyword corresponding to each group of row information in the data model; matching the keyword corresponding to each group of row information with the target keyword of each preset code template to determine the preset code template corresponding to each group of row information; filling the row data corresponding to each group of row information into the variable field of the corresponding preset code template to obtain the corresponding row code; and combining all the generated row codes into the code corresponding to the database table.

2. The code generation method according to claim 1, characterized in that, Before generating the code corresponding to the database table based on the data model and the preset code template, the method further includes: Determine the fixed fields of the target keyword; Determine the variable fields of the target keyword and the positions of the variable fields within the fixed fields; Based on the fixed field and the variable field, a preset code template is set; wherein the preset code template corresponds to the target keyword.

3. The code generation method according to claim 1, characterized in that, After generating the code corresponding to the database table based on the data model and the preset code template, the method further includes: Determine the business type of the database table; Determine the fixed business template corresponding to the business type; wherein, the fixed business template defines the fixed code format corresponding to the business type; Add the fixed template for the business to the code corresponding to the generated database table to obtain the standard code corresponding to the business type.

4. The code generation method according to claim 1, characterized in that, After generating the code corresponding to the database table based on the data model and the preset code template, the method further includes: Determine the translation type of the code corresponding to the database table; Convert the code corresponding to the database table into the format of the translation type.

5. A code generation apparatus, employing a code generation method as described in any one of claims 1-4, characterized in that, The device includes: The database table retrieval unit is used to retrieve the database tables for the code to be generated based on the pre-configured database connection information. The data model generation unit is used to generate a corresponding data model based on the table information of the database table; The processing unit is used to generate the code corresponding to the database table based on the data model and the preset code template.

6. A code generation electronic device, characterized in that, The electronic device 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 to enable the at least one processor to perform a code generation method according to any one of claims 1-4.

7. A computer-readable medium, characterized in that, The computer-readable medium stores computer instructions that, when executed by a processor, enable the processor to perform a code generation method according to any one of claims 1-4.

Citation Information

Patent Citations

  • A code automatic generation method and a code generator

    CN109359112A