Code generation method and system

Through custom database dialect classes and Doma2 frameworks, the database table structure information is mapped to Java class attributes, and the FreeMarker template engine outputs code, solving the problem of limited support scope and poor flexibility of existing code generation methods, achieving efficient and flexible Code generation and integration.

CN119440488APending Publication Date: 2025-02-14FOUNDER INT(WUHAN)TECH DEV CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411593020.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-08
Publication Date
2025-02-14

AI Technical Summary

Technical Problem

Existing code generation methods only support specific database access frameworks or development environments, and the flexibility and scalability of generating code are poor, which cannot meet changing development needs.

Method used

By reading the database table structure information based on the database configuration file, using the customized database dialect class and Doma2 framework to map the table structure information to Java class attributes, and output Java entity class, DAO class and SQL statements through the FreeMarker template engine, and finally These codes are integrated into the Spring MVC framework.

Benefits of technology

It realizes automated code generation, improves code development efficiency, has high reusability and consistency, reduces the workload of post-maintenance and debugging, and supports database compatibility and flexible expansion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119440488A_ABST
    Figure CN119440488A_ABST
Patent Text Reader

Abstract

The invention provides a code generation method and system, and the method comprises the steps: reading a database based on a database configuration file, and obtaining table structure information in the database; based on a self-defined database dialect class, table structure information is mapped to attributes of a Java class through a Doma2 framework, a Java entity class, a DAO class and an SQL statement are output through a FreeMarker template engine, and the DAO class comprises annotation configuration information; and integrating the Java entity class, the DAO class and the SQL statement into a Spring MVC framework according to the annotation configuration information to obtain a database access layer code. According to the scheme, automatic generation of the database operation codes can be achieved, the flexibility and expansibility of generated codes can be guaranteed, and high reusability and consistency are achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the field of software development, and in particular relates to a code generation method and system. Background Art

[0002] In the process of software development, code generation technology has been widely used in various development frameworks. Among them, the technology of generating code based on database access framework is more common. These code generation technologies often use fixed templates or tools to obtain database access code, but because these templates or tools are only applicable to specific database access frameworks or development environments, the support for Doma2 database access framework is not perfect.

[0003] Currently, the use of fixed templates to generate database access code will result in the tool only supporting specific database access frameworks or development environments due to the limitations of the code generation logic. In view of the ever-changing development needs of today, the flexibility and scalability of the generated code are poor, which limits its scope of application. Summary of the invention

[0004] In view of this, an embodiment of the present invention provides a code generation method and system for solving the problem that the existing code generation method only supports a specific database access framework or development environment and has poor flexibility and scalability of the generated code.

[0005] In a first aspect of an embodiment of the present invention, a code generation method is provided, comprising: Read the database based on the database configuration file to obtain the table structure information in the database; Based on the custom database dialect class, the table structure information is mapped to the properties of the Java class through the Doma2 framework, and the Java entity class, DAO class and SQL statement are output through the FreeMarker template engine. The DAO class contains annotation configuration information. According to the annotation configuration information, the Java entity class, DAO class and SQL statement are integrated into the Spring MVC framework to obtain the database access layer code.

[0006] In a second aspect of an embodiment of the present invention, a code generation system is provided, including: A data acquisition module is used to read the database based on the database configuration file and obtain the table structure information in the database; A mapping output module is used to map the table structure information to the properties of the Java class through the Doma2 framework based on the custom database dialect class, and output the Java entity class, DAO class and SQL statement through the FreeMarker template engine, wherein the DAO class contains annotation configuration information; The code integration module is used to integrate Java entity classes, DAO classes and SQL statements into the Spring MVC framework according to annotation configuration information to obtain the database access layer code.

[0007] In a third aspect of an embodiment of the present invention, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the method described in the first aspect of the embodiment of the present invention when executing the computer program.

[0008] In a fourth aspect of an embodiment of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method provided in the first aspect of the embodiment of the present invention are implemented.

[0009] In the embodiment of the present invention, based on the customized database dialect class, the Doma2 framework is used to map the table structure information to the properties of the Java class, output through the FreeMarker template engine, and integrate the code into the Spring MVC framework according to the annotation configuration information in the DAO class. This can not only realize automatic code generation and improve code development efficiency, but also realize database compatibility, ensure the flexibility and extensibility of the generated code, and have high reusability and consistency, thereby reducing the workload of later maintenance and debugging.

[0010] At the same time, based on the database dialect customization, it can automatically handle complex primary key generation strategies and support mapping processing for specific data types. In addition, the DAO layer code supports Spring MVC's dependency injection and transaction management, allowing developers to quickly integrate the database access layer and reduce the workload of manual configuration. BRIEF DESCRIPTION OF THE DRAWINGS

[0011] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0012] Figure 1 A schematic diagram of a flow chart of a code generation method provided by an embodiment of the present invention; Figure 2 A schematic diagram of the structure of a code generation system provided by one embodiment of the present invention; Figure 3 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0013] In order to make the purpose, features and advantages of the present invention more obvious and easy to understand, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described below are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0014] It should be understood that the term "including" and other similar expressions in the specification or claims of the present invention and the above-mentioned drawings are intended to cover non-exclusive inclusions, such as a process, method, system, or device including a series of steps or units is not limited to the listed steps or units. In addition, "first" and "second" are used to distinguish different objects, not to describe a specific order.

[0015] See also Figure 1 , a flowchart of a code generation method provided by an embodiment of the present invention includes: S101, reading a database based on a database configuration file to obtain table structure information in the database; The database configuration file refers to a file used to store database connection and configuration information, which is generally located in a specified directory of the operating system, a database installation directory, or a specified location in an environment variable, and contains database connection information, server settings, performance optimization parameters, etc.

[0016] Based on the database configuration file, you can connect to and access the database, and read the table structure information in the database. The table structure information may include table names, field names, etc.

[0017] Among them, it is determined whether there is a table structure in the database. If there is no table structure, the code generation is terminated. If there is a table structure, the table structure information is read, and the table structure information includes the table name, primary key and field.

[0018] S102, based on the custom database dialect class, map the table structure information to the properties of the Java class through the Doma2 framework, and output the Java entity class, DAO class and SQL statement through the FreeMarker template engine, wherein the DAO class contains annotation configuration information; The database dialect class can be called a database dialect library or a database adapter. It is a software component used to process SQL statements and data types of a specific database system. It can convert general SQL statements into SQL statements that can be understood by a specific database system, process data types unique to the database system, and provide support for database system-specific functions.

[0019] By customizing the database dialect class, you can actively adapt to different database operation codes, enhance the flexibility of the code, and generate codes that contain complex business logic, data verification, and transaction management to adapt to more complex business scenarios.

[0020] The Doma2 framework is an open source object-relational mapping (ORM) framework that allows developers to efficiently perform database operations in SpringBoot applications and enables developers to focus on the implementation of business logic by simplifying database interactions.

[0021] Based on the Doma2 framework, the table structure information in the database can be mapped to the Java class attributes. For example, the common data types in the database, such as DATE and TIMESTAMP, can be mapped to the corresponding data types in Java, such as java.util.Date and java.sql.Timestamp.

[0022] The FreeMarker template engine is a Java class library, which is usually embedded in a Java Web project and used to dynamically generate content. It can be used as a general tool to generate output text (such as source code, etc.) based on a template and data to be changed. The FreeMarker template engine can be used to convert the attribute information in the Java class according to the defined template to generate Java entity classes, DAO classes and SQL statements.

[0023] Preferably, a custom code generation template is used, and based on the code generation template, Java entity classes, DAO classes and SQL statements with a unified code style are output through a FreeMarker template engine.

[0024] It can be understood that although the generated code is mainly used for basic CRUD operations, developers are allowed to add complex business logic to custom templates so that the generated code can flexibly adapt to different business needs.

[0025] DAO class, or Data Access Object class, is a design pattern whose core functions include encapsulating CRUD (Create, Read, Update, Delete) operations on the database, providing a unified API interface, separating data access logic from business logic, and increasing code reusability and maintainability. By encapsulating data access logic in the DAO class, the code can be made more modular, reducing the coupling between the various parts, and improving the maintainability and scalability of the system.

[0026] The DAO class contains some commonly used annotation configuration information, which is mainly used to identify the data access object class and provide additional functional support. The annotation configuration information can include @Repository, @Entity, @GeneratedValue, etc. The generated DAO class will automatically contain the annotations required by Spring MVC to ensure that it can be automatically injected and used in the Spring framework.

[0027] The annotation configuration information includes at least the annotation of the data access layer component (ie, @Repository‌) and the annotation of automatic assembly (ie, @ConfigAutowireable).

[0028] Optionally, based on a custom dialect class of the database, fields are generated for the auto-increment primary key and sequence in the database, and predetermined annotation configuration information is generated, the predetermined annotation configuration information is the @SequenceGenerator annotation. This annotation is used to specify the generation strategy of the entity class primary key, especially when the database sequence is used to generate the primary key value, and is usually used together with the @GeneratedValue annotation, which specifies the generation strategy as SEQUENCE. .

[0029] S103. Integrate the Java entity class, DAO class and SQL statement into the Spring MVC framework according to the annotation configuration information to obtain the database access layer code.

[0030] According to the annotation configuration information in the DAO class, Java entity classes, DAO classes and SQL statements can be automatically injected and managed in the Spring MVC framework. At the same time, it supports the use of Spring's transaction management and exception handling mechanisms.

[0031] It can be understood that the generated DAO class supports Spring's dependency injection mechanism and can automatically include annotations such as @Repository and @ConfigAutowireable to ensure that the code can be managed and injected by Spring in the Spring MVC project, allowing developers to quickly integrate the database access layer and reduce the workload of manual configuration.

[0032] In this embodiment, the complexity of code generation is reduced and development efficiency is improved through automatic code generation. Developers can quickly generate Entity layer, Dao layer, and SQL code that meet project requirements, reduce the time cost of manually writing basic code, and reduce development difficulty.

[0033] The code generated based on the Doma2 framework has high reusability and consistency, which reduces the workload of subsequent maintenance and debugging.

[0034] By customizing the code generation template, you can ensure the uniformity of code style and structure, reduce code duplication and differences, and improve the maintainability and readability of the code. At the same time, developers can modify and expand the template according to actual needs to generate code that meets specific business needs, enhancing the flexibility and scalability of the code.

[0035] It supports database dialect customization, can automatically handle complex primary key generation strategies, such as generating primary keys through sequences, and supports mapping processing for specific data types.

[0036] Achieve seamless integration of Spring MVC. The generated DAO layer code supports Spring MVC's dependency injection and transaction management, allowing developers to quickly integrate the database access layer and reduce the workload of manual configuration.

[0037] It should be understood that the serial numbers of the steps in the above embodiments do not imply a sequence of execution, and the execution sequence of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.

[0038] Figure 2 A schematic diagram of the structure of a code generation system provided by an embodiment of the present invention, the system comprising: The data acquisition module 210 is used to read the database based on the database configuration file and obtain the table structure information in the database; Among them, it is determined whether there is a table structure in the database. If there is no table structure, the code generation is terminated. If there is a table structure, the table structure information is read, and the table structure information includes the table name, primary key and field.

[0039] A mapping output module 220 is used to map the table structure information to the attributes of the Java class through the Doma2 framework based on the custom database dialect class, and output the Java entity class, DAO class and SQL statement through the FreeMarker template engine, wherein the DAO class contains annotation configuration information; Preferably, a custom code generation template is used, and based on the code generation template, Java entity classes, DAO classes and SQL statements with a unified code style are output through a FreeMarker template engine.

[0040] Optionally, based on the custom dialect class of the database, fields are generated for the auto-increment primary key and sequence in the database, and predetermined code annotation information is generated, where the predetermined code annotation information is the @SequenceGenerator annotation.

[0041] The annotation configuration information at least includes annotations of data access layer components and annotations of automatic assembly.

[0042] The code integration module 230 is used to integrate the Java entity class, DAO class and SQL statement into the Spring MVC framework according to the annotation configuration information to obtain the database access layer code.

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

[0044] Figure 3 1 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. The electronic device is used to automatically generate a database operation code. Figure 3 As shown, the electronic device 3 of this embodiment includes: a memory 310, a processor 320 and a system bus 330, and the memory 310 includes an executable program 3101 stored thereon. Those skilled in the art can understand that Figure 3 The electronic device structure shown in the figure does not constitute a limitation of the electronic device, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.

[0045] Combine the following Figure 3 A detailed introduction to the various components of electronic equipment: The memory 310 can be used to store software programs and modules. The processor 320 executes various functional applications and data processing of the electronic device by running the software programs and modules stored in the memory 310. The memory 310 can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area can store data created according to the use of the electronic device (such as cache data), etc. In addition, the memory 310 can include a high-speed random access memory, and can also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.

[0046] The memory 310 includes an executable program 3101 of a data set conversion method, and the executable program 3101 can be divided into one or more modules / units, which are stored in the memory 310 and executed by the processor 320 to generate codes, etc. The one or more modules / units can be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the executable program 3101 in the electronic device 3. For example, the executable program 3101 can be divided into functional modules such as a data acquisition module, a mapping output module, and a code integration module.

[0047] The processor 320 is the control center of the electronic device. It uses various interfaces and lines to connect various parts of the entire electronic device. By running or executing software programs and / or modules stored in the memory 310, and calling data stored in the memory 310, it performs various functions of the electronic device and processes data, thereby monitoring the overall status of the electronic device. Optionally, the processor 320 may include one or more processing units; preferably, the processor 320 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, application programs, etc., and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 320.

[0048] The system bus 330 is used to connect the various functional components inside the computer, and can transmit data information, address information, and control information. Its types can be, for example, PCI bus, ISA bus, CAN bus, etc. The instructions of the processor 320 are transmitted to the memory 310 through the bus, and the memory 310 feeds back data to the processor 320. The system bus 330 is responsible for the data and instruction exchange between the processor 320 and the memory 310. Of course, the system bus 330 can also be connected to other devices, such as network interfaces, display devices, etc.

[0049] In the embodiment of the present invention, the executable program executed by the processor 320 included in the electronic device includes: Read the database based on the database configuration file to obtain the table structure information in the database; Based on the custom database dialect class, the table structure information is mapped to the properties of the Java class through the Doma2 framework, and the Java entity class, DAO class and SQL statement are output through the FreeMarker template engine. The DAO class contains annotation configuration information. According to the annotation configuration information, the Java entity class, DAO class and SQL statement are integrated into the Spring MVC framework to obtain the database access layer code.

[0050] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and modules described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0051] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0052] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A code generation method, characterized in that: include: Read the database based on the database configuration file to obtain the table structure information in the database; Based on the custom database dialect class, the table structure information is mapped to the properties of the Java class through the Doma2 framework, and the Java entity class, DAO class and SQL statement are output through the FreeMarker template engine. The DAO class contains annotation configuration information. According to the annotation configuration information, the Java entity class, DAO class and SQL statement are integrated into the Spring MVC framework to obtain the database access layer code.

2. The method according to claim 1, characterized in that The step of reading the database based on the database configuration file and obtaining the table structure information in the database includes: Determine whether there is a table structure in the database. If not, terminate code generation. If there is a table structure, read the table structure information, which includes the table name, primary key, and fields.

3. The method according to claim 1, characterized in that The method based on the custom database dialect class maps the table structure information to the attributes of the Java class through the Doma2 framework, and outputs the Java entity class, DAO class and SQL statement through the FreeMarker template engine, including: Customize the code generation template, based on the code generation template, output Java entity classes, DAO classes and SQL statements with unified code style through the FreeMarker template engine.

4. The method according to claim 1, characterized in that: The method based on the custom database dialect class maps the table structure information to the attributes of the Java class through the Doma2 framework, and outputs the Java entity class, DAO class and SQL statement through the FreeMarker template engine, including: Based on the custom dialect class of the database, fields are generated for the auto-increment primary key and sequence in the database, and predetermined code annotation information is generated, where the predetermined code annotation information is the @SequenceGenerator annotation.

5. The method according to claim 1, characterized in that The annotation configuration information at least includes annotations of data access layer components and automatic assembly annotations.

6. A code generation system, characterized in that: include: A data acquisition module is used to read the database based on the database configuration file and obtain the table structure information in the database; A mapping output module is used to map the table structure information to the properties of the Java class through the Doma2 framework based on the custom database dialect class, and output the Java entity class, DAO class and SQL statement through the FreeMarker template engine, wherein the DAO class contains annotation configuration information; The code integration module is used to integrate Java entity classes, DAO classes and SQL statements into the SpringMVC framework according to annotation configuration information to obtain the database access layer code.

7. The system according to claim 6, characterized in that The method based on the custom database dialect class maps the table structure information to the attributes of the Java class through the Doma2 framework, and outputs the Java entity class, DAO class and SQL statement through the FreeMarker template engine, including: Customize the code generation template, based on the code generation template, output Java entity classes, DAO classes and SQL statements with unified code style through the FreeMarker template engine.

8. The system according to claim 6, characterized in that The method based on the custom database dialect class maps the table structure information to the attributes of the Java class through the Doma2 framework, and outputs the Java entity class, DAO class and SQL statement through the FreeMarker template engine, including: Based on the custom dialect class of the database, fields are generated for the auto-increment primary key and sequence in the database, and predetermined code annotation information is generated, where the predetermined code annotation information is the @SequenceGenerator annotation.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of a code generation method according to any one of claims 1 to 5 are implemented.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed, the steps of a code generation method according to any one of claims 1 to 5 are implemented.