Method for automatically creating database table and generating form based on model annotation

Through the method based on model annotation, database tables are automatically created and forms are generated, which solves the problem of inefficient database table creation and form generation in the existing technology, and achieves the purpose of improving developers' work efficiency.

CN119861983APending Publication Date: 2025-04-22GUIZHOU HAOYI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411938484.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-26
Publication Date
2025-04-22

AI Technical Summary

Technical Problem

In the prior art, the creation of database tables and the generation of forms are inefficient, resulting in the limitation of the work efficiency of software developers.

Method used

Through a model annotation-based method, automatically create database tables and generate forms. This method includes configuring the relationship between the model and the database table through class annotation, configuring the correspondence between model attributes and database table fields through attribute annotation, and realizing dynamic creation of database tables and automatic field checking through a unified table creation interface.

Benefits of technology

It realizes automatic creation of database tables, automatic inspection of fields and automatic generation of forms, which improves the work efficiency of developers and allows developers to focus more on business implementation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119861983A_ABST
    Figure CN119861983A_ABST
Patent Text Reader

Abstract

The invention discloses a method for automatically creating a database table and generating a form based on model annotation, which realizes the relationship between a model and the database table through annotation, scans the model when a system is initialized, and loads annotation configuration to realize automatic table creation through the annotation configuration under different databases. If the table division rule exists, whether a table corresponding to the table division rule exists or not needs to be detected, and if the table does not exist, a corresponding sub-table is created in advance; and generating a corresponding form through the layout configuration, the row configuration and the corresponding input type during data entry. According to the method, the database table can be automatically created, the fields can be automatically checked, the form can be automatically generated, and the form data can be automatically verified, so that the aim of improving the development efficiency is fulfilled.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to a method for automatically creating a database table and generating a form based on model annotation, belonging to the technical field of automatic program development. Background Art

[0002] With the continuous development of information technology, the Internet is no longer a small website or portal as it used to be. The systems currently being built are all information-based business systems related to their own businesses. There are hundreds or thousands of database tables involved, and the complexity of the business continues to increase, resulting in low work efficiency for software developers. Summary of the invention

[0003] The technical problem to be solved by the present invention is to provide a method for automatically creating a database table and generating a form based on model annotations, so as to solve the technical problems existing in the above-mentioned prior art.

[0004] The technical solution adopted by the present invention is: a method for automatically creating a database table and generating a form based on model annotation, comprising the following steps:

[0005] a. Configure the relationship between the model and the database table through class annotation A, including the database connection identifier, name, table name, delete flag attribute, table partition type, table partition rule, and initialization field check;

[0006] b. Configure the corresponding relationship between the attributes of the model and the fields in the database table and their corresponding form controls through attribute annotation B, including whether it is a primary key identifier, field name, field name, field length, field precision, field default value, whether it can be empty, input control type, validation rules, validation regular expressions, and display style;

[0007] c. Configure the form layout properties through attribute annotation C, including title, number of display columns, icons, and operation buttons;

[0008] d. Configure whether the form input controls are in the same row through attribute annotation D;

[0009] e. Define a unified interface E for creating a table. The interface defines methods for checking whether a table exists in the database, including methods for creating a table according to annotations, methods for checking whether a field exists, methods for adding a new field to a table, and methods for deleting a field.

[0010] f. Implement different classes for different databases according to the method defined by E, so that you can detect whether a table exists in different databases, and automatically create a table if it does not exist. If a table exists, you can check whether the field corresponding to the model attribute exists, and automatically create the field if it does not exist.

[0011] g. Scan the entity model class when the system is initialized, and create an implementation instance F of the interface E of the corresponding database according to the database connection identifier configured by the annotation A of the model class;

[0012] h. Use F to check whether the table corresponding to the model exists. If the table does not exist, call F's table creation method and field addition method to dynamically create the corresponding table. If the table exists, check whether the field corresponding to the model attribute exists in the table. If not, automatically create the corresponding field to complete the binding of the database table and the model and the creation of the table.

[0013] i. In the process of generating a table, it is necessary to determine whether A needs to be divided into tables. If so, the corresponding table is generated according to the corresponding rules. The table division rules include generation by day, generation by month, and generation by year;

[0014] j. In the case of splitting tables, you need to create tables with corresponding rules in advance. If you split the table by day, you will automatically create tables for the latest 10 days; if you split the table by month, you will automatically create tables for the latest two months; if you split the table by year, you will automatically create tables for the latest two years. At the same time, add split table check to the system tasks to detect and automatically create tables in advance according to the rules;

[0015] k. Create a form layout control G according to the attribute annotation C. The layout control includes a title, an icon, and an operation button;

[0016] l. Create corresponding form input controls according to the order of model attributes. H. Common input types include hidden fields, text input boxes, multi-line text, drop-down selections, radio buttons, check boxes, time selections, date selections, image uploads, file uploads, drop-down trees, drop-down table selections, and rich text editors;

[0017] m. During the process of creating form controls, it is necessary to determine whether the current attribute has annotation D. If annotation D exists, it is necessary to create a form row control I according to the number of columns defined in C; add the created input control H to the row control I until the corresponding number of columns in C is reached to complete the creation of the current row; if annotation D does not exist, create a row control J according to 1 column and add H to J at the same time;

[0018] n. Add the created row controls I and J to the layout component G to complete the form generation operation.

[0019] Preferably, after the form is created, when the user enters data, the validity of the input data needs to be verified according to the validation rules in B. The verification includes English letter verification, number verification, length verification, phone number verification, email verification, mobile phone number verification, postal code verification and custom positive expression verification.

[0020] Preferably, after the user submits the data, before saving the data, the input data needs to be verified again according to the verification rules in annotation B, so as to achieve double verification before submission and before saving to ensure the validity of the data.

[0021] Preferably, when the user edits the data, the data needs to be retrieved and then assigned to the corresponding form control.

[0022] Preferably, when data is deleted by the user, the deletion flag in annotation A needs to be checked. If the deletion criteria exist, logical deletion is performed according to the deletion criteria, otherwise physical deletion is performed.

[0023] Beneficial effects of the present invention: Compared with the prior art, the present invention can automatically create database tables, automatically check fields, automatically generate forms, and automatically verify form data, thereby achieving the purpose of improving the development efficiency of staff and allowing developers to have more energy to focus on business implementation. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] Figure 1 It is a flow chart of a method for automatically creating a database table and generating a form based on model annotations of the present invention.

[0025] Figure 2 It is a form page diagram automatically generated in an embodiment of the present invention. DETAILED DESCRIPTION

[0026] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments.

[0027] Embodiment 1:

[0028] A method for automatically creating a database table and generating a form based on a model annotation comprises the following steps:

[0029] a. Configure the relationship between the model and the database table through class annotation A, including the database connection identifier, name, table name, delete flag attribute, table partition type, table partition rule, and initialization field check;

[0030] b. Configure the corresponding relationship between the attributes of the model and the fields in the database table and their corresponding form controls through attribute annotation B, including whether it is a primary key identifier, field name, field name, field length, field precision, field default value, whether it can be empty, input control type, validation rules, validation regular expressions, and display style;

[0031] c. Configure the form layout properties through attribute annotation C, including title, number of display columns, icons, and operation buttons;

[0032] d. Configure whether the form input controls are in the same row through attribute annotation D;

[0033] e. Define a unified interface E for creating a table. The interface defines methods for checking whether a table exists in the database, including methods for creating a table according to annotations, methods for checking whether a field exists, methods for adding a new field to a table, and methods for deleting a field.

[0034] f. Implement different classes according to the method defined by E for different databases (e.g. MSQL, MYSQL, ORACLE, etc.), so that it can detect whether a table exists in different databases, and automatically create a table if it does not exist. If a table exists, it can check whether the field corresponding to the model attribute exists, and automatically create the field if it does not exist.

[0035] g. Scan the entity model class when the system is initialized, and create an implementation instance F of the interface E of the corresponding database according to the database connection identifier configured by the annotation A of the model class;

[0036] h. Use F to check whether the table corresponding to the model exists. If the table does not exist, call F's table creation method and field addition method to dynamically create the corresponding table. If the table exists, check whether the field corresponding to the model attribute exists in the table. If not, automatically create the corresponding field to complete the binding of the database table and the model and the creation of the table.

[0037] i. In the process of generating a table, it is necessary to determine whether A needs to be divided into tables. If so, the corresponding table is generated according to the corresponding rules. The table division rules include generation by day, generation by month, and generation by year;

[0038] j. In the case of splitting tables, you need to create tables with corresponding rules in advance. If you split the table by day, you will automatically create tables for the latest 10 days; if you split the table by month, you will automatically create tables for the latest two months; if you split the table by year, you will automatically create tables for the latest two years. At the same time, add split table check to the system tasks to detect and automatically create tables in advance according to the rules;

[0039] k. Create a form layout control G according to the attribute annotation C. The layout control includes a title, an icon, and an operation button;

[0040] l. Create corresponding form input controls according to the order of model attributes. H. Common input types include hidden fields, text input boxes, multi-line text, drop-down selections, radio buttons, check boxes, time selections, date selections, image uploads, file uploads, drop-down trees, drop-down table selections, and rich text editors;

[0041] m. During the process of creating form controls, it is necessary to determine whether the current attribute has annotation D. If annotation D exists, it is necessary to create a form row control I according to the number of columns defined in C; add the created input control H to the row control I until the corresponding number of columns in C is reached to complete the creation of the current row; if annotation D does not exist, create a row control J according to 1 column and add H to J at the same time;

[0042] n. Add the created row controls I and J to the layout component G to complete the form generation operation.

[0043] Preferably, after the form is created, when the user enters data, the validity of the input data needs to be verified according to the validation rules in B. The verification includes English letter verification, number verification, length verification, phone number verification, email verification, mobile phone number verification, postal code verification and custom positive expression verification.

[0044] Preferably, after the user submits the data, before saving the data, the input data needs to be verified again according to the verification rules in annotation B, so as to achieve double verification before submission and before saving to ensure the validity of the data.

[0045] Preferably, when the user edits the data, the data needs to be retrieved and then assigned to the corresponding form control.

[0046] Preferably, when data is deleted by the user, the deletion flag in annotation A needs to be checked. If the deletion criteria exist, logical deletion is performed according to the deletion criteria, otherwise physical deletion is performed.

[0047] Here’s how it works:

[0048] According to the definition, the corresponding database connection identifier, database table name, name, deletion identifier, table partitioning rules, etc. are set in the model annotation A, and the primary key identifier, field name, field name, field length, field precision, field default value, whether it can be empty, validation rules, input type, etc. of the corresponding database are set in the annotation B. Then, the model is scanned when the system is initialized, and different database instances are created through the database connection identifier defined in A to automatically generate the corresponding database table, and check whether the newly added field exists. If it does not exist, it is created. When there is a sub-table, it is also necessary to create a table for the most recent time period related to the sub-table rule, and add a system task to check the sub-table and automatically create the table in advance. In the process of generating the form, the form layout control is created according to the annotation C, the row control is created according to the annotation D, and the corresponding input control is automatically created according to the input type defined in B. At the same time, the created input control is added to the row control, and then the row control is added to the form layout control to complete the generation of the form. After the user enters the data, the data is validated before submission and before the back-end is saved according to the validation rules defined in B to prevent the generation of dirty data. The above steps can be used to automatically create database tables, automatically check fields, automatically generate forms, and automatically verify form data, thereby improving development efficiency and allowing developers to focus more on business implementation.

[0049] For example, in Java as the development language, the following model is annotated:

[0050] OTable corresponds to annotation A

[0051] OField corresponds to annotation B

[0052] HTab corresponds to annotation C

[0053] HCol corresponding annotation D

[0054] The following is an annotation of a Java model

[0055] @OTable(TableName="TG_HD_GOODS",

[0056] CName="Group Purchase Product",DbConn="EShopDb",AutoCreate=

[0057] true,checkFiled="xzUser")

[0058] public class TGGoodsOrm{

[0059] @OField(IsKey=true,Name="ID",IType=

[0060] FiledInputType.Hidden, DefValue = "{ulid}", Length = 50)

[0061] public String tgid;

[0062] @HTab(showNum = 2)

[0063] @OField(Name = "团购名称", IsNull = false, Length = 100, IType

[0064] = FiledInputType.TextBox)

[0065] public String tgName;

[0066] @OField(Name = "所属专题", IsNull = false, Length = 100, IType

[0067] = FiledInputType.SelectComBox, ItemStr =

[0068] "orm = eshop.orm.hd.tg.TGGoodsCla&dbconn = EShopDb&where = sid = {sys.sid}and isfb = 1")

[0069] public String ztId;

[0070] @OField(Name = "专题标题", Length = 500)

[0071] public String ztName;

[0072] @HCol

[0073] @OField(Name = "产品名称", IType =

[0074] FiledInputType.GridSelect, IsNull = false, IsSearch = true)

[0075] public String gName;

[0076] @HCol

[0077] @OField(Name = "object or specification", Length = 300, IsSearch = true, IType = FieldInputType.GridSelect, IsNull = false)

[0078] public String bindName;

[0079] @HCol

[0080] @OField(Name="Group purchase times",IType=

[0081] FiledInputType.IntInput,Length=10,DefValue="",InAlert="After how many successful group purchases, the group purchase ends. 0 is unlimited")

[0082] public int tgNum;

[0083] @HCol

[0084] @OField(Name="Number of groups formed",Length=10,DefValue="0")

[0085] public int yctNum;

[0086] @HCol

[0087] @OField(Name="Number of people in the group",IType=

[0088] FiledInputType.IntInput,Length=10,DefValue="0",IsNull=false)

[0089] public int tgOrderNum;

[0090] @HCol

[0091] @OField(Name="Limit Quantity",IType=

[0092] FiledInputType.IntInput,Length=10,DefValue="1",IsNull=false)

[0093] public int xzNum;

[0094] @HCol

[0095] @OField(Name="Group formation time (minutes)",IType=

[0096] FiledInputType.IntInput,InAlert = "How many minutes after the group starts will it end", IsNull = false)

[0097] public int ctMut;

[0098] @HCol

[0099] @OField(Name="Start Time",IType=

[0100] FiledInputType.DateTime,Precision=2,IsNull=false)

[0101] public Date startTime;

[0102] @HCol

[0103] @OField(Name="End Time",IType=

[0104] FiledInputType.DateTime,Precision=2,IsNull=false)

[0105] public Date endTime;

[0106] @OField(Name = "Limit one order per person", IType =

[0107] FiledInputType.ISNO,DefValue="1")

[0108] public boolean xzUser;

[0109] @UpLoadConfig(url=" / upload / submit.do?maxWidth=420")

[0110] @OField(Name="Cover Image",IType=

[0111] FiledInputType.UpImage,Length=500)

[0112] public String fmPht;

[0113] @HCol

[0114] @OField(Name="Market Price",IType=

[0115] FiledInputType.IntInput,Length=10,Precision=2)

[0116] public double scPrice;

[0117] @HCol

[0118] @OField(Name="Direct Purchase Price",IType=

[0119] FiledInputType.IntInput,Length=10,Precision=2,EditReadOnlay=

[0120] true)

[0121] public double gPrice;

[0122] @OField(Name="Group purchase price",IType=

[0123] FiledInputType.IntInput,Length=10,Precision=2)

[0124] public double tPrice;

[0125] @OField(Name="Group Purchase Instructions",Length=3600,IType=

[0126] FieldedInputType.TextArea)

[0127] public String content;

[0128] }

[0129] The above model TGGoodsOrm uses OTable to associate with the table in the database

[0130] TG_HD_GOODS, TGGoodsOrm's attributes tgid, tgName, ztId, ztName, gName, etc. correspond to the fields of TG_HD_GOODS. The system automatically checks the table and creates it during initialization. The form is automatically generated when adding or editing.

[0131] The form interface is as follows Figure 2 shown.

[0132] The above description is only a specific implementation mode of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.

Claims

1. A method for automatically creating a database table and generating a form based on model annotations, characterized in that: The following steps are involved: a. Configure the relationship between the model and the database table through class annotation A, including the database connection identifier, name, table name, delete flag attribute, table partition type, table partition rule, and initialization field check; b. Configure the corresponding relationship between the attributes of the model and the fields in the database table and their corresponding form controls through attribute annotation B, including whether it is a primary key identifier, field name, field name, field length, field precision, field default value, whether it can be empty, input control type, validation rules, validation regular expressions, and display style; c. Configure the form layout properties through attribute annotation C, including title, number of display columns, icons, and operation buttons; d. Configure whether the form input controls are in the same row through attribute annotation D; e. Define a unified interface E for creating a table. The interface defines methods for checking whether a table exists in the database, including methods for creating a table according to annotations, methods for checking whether a field exists, methods for adding a new field to a table, and methods for deleting a field. f. Implement different classes for different databases according to the method defined by E, so that you can detect whether a table exists in different databases, and automatically create a table if it does not exist. If a table exists, you can check whether the field corresponding to the model attribute exists, and automatically create the field if it does not exist. g. Scan the entity model class when the system is initialized, and create an implementation instance F of the interface E of the corresponding database according to the database connection identifier configured by the annotation A of the model class; h. Use F to check whether the table corresponding to the model exists. If the table does not exist, call F's table creation method and field addition method to dynamically create the corresponding table. If the table exists, check whether the field corresponding to the model attribute exists in the table. If not, automatically create the corresponding field to complete the binding of the database table and the model and the creation of the table. i. In the process of generating a table, it is necessary to determine whether A needs to be divided into tables. If so, the corresponding table is generated according to the corresponding rules. The table division rules include generation by day, generation by month, and generation by year; j. In the case of splitting tables, you need to create tables with corresponding rules in advance. If you split the table by day, you will automatically create tables for the latest 10 days; if you split the table by month, you will automatically create tables for the latest two months; if you split the table by year, you will automatically create tables for the latest two years. At the same time, add split table check to the system tasks to detect and automatically create tables in advance according to the rules; k. Create a form layout control G according to the attribute annotation C. The layout control includes a title, an icon, and an operation button; l. Create corresponding form input controls according to the order of model attributes. H. Common input types include hidden fields, text input boxes, multi-line text, drop-down selections, radio buttons, check boxes, time selections, date selections, image uploads, file uploads, drop-down trees, drop-down table selections, and rich text editors; m. During the process of creating form controls, it is necessary to determine whether the current attribute has annotation D. If annotation D exists, it is necessary to create a form row control I according to the number of columns defined in C; add the created input control H to the row control I until the corresponding number of columns in C is reached, completing the creation of the current row; If annotation D does not exist, create row control J by 1 column and add H to J; n. Add the created row controls I and J to the layout component G to complete the form generation operation.

2. A method for automatically creating a database table and generating a form based on model annotation according to claim 1, characterized in that: After the form is created, when the user enters data, the validity of the input data needs to be verified according to the validation rules in B. The verification includes English letter verification, number verification, length verification, phone number verification, email verification, mobile phone number verification, postal code verification and custom expression verification.

3. A method for automatically creating a database table and generating a form based on model annotation according to claim 2, characterized in that: After the user submits the data, the input data needs to be verified again according to the validation rules in annotation B before it is saved, so as to ensure the validity of the data by double verification before submission and before saving.

4. The method for automatically creating a database table and generating a form based on model annotation according to claim 1, characterized in that: When the user edits the data, the data needs to be retrieved and assigned to the corresponding form control.

5. The method for automatically creating a database table and generating a form based on model annotation according to claim 1, characterized in that: When data is deleted by the user, it is necessary to check the deletion flag in annotation A. If the deletion criteria exist, logical deletion is performed according to the deletion criteria, otherwise physical deletion is performed.