A JPA-based web code automatic generation system
By using a JPA-based web code generation system, combined with template technology and the chain of responsibility pattern, the system automates the generation of database tables and web application systems. This solves the problems of long development cycles and unstable quality in business systems, and improves development efficiency and the ability to respond to business changes.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINA INFOMRAITON CONSULTING & DESIGNING INST CO LTD
- Filing Date
- 2022-08-01
- Publication Date
- 2026-06-02
AI Technical Summary
The existing business system has a long development cycle, the delivery quality is not high due to the cooperation of multiple professions, and the variability of business requirements leads to low development efficiency and uncertain stability.
A JPA-based web code generation system is adopted, which combines JPA technology with template technology. It generates database tables and web application systems through client-side custom annotations, uses the chain of responsibility pattern to parse annotations and generate JSON data, combines Freemarker templates to generate a complete set of code, and achieves automatic deployment through a deployment module.
Significantly improve development efficiency, shorten development cycle, reduce communication costs, and enable one-click deployment and rapid response to changes in business needs.
Smart Images

Figure CN115437616B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of business software development, and in particular relates to a JPA-based automatic web code generation system. Background Technology
[0002] The development of business systems involves processes such as requirements analysis, page prototype design, software architecture design, database design, code development, and testing. Each requirement iteration typically takes weeks. Furthermore, the variability of business requirements means projects often require multiple iterations before delivery and operation. Current development methods result in long implementation cycles, require collaboration across multiple roles, and lead to low delivery quality, uncertain output stability, and prolonged development periods. This significantly impacts project delivery efficiency. Summary of the Invention
[0003] Purpose of the Invention: The technical problem this invention aims to solve is to address the shortcomings of existing technologies by providing a JPA-based automatic web code generation system. This system combines JPA technology with template technology, enabling batch generation of database tables and front-end / back-end code for web applications based on JPA classes. This significantly improves development efficiency. When users adjust table structures or attributes, a single click can further enhance development efficiency.
[0004] This includes the client, code generation server, backend service deployment module, and frontend service deployment module;
[0005] JPA is short for Java Persistence API. It is a mapping relationship between objects and relational tables described by annotations or XML in JDK 5.0. It can automatically generate relational data tables based on annotations during project initialization.
[0006] The client is used to: automatically generate database tables using JPA annotations; input various variables required for template generation using custom user annotations; combine user-input attributes and annotations into JSON format and send it to the code generation server, which then parses the JSON and combines it with the code template to generate a complete set of code.
[0007] The code generation server is used to: design front-end and back-end template code using Freemarker templates; and merge the templates with the code based on the parameters passed by the client to generate a complete set of code.
[0008] The deployment backend service module is used to deploy the application system, receive the files generated by merging according to the template and parameters from the code generation server, distribute them to the specified code location, and configure menus and permissions;
[0009] The deployment front-end service module is used to receive front-end files from the code generation server and distribute them to the specified front-end code location.
[0010] When the client starts, it automatically scans for classes with user annotations in the specified directory. When using the client, the user creates a new class in the editor environment and adds various custom attributes to the new class.
[0011] Annotations include both JPA's built-in annotations and user-defined annotations.
[0012] The annotations provided by JPA include annotations on classes and annotations on properties in the JPA framework.
[0013] The annotations on classes in the JPA framework specifically include:
[0014] @Entity: Used to establish the mapping between JPA entities and tables; it is an annotation defined in the Java Persistence API. It indicates that this class is an entity class and uses the default ORmapping rules, meaning the class name is the table name in the database table, and the class field names are the field names in the table. The @Entity annotation specifies that this is an entity bean.
[0015] @Table: If the user needs to define a separate table name generated in the database, the @Table annotation can override the default table name of @Entity. This @Table annotation specifies the database table to be mapped to the Entity, where @Table.name() is used to specify the table name. It declares the data table that this object maps to in the database, allowing you to specify the table, catalog, and schema names for the entity.
[0016] The annotations on attributes in the JPA framework specifically include:
[0017] @ID: Used to specify that an attribute is mapped to a database primary key;
[0018] @Column: Used to specify the data type when defining the database field corresponding to the attribute;
[0019] User-defined annotations include annotations on classes and annotations on attributes;
[0020] The annotations on the class specifically include:
[0021] The @WVo annotation is used to specify the target location, package name, and template type for code generation.
[0022] @TVo annotation: Used to determine the table description and whether it is a tree structure;
[0023] The annotations on the attributes specifically include:
[0024] @PageVo: Used to specify the attribute of a field as it appears on the page when the page is generated;
[0025] @ValidateTo: Used to specify the conditions to be validated when the page is generated;
[0026] Depending on changes in the code structure of the code generation server and changes in the front-end presentation, other user-defined annotations may be added in the future.
[0027] The client uses the chain of responsibility pattern to parse various annotations, assembles the parsed results into an object, serializes it, and sends it to the code generation server. Then, the startup class inherits from the ApplicationRunner in the Spring Boot container. When the Spring Boot container starts, the client automatically generates database tables, scans a custom directory, parses annotations using reflection, and sends the generated JSON file to the code generation server by calling a remote service.
[0028] When parsing various types of annotations, each annotation is processed by an independent class, and then all processing classes are linked together using a chain of responsibility. This approach is highly extensible; adding a new annotation only requires adding a new class, which conforms to the open / closed principle of object-oriented programming.
[0029] The backend is the server-side, and the backend template code is a multi-layered framework code based on Spring Boot + Mybatis; the frontend template code is based on the Vue 2.0 framework code.
[0030] The parameters that change in the backend Spring Boot multi-layer structure code program include: whether it is a primary key, package name, entity name, table name, table comment, field name, and field comment;
[0031] The parameters that change on a Vue front-end page include: the type of the field displayed on the page: commonly used page control types such as text boxes, dropdown lists, radio buttons, rich text, files, and dates;
[0032] Define the parameters that change between the front-end and back-end as JSON data format;
[0033] The code generation server accepts the JSON file generated by the client, deserializes it, and converts the JSON data into Map key-value data. The Map data is combined with the Freemarker template, and the Freemarker template engine is used to replace the changes in the Freemarker template with parts of the Map collection to generate front-end and back-end code. At the same time, the generated front-end and back-end code is distributed to the deployment system.
[0034] Beneficial effects: The system of this invention deeply integrates multiple technologies such as JPA annotations, custom annotations, and code templates to achieve one-click deployment, which greatly improves development efficiency;
[0035] Using a developer's typical development environment, users simply create a class, input its attributes, and then add JPA annotations and custom annotations. After execution, the database tables, the complete front-end and back-end system are generated and deployed to the application server. After restarting the deployed system, it is immediately accessible. This development model significantly improves development efficiency and greatly shortens the development cycle.
[0036] Embrace Change: During development, business requirements frequently change, each requiring redesign, development, and testing. With this tool, if tables are added, adjusted, or fields are added, deleted, or modified, they can be regenerated. The system automatically overwrites the previous version, allowing for rapid redeployment of the new version.
[0037] This reduces the requirements for developers, allowing business leaders and experts to perform modeling and generate the entire system, significantly reducing various communication costs during the development process. Attached Figure Description
[0038] The present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments, and the advantages of the present invention in the above and / or other aspects will become clearer.
[0039] Figure 1 This is the overall system architecture diagram of the present invention.
[0040] Figure 2 This is a schematic diagram after the server is restarted in the embodiment.
[0041] Figure 3 This is a schematic diagram of the front-end interface.
[0042] Figure 4 This is a diagram illustrating the class structure of the WebModelVO that the client needs to generate. Detailed Implementation
[0043] like Figure 1As shown, the present invention provides a JPA-based automatic web code generation system, including a client, a code generation server, a backend service deployment module, and a frontend service deployment module.
[0044] The client is used for:
[0045] Custom user annotations are used to input information;
[0046] Use JPA annotations to generate database tables;
[0047] When the client starts, it automatically scans for classes with user annotations in the specified directory;
[0048] The chain of responsibility pattern is used to parse various annotations, generate JSON data, and send it to the code generation server; the specific implementation method is as follows.
[0049] 1. JPA's built-in annotations
[0050] In order to automatically generate database tables from the domain model, JPA's built-in annotations are required: @Entity, @Table, and @Column. JPA can automatically generate database tables based on these annotations.
[0051] 2. Class definition annotations
[0052] 2.1 WVO Annotations
[0053] When the code generator generates code, you need to specify the code package name and template name.
[0054] Define a custom WVO annotation on the class, with the user specifying these three names. If the user does not specify, the annotation will use the default settings.
[0055] The format is as follows:
[0056]
[0057]
[0058] 2.2 TVO Annotation
[0059] When defining the code template, the entity name, table name, and table description are used as variable parameters. Additionally, for the tree generation template, there are variable parameters such as primary key and ParentId.
[0060] The format is as follows:
[0061]
[0062] To support user default settings, annotations have default values. However, Wvo annotations cannot be omitted. They serve as a marker that is scanned and processed by the program.
[0063] 3. Custom annotations on attributes
[0064] Front-end Vue template design: The characteristics of a web page can be abstracted into two pages: one page displays a list, and the second page is for adding or updating items.
[0065] The first page is divided into the following three areas:
[0066] The first part is the query area. If you want a certain attribute field to be displayed in the query area, then enter this information in the annotation.
[0067] The second part is the button area, which includes options such as Add, Import, and Export. These options remain unchanged.
[0068] The third part is the list area, where a list of fields is displayed after the records are retrieved from the database.
[0069] 3.1 PageVo Annotation
[0070] On the list page, the parameters that the user needs to input are: whether to display in the search area and whether to display in the list area;
[0071] On new or updated pages, the following parameters are required from the user:
[0072] Whether this field is displayed on the page;
[0073] This field can be displayed on the page in various forms, such as a text box, drop-down list, or rich text.
[0074] This field indicates read permissions on the page, such as whether it's just for display or allows reading and writing, in the following format:
[0075]
[0076]
[0077] 3.2 @ValidateVo annotation
[0078] The content selected in the field on the page comes from a column in a table in the database.
[0079]
[0080]
[0081] 4. Annotation Analysis
[0082] 4.1 Annotations on parsing classes
[0083] The class is annotated with JPA's `@TableName` annotation, as well as custom annotations `@WVO` and `@TVO`, and these annotations may be expanded in the future. The design employs the chain-of-responsibility pattern.
[0084] Each annotation corresponds to a separate class for parsing. The parsing results are saved in WebModelVo in the following format:
[0085]
[0086] 4.2 Parsing annotations on attributes
[0087] The annotations defined on the attributes include JPA annotations @Column and @ID, as well as custom annotations @PageVo and @ValidateVo. Similar to the parsing method for class annotations, it also adopts the chain of responsibility design pattern, with each annotation being parsed by an independent class. The format is as follows:
[0088]
[0089] 5. Implementation of the startup class
[0090] The startup class inherits the ApplicationRunner interface. Within this interface implementation, a fixed directory is scanned, and the class files of all classes within that directory, including its subdirectories, are parsed using reflection. The parsed results are then written to a WebModelVo object.
[0091] Entry point for startup:
[0092] Startup class inherits from ApplicationRunner
[0093] public class CodeService implements ApplicationRunner{}
[0094] According to Spring Boot's runtime mechanism, the startup class is the final step in the Spring Boot startup process, after the previous startup steps have already been executed. Therefore, it can retrieve the classes and annotations that have already been compiled in the preceding environment.
[0095] 5.1 Obtain the class files in the specified directory;
[0096] 5.2 If empty, return directly. If there is a value, add the obtained class file to a List array for further processing.
[0097] 5.3 Annotation Parsing. The definition of annotations may be expanded in the future. During parsing, a chain of responsibility design pattern is used, employing a plug-in approach to annotate parsing. To facilitate user input, a default implementation is used when no user input is provided.
[0098] Parse JPA annotations and custom annotations, and write the parsed results into the class structure of the domain model WebModelVO, as follows: Figure 4 As shown, WebModelVo corresponds to one main table model TableVoDomain. One TableVoDomain has 0 to n sub-table models (where n>=0) and 0 to m fields (where m>0).
[0099] 5.4 Calling remote services: After assembling the parsed results into an object and serializing it, call the service engine of the code generator.
[0100] 5.5 The specific code is as follows:
[0101]
[0102]
[0103]
[0104] The code generation server is used for:
[0105] The template design includes:
[0106] Server-side template design: The server-side is designed using the common Spring Boot + Mybatis Plus approach; the code structure consists of four layers: Controller, Entity, Mapper, and Service.
[0107] Using Freemarker templates, we designed the template code structure for the front-end Vue, as well as the code structure for the server-side, and used the changing parts as parameters of a Map.
[0108] When generating Freemarker templates, the variable parts are typically stored as a collection of Map objects, while the fixed parts use Freemarker syntax.
[0109] When the program is generated, the changed parts are replaced with actual data and then merged into the Freemarker syntax.
[0110] The code generation server adopts a Spring Boot-based architecture, accepts web requests from clients, parses the received data into WebVO classes, converts the WebVO classes into Map objects, and combines the changed parts of the Map objects with Freemarker templates to generate front-end and back-end code.
[0111] Distribute the generated front-end and back-end code to the deployment system;
[0112] The deployment backend service module is used to deploy the file server, accept files from the code generation server, distribute them to the specified code location, and configure the code configuration menu and default permissions.
[0113] The deployment front-end service module is used to receive front-end files from the code generation server and distribute them to the specified front-end code location.
[0114] Example
[0115] In this embodiment, the system deployment scheme is as follows:
[0116] Set up a local development environment and open the JpaClient client. During development, write the entity class code in the specified directory.
[0117] The code generation server is deployed separately. It accepts requests from clients.
[0118] The front-end and server are deployed separately.
[0119] The backend server is deployed separately.
[0120] On the client side, an inheritance class is used. This class has five properties, which are the five essential fields for developing a data table. The code is shown below:
[0121]
[0122]
[0123] The specific JPA class example code is as follows:
[0124]
[0125]
[0126]
[0127]
[0128] After clicking "Run", the client automatically generates a data table based on the custom business class, parses the annotations on the class to form a JSON file and sends it to the code generation server. The code generator generates the code and sends the generated code to the Java deployment server and the front-end Vue server.
[0129] Then after restarting the server, as follows Figure 2 As shown, the front-end interface observed on the deployment server allows for typical CRUD operations. Figure 3 As shown,
[0130] Users enter classes, attributes, and corresponding annotations in the editor. After execution, a runnable web system is immediately available, significantly improving development efficiency.
[0131] In its specific implementation, this application provides a computer storage medium and a corresponding data processing unit. The computer storage medium is capable of storing a computer program, which, when executed by the data processing unit, can run the invention's content regarding a JPA-based automatic web code generation system and some or all of the steps in its various embodiments. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.
[0132] Those skilled in the art will clearly understand that the technical solutions in the embodiments of the present invention can be implemented using computer programs and their corresponding general-purpose hardware platforms. Based on this understanding, the technical solutions in the embodiments of the present invention, or the parts that contribute to the prior art, can be embodied in the form of computer programs, i.e., software products. These computer program software products can be stored in a storage medium and include several instructions to cause a device containing a data processing unit (which may be a personal computer, server, microcontroller, MUU, or network device, etc.) to execute the methods described in various embodiments or certain parts of the embodiments of the present invention.
[0133] This invention provides a JPA-based automatic web code generation system. Many methods and approaches exist for implementing this technical solution; the above description is merely a preferred embodiment. It should be noted that those skilled in the art can make various improvements and modifications without departing from the principles of this invention, and these improvements and modifications should also be considered within the scope of protection of this invention. All components not explicitly stated in this embodiment can be implemented using existing technologies.
Claims
1. A JPA-based automatic web code generation system, characterized in that, This includes the client, code generation server, backend service deployment module, and frontend service deployment module; The client is used to: automatically generate database tables using JPA annotations; input various variables required for template generation using custom user annotations; combine user-defined attributes and annotations in the class into JSON format and send it to the code generation server, which then parses the JSON and generates a complete set of code in combination with the code template. The code generation server is used to: design front-end and back-end template code using Freemarker templates, and merge the code with the templates to generate a complete set of code based on the parameters passed by the client; The deployment backend service module is used to deploy the application system, receive the files generated by merging according to the template and parameters from the code generation server, distribute them to the specified code location, and configure menus and permissions; The deployment front-end service module is used to receive front-end files from the code generation server and distribute them to the specified front-end code location; When the client starts, it automatically scans for classes with user annotations in the specified directory. When using the client, the user creates a new class in the editor environment and adds various custom attributes to the new class. Annotations include JPA's built-in annotations and user-defined annotations; The annotations provided by JPA include annotations on classes and annotations on properties in the JPA framework. The annotations on classes in the JPA framework specifically include: @Entity: Used to establish the mapping between JPA entities and tables; it is an annotation defined in the Java Persistence API. @Table: If the user needs to define the table name generated in the database separately, the @Table annotation can override the default table name of @Entity; the @Table annotation specifies the database table to be mapped by the Entity, where @Table.name() is used to specify the table name of the mapped table; The annotations on attributes in the JPA framework specifically include: @ID: Used to specify that an attribute is mapped to a database primary key; @Column: Used to specify the data type when defining the database field corresponding to the attribute; User-defined annotations include annotations on classes and annotations on attributes; The annotations on the class specifically include: The @WVo annotation is used to specify the target location, package name, and template type for code generation. @TVo annotation: Used to determine the table description and whether it is a tree structure; The annotations on the attributes specifically include: @PageVo: Used to specify the attribute of a field as it appears on the page when the page is generated; @ValidateTo: Used to specify the conditions to be validated when the page is generated; The client uses the chain of responsibility pattern to parse various annotations, assembles the parsed results into an object, serializes it, and sends it to the code generation server. Then, the startup class inherits from the ApplicationRunner in the Spring Boot container. When the Spring Boot container starts, the client's database tables are automatically generated, and the custom directory is scanned. Annotations are parsed using reflection, and the generated JSON file is sent to the code generation server by calling a remote service. The backend template code is a multi-layered framework based on Spring Boot + Mybatis; the frontend template code is based on the Vue 2.0 framework. The parameters that change in the backend Spring Boot multi-layer structure code program include: whether it is a primary key, package name, entity name, table name, table comment, field name, and field comment; The parameters that change on a Vue front-end page include: the type of the field as it appears on the page, and the type of page controls. Define the parameters that change between the front-end and back-end as JSON data format; The code generation server accepts JSON files generated by clients, deserializes them, and converts the JSON data into key-value pairs for a Map. The Map data is then combined with Freemarker templates, and Freemarker's template engine is used to replace the changes in the Freemarker template with parts of the Map collection to generate front-end and back-end code. Simultaneously, the generated front-end and back-end code is distributed to the deployment system.