A method and device for generating multi-language program templates based on data model
By defining environment variables and project structure templates, multi-language program templates are generated, which solves the problems of single source code generation and operational complexity on low-code platforms, achieves multi-language support and flexible website customization, and improves development efficiency.
Patent Information
- Application Number
- CN202410271018.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-11
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2044-03-11
AI Technical Summary
The source code generated by the existing low-code platform is single, and it only supports HTML static pages, h5 and json. It cannot generate APP languages such as vue, iOS, Android, flutter, reactnative, etc., making user operations complicated and unable to freely customize the website.
By defining environment variables, determining project structure templates and project interface paths, we can generate multi-language program templates based on data models, support batch generation of technical documents such as markdown and csv, and achieve flexible writing and multi-language support for template files.
It improves the efficiency and flexibility of low-code platform generation of source code and supports multi-language development. Users can write template files according to actual needs to achieve richer website customization.
Smart Images

Figure CN118170356B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of low-code platforms, and in particular to a method and device for generating multi-language program templates based on a data model. Background Art
[0002] Low-code platforms are mainly used for the development of enterprise applications. They are easy to use, highly customizable, rich in functionality, and powerfully extensible, and are favored by many enterprises and developers. However, most low-code platforms currently generate a single source code that only supports HTML static pages, H5, and JSON, and does not support the generation of various APP languages such as Vue, iOS, Android, Flutter, and ReactNative. In addition, users need to add pages and fields one by one to build a complete website. The operation is complicated and the website building is slow. It can only be developed on the limited templates of the platform, and the website cannot be customized freely. Summary of the invention
[0003] The purpose of the present invention is to provide a multi-language program template generation method and device based on a data model, which can improve the efficiency and flexibility of generating source code on a low-code platform.
[0004] To achieve the above object, the present invention provides a method for generating a multi-language program template based on a data model, comprising the following steps.
[0005] Define environment variables; the environment variables include global variables and local variables.
[0006] A project structure template is determined based on the environment variable; the project structure template is used to construct an output path of a template file.
[0007] The project interface path is determined based on the environment variable; the project interface path is used to construct an access interface for a code file corresponding to a data model; and the data model is a database constructed in advance according to project requirements.
[0008] Based on the environment variables, the project structure template and the project interface path, a template file for implementing project functions is written to obtain a multi-language program template.
[0009] To achieve the above objectives, the present invention provides a computer device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the above-mentioned method for generating a multi-language program template based on a data model.
[0010] According to the specific embodiment provided by the present invention, the present invention discloses the following technical effects: the present invention first defines environment variables, determines the project structure template and the project interface path, so as to determine the output path of the template file and the access interface of the code file corresponding to the data model, and then writes a template file for implementing the project function based on the environment variables, the project structure template and the project interface path to obtain a multi-language program template. Users can write template files according to actual needs, thereby improving the efficiency and flexibility of the low-code platform in generating source code. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. 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 paying creative labor.
[0012] Figure 1 A flowchart of a method for generating multi-language program templates based on a data model provided by the present invention.
[0013] Figure 2 Write a flowchart for the template file of the new function.
[0014] Figure 3 This is a schematic diagram of the basic layer and business layer in the template file.
[0015] Figure 4 Write a flowchart for the query function's template file.
[0016] Figure 5 Write a flowchart for the template file that modifies the function.
[0017] Figure 6 Write a flowchart for the template file that imports the function. DETAILED DESCRIPTION
[0018] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments 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.
[0019] The purpose of the present invention is to provide a method and device for generating multi-language program templates based on data models. By writing business system templates, data models are converted into project engineering templates with business value. After a set of templates is built, it can be applied to projects with the same development language, and users can freely modify the interface on the existing templates to achieve the desired effect. The present invention also supports batch generation of technical documents such as markdown and csv, which is more universal.
[0020] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0021] like Figure 1 As shown, the method for generating a multi-language program template based on a data model provided by the present invention includes steps 100 to 400.
[0022] Step 100: Define environment variables. When writing a template, you can use environment variables to fill in corresponding data. The Java program fills the corresponding data model into the corresponding variable name and passes it to the template.
[0023] Environment variables include global variables and local variables. Global variables include the Chinese name of the project, package name, interface path, service port number, database name, and package name path. Local variables include the data model name, table name, and field name. The information of environment variables is shown in Table 1.
[0024] Table 1 is the information table of environment variables.
[0025]
[0026]
[0027]
[0028] All data models have six fixed fields, which are used to manage data. The fields of the data model are shown in Table 2.
[0029] Table 2 Field table of the data model.
[0030] Variable Name type illustrate create_user_id Integer Create a user id create_time date Creation time update_user_id Integer Modify user id update_time date Modification time is_del Boolean Delete
[0031] Step 200: Determine a project structure template based on the environment variable. The project structure template is used to construct an output path of a template file.
[0032] The rules of the project structure template include the following points.
[0033] A) Template file paths are relative to the project template root directory.
[0034] B) The template file path contains global variable names, which are replaced by the values corresponding to the variable names.
[0035] C) The template file path contains local variable names, and the number of local variables is the same as the number of data models, and the local variable names in the path are replaced by the values corresponding to the variable names.
[0036] D) The file xxx.ftl ending with .ftl in the project structure template needs to be rendered by freemarker and output to a xxx file without .ftl.
[0037] E) Files in the project structure template that do not end with .ftl are directly copied to the target file according to the path after the variable is filled.
[0038] For example, the global variable ${packagePath} is io / proinsight / a. Local variables: ${tableName} is the corresponding data model adminUser and roleMenu; ${table_name} is the corresponding data model admin_user and role_menu. The path after processing by the project structure template is shown in Table 3.
[0039] Table 3 Paths after processing by the project structure template.
[0040]
[0041] Step 300: Determine a project interface path based on the environment variable. The project interface path is used to construct an access interface for a code file corresponding to a data model. The data model is a database pre-built according to project requirements.
[0042] For example, the global variable ${serverPort} is 8080, and ${baseApiPath} is / aApi. The local variable ${tableName} is the corresponding data model adminUser and roleMenu, and ${table_name} is the corresponding data model admin_user and role_menu.
[0043] After replacing the variables with corresponding values, the server path access address changes from "http: / / localhost:${serverPort} / ${baseApiPath} / " to "http: / / localhost:8080 / aApi / ".
[0044] In the present invention, the template pre-defined related interfaces of adding, deleting, modifying, querying, importing and exporting are shown in Table 4.
[0045] Table 4 Template predefined add, delete, modify, query, import and export related interfaces.
[0046]
[0047]
[0048] Step 400: Based on the environment variables, the project structure template and the project interface path, a template file for implementing the project function is written to obtain a multi-language program template. The present invention writes a multi-language program template for each data model. The FreeMarker template language is used to write the multi-language program template.
[0049] The principle of multi-language program template is as follows: hello.tpl language program template file is written in the template language of the template engine, and its content is "Hello${name}". Among them, ${name} is variable content. Java fills the data model such as: cfg.getTemplate("hello.java.ftl").process(new HashMap(){{this.put("name", "Zhang San");}},out). When the data variable is substituted into Java, the template rendering engine replaces the corresponding position according to the variable name and finally outputs the code hello.java, whose content is "Hello Zhang San".
[0050] The project functions in the present invention include adding function, deleting function, modifying function, querying function, exporting function and importing function.
[0051] (1) For new functions: the template file includes a front-end template file for new functions and a back-end template file for new functions. The template file writing process for new functions is as follows: Figure 2 shown.
[0052] The template file of a single data model mainly extracts the field information of the data model from the tableBean variable to build the form entry page. Because the fixed fields are system-controlled fields and do not need to be filled in by users, the loop can input fields ("columnsForInput" local variable), and its field information is shown in Table 5.
[0053] Table 5 Input fields.
[0054]
[0055]
[0056] Step 400 specifically includes steps 410 to 416 .
[0057] Step 410: Based on the environment variables, the project structure template and the project interface path, a new function front-end template file and a new function back-end template file are established.
[0058] Step 411: For any data model, extract the field information of all fields in the data model, including field name, data type, parent table associated field, newly added components and verification rules.
[0059] That is, loop through multiple fields in columnsForInput (no primary key by default, which is generated by the backend). Generally, the field name corresponding to "column_comment" is displayed on the left side of the input box, and a red asterisk is displayed when "required" is "true".
[0060] Step 412: For any field in the data model, determine whether the field has a parent table based on the parent table associated field of the field. If the field has a parent table, construct a drop-down list box for loading the parent table in the new function front-end template file. If the field does not have a parent table, determine whether there is a custom component based on the new component. If there is a custom component, construct the custom component in the new function front-end template file. If there is no custom component, construct a radio button, input box or date selector in the new function front-end template file based on the data type of the field.
[0061] When displaying the field input area, the first thing to consider is the numerical association between tables. If there is an association, a drop-down box is generally used for selection. The drop-down box data is obtained from the ${columnBean.parentColumn.table_name} / getDictList.do interface of the parent table. This interface is called to obtain the drop-down box data when the page is displayed.
[0062] If it is not an associated field, first use "add_component" to determine whether there is a user-defined input component. If so, use the label name specified by "add_component" to output it.
[0063] If it is not an associated field and there is no custom input component, the type of the input box can be determined by the data type of the field. The default is string input. If it is a number type, it is entered in the form of a sequence number; if it is a date type, a date selector is displayed.
[0064] Step 413: Construct form validation rules in the new function front-end template file according to the validation rules of the fields.
[0065] Determine whether verifys has field verification rules. If so, fill them into the form verification rules. When the input box loses focus, the form verification rules will be called for verification.
[0066] Step 414: construct a duplicate name verification function and a form submission function in the new function front-end template file. The duplicate name verification function is used to call the duplicate data verification interface in the new function back-end template file. The form submission function is used to call the insert data interface in the new function back-end template file.
[0067] When the front-end displays the interface, you can build a duplicate name verification function to call the background duplicate data verification interface (${tableBean.camelName} / exists.do), and pass the field value with is_name as true to the background to verify whether it is a duplicate name.
[0068] Build a form submission function to call the background data insertion interface (such as ${tableBean.camelName} / insert.do), write the data into the data model, and call the function when the user clicks submit to complete the new function.
[0069] Step 415: Construct a basic layer and a business layer in the new function backend template file. The basic layer includes new function classes. The business layer inherits the basic layer, and the business layer is used to cover or extend the new function classes in the basic layer. The basic layer is generated by the low-code platform, and the business layer is customized and developed by the developer. The purpose of designing two layers is to facilitate code generation through the low-code platform without affecting secondary development.
[0070] Taking the springboot architecture as an example, it is divided into the interface layer, service layer, data persistence layer and data model layer. Controller is responsible for the interface, Service is responsible for the service, Dao is responsible for the database instantiation, such as Figure 3 shown.
[0071] For example, a class ComanyBaseController in a certain interface layer has two interfaces, find and getDictList, and the codes of these two interfaces are generated and output through templates. A class CompanyController in another interface layer inherits ComanyBaseController, and an empty class is generated to inherit ComanyBaseController for the first time. Users can add their own methods such as findByName to this class, or rewrite the interface methods of the parent class, and the code added by users will not be affected. And because CompanyController inherits ComanyBaseController, it can use all methods of the parent class.
[0072] Similarly, the service layer inherits all properties and functions in BaseService and can add or override functions. BaseController references all functions in Service. When the user modifies Service, it will take effect in both BaseController and Controller.
[0073] Similarly, the data persistence layer inherits all properties and functions in BaseDao and can add or override functions. All functions in Dao are referenced in BaseService. When users modify Dao, it will take effect in BaseService and Service, BaseController and Controller at the same time.
[0074] Similarly, in the data model layer, Model inherits all properties and functions in BaseModel and can add or override functions. All functions in Model are referenced in BaseDao. When users modify Model, it will take effect in BaseDao and Dao, BaseService and Service, BaseController and Controller at the same time.
[0075] Step 416: Construct a duplicate data verification interface and a data insertion interface in the business layer of the newly added function backend template file to write the data received by the frontend page into the data model.
[0076] Specifically, the verification interface for duplicate data (such as ${tableBean.camelName} / exists.do) traverses all field information in the local variable tableBean.columns at the BaseController layer, finds the name field with is_name set to true as the input parameter, and receives the name to be verified from the front end. At the BaseService layer, after receiving the parameter, the BaseDao layer is directly called, and the BaseDao layer splices SQL to query whether the value corresponding to the name field exists in the table.
[0077] Insert data interface (such as ${tableBean.camelName} / insert.do) to generate Model class from data model, read columns in tableBean, traverse all fields to generate ${tableBean.PascalName}BaseModel, which mainly includes code comments (field name, author, creation time), annotations for generating interface documents (field name, table name, field type), Excel import and export annotations (matching column number, field English name), field validation rules (traverse all validation rules in the verifys field), field data type, field name and field default value.
[0078] In the BaseController layer, put ${tableBean.pascalName}Model in the receiving parameter, and the form data passed in by the front end will be automatically filled in the Model. First, check the corresponding fields in the passed-in Model. The verification method can be verified through the Model's annotations, or all the verification rules can be obtained by traversing the verifys of each field. Generate verification codes based on values and strings respectively (values only need to add size judgment, and strings need to add regular expression verification). If the verification fails, an error message will be returned. After the verification succeeds, the Model will be passed to the Service for the next business processing. After the service layer implements the duplicate name verification of the data, due to security reasons, the newly added interface cannot receive the primary key passed in by the front end, and only the primary key can be generated by the back end. Therefore, it is determined whether the primary key is auto-incremental. If it is an auto-incremental primary key, the Model can be directly inserted into the database. If it is not an auto-incremental primary key, you can use uuid or snowflake algorithm to generate a unique string, set the value of the primary key field in the Model, and then insert it into the database. Write the current time to create_time in the Model and the login user id to create_user_id. In the database persistence layer, by looping through the columns in tableBean, traversing all the fields and concatenating them, we get the SQL statement: insert into table_name(<field name 1>,<field name 2>....)value(<field value 1>,<field value 2>....), and then substitute the Model through the code and execute the SQL statement.
[0079] (2) For the query function: the template file includes a query function front-end template file and a query function back-end template file. The query function template file writing process is as follows: Figure 4 shown.
[0080] The template file of a single data model mainly extracts the configured list field information that can be displayed from the tableBean variable, columnsForList, to build query conditions and list headers. When the list page is loaded, a page of data is loaded through the interface and filled into the list for display.
[0081] Enter the field variables ("columnsForList" local variable) in a loop, and its field information is shown in Table 6.
[0082] Table 6 Input field variables.
[0083]
[0084]
[0085] Step 400 specifically includes steps 420 to 426 .
[0086] Step 420: Based on the environment variables, the project structure template and the project interface path, a query function front-end template file and a query function back-end template file are established.
[0087] Step 421: For any data model, extract the field information of all fields in the data model, including field name, data type, parent table associated field, verification rule and viewing component.
[0088] Specifically, loop through multiple fields in columnsForList (excluding primary key by default) and output the query condition area respectively. Generally, the field name corresponding to column_comment is displayed on the left side of the input box.
[0089] Step 422: For any field in the data model, determine whether the field has a parent table based on the parent table associated field of the field. If the field has a parent table, construct a drop-down list box for loading the parent table in the query function front-end template file. If the field does not have a parent table, determine whether a custom display component exists based on the view component. If a custom display component exists, construct the custom display component in the query function front-end template file. If no custom display component exists, construct a radio button, input box or date selector in the query function front-end template file based on the data type of the field to construct query conditions on the front-end page.
[0090] Step 423: for any field in the data model, determine whether the field has a parent table; if the field has a parent table, construct a label for displaying the parent table name in the query function front-end template file. If the field does not have a parent table and the field has a custom display component, construct a custom display component in the query function front-end template file to construct a list header on the front-end page.
[0091] When displaying the field search box, the first thing to consider is the numerical association between tables. If there is an association, a drop-down box is generally used for selection. The drop-down box data is obtained from the ${columnBean.parentColumn.table_name} / getDictList.do interface of the parent table. This interface is called to obtain the drop-down box data when the page is displayed.
[0092] If it is not an associated field, the input box type can be determined by the field type. The default is string input. If it is a number type, it is entered as a sequence number. If it is a date type, a date range selector is displayed.
[0093] Loop through multiple fields in columnsForList (excluding primary keys by default) and output each column of the list. Generally, the field name corresponding to column_comment is displayed on the table header, and the English name of the sql_name field is used as the table header name to match the returned data. The view_component variable is used to determine whether there is a custom display component. If so, the label corresponding to view_component is displayed.
[0094] An operation column is added to the list to provide an entry to the modification page. The URL of the entry link is: ' / ${tableBean.camelName} / edit? id='+row.${tableBean.primaryColumn.sqlName}, where tableBean.primaryColumn.sqlName is the primary key name. The corresponding primary key value is obtained through row.primary key name, and the user clicks it to transfer to the modification page.
[0095] Get the table name of the child table and build the entry to the associated table. The URL of the entry link is: ' / ${childTableBean.camelName}? ${relation.from_column_name}='+row.${tableBean.primaryColumn.sqlName}. Among them, childTableBean and relation are the obtained child table definition and the associated relationship. The name of the associated table is obtained from the child table definition. relation.from_column_name indicates the primary key name of this table, followed by the primary key value.
[0096] Step 424: Construct a query paging function in the query function front-end template file. The query paging function is used to call the query data interface (such as ${tableBean.camelName} / find.do) in the query function back-end template file, pass the value of the search condition, page number, and number of rows displayed per page to the back-end for query, and fill it into the list after the query is successful.
[0097] Step 425: construct a basic layer and a business layer in the query function backend template file. The basic layer includes a query function class. The business layer inherits the basic layer, and the business layer is used to cover or extend the query function class in the basic layer.
[0098] Step 426: construct a query data interface in the business layer of the query function backend template file to query the fields in the data model according to the query conditions received by the frontend page, and fill the query results into the list.
[0099] The query data interface (such as ${tableBean.camelName} / find.do) generates a params class from the data model, reads the columns in the tableBean, traverses all the fields, and generates ${tableBean.PascalName}Params, which mainly includes code comments (field name, author, creation time), annotations for generating interface documents (field name, table name, field type), field data type, field name, and field default value.
[0100] In the BaseController layer, put ${tableBean.pascalName}Params into the receiving parameters, and the form data passed in by the front end will be automatically filled into params, and params will be passed to the Service for the next business processing. In the service layer, pass params to the Dao layer. In the database persistence layer, loop through all the fields in the columns of the tableBean to determine whether each field has a value in params. If so, concatenate the query conditions into the query statement to get the SQL statement. Then substitute params through the code and execute the SQL statement.
[0101] (3) Regarding the deletion function: the template file also includes a deletion function front-end template file and a deletion function back-end template file.
[0102] The template file of a single data model mainly extracts the primary key field information of the data model from the tableBean variable to build a form, and adds a separate column of check boxes to the query page.
[0103] Step 400 specifically includes steps 430 to 434 .
[0104] Step 430: Based on the environment variables, the project structure template and the project interface path, a deletion function front-end template file and a deletion function back-end template file are established.
[0105] Step 431: copy the content in the query function front-end template file to the deletion function front-end template file, and construct a check box corresponding to the primary key field in the data model in the deletion function front-end template file.
[0106] Step 432: construct a primary key deletion function in the deletion function front-end template file. The primary key deletion function is used to call the batch deletion interface in the deletion function back-end template file, submit multiple values corresponding to the primary key to the back-end for deletion, and refresh the list after the query is successful.
[0107] Step 433: construct a basic layer and a business layer in the deletion function backend template file. The basic layer includes a deletion function class. The business layer inherits the basic layer, and the business layer is used to cover or extend the deletion function class in the basic layer.
[0108] Step 434: construct a batch deletion interface in the business layer of the deletion function backend template file to delete the data in the data model according to the primary key field received by the frontend page.
[0109] Batch deletion interface (such as ${tableBean.camelName} / delete.do), in the BaseController layer, the primary key field ${tableBean.primaryColumnName}s is used as the input parameter of get.do, and the data id to be deleted is received from the front end. In the BaseService layer, after receiving the parameters, the baseDao layer is directly called. In the BaseDao layer, the related data is first deleted, the related sub-table is checked, and the Dao layer deletion method of the sub-table is called. If the sub-table has a grandchild table, it is also deleted. After all related tables are deleted, the deletion SQL statement is spliced, and the value corresponding to the primary key field is queried to perform logical deletion. The SQL statement template is update${tableBean.sqlName}set is_del=1,update_user_id=? where${tableBean.primaryColumn.sqlName}=?.
[0110] (4) For the modification function: the template file includes a modification function front-end template file and a modification function back-end template file. The template file writing process for the modification function is as follows: Figure 5 shown.
[0111] The template file of a single data model mainly extracts the field information of the data model from the tableBean variable to build a form entry page. The form entry page receives the primary key value passed in by the list, and after the page is displayed, the data is loaded from the backend and filled into the entry page, and the user can edit the information on the page. Because the fixed field is a system-controlled field and does not need to be filled in by the user, the loop can input the field (the "columnsForInput" local variable), and its field information is shown in Table 5.
[0112] Step 400 specifically includes steps 440 to 446 .
[0113] Step 440: Based on the environment variables, the project structure template and the project interface path, a modification function front-end template file and a modification function back-end template file are established.
[0114] Step 441: For any data model, extract the field information of all fields in the data model, including field name, data type, parent table associated field, newly added components and verification rules.
[0115] Step 442: For any field in the data model, determine whether the field has a parent table based on the parent table associated field of the field. If the field has a parent table, construct a drop-down list box for loading the parent table in the modification function front-end template file. If the field does not have a parent table, determine whether there is a custom component based on the newly added component. If there is a custom component, construct the custom component in the modification function front-end template file. If there is no custom component, construct a radio button, input box or date selector in the modification function front-end template file based on the data type of the field.
[0116] Step 443: Construct form validation rules in the modification function front-end template file according to the validation rules of the fields.
[0117] The process from step 441 to step 443 is the same as the process from step 411 to step 413, and will not be repeated here.
[0118] Step 444: Construct a primary key query function, a duplicate name verification function and a form submission function in the modification function front-end template file. The primary key query function is used to call the primary key query single data interface in the modification function back-end template file, query the value corresponding to the primary key to the back-end, and fill it into the form after the query is successful. The duplicate name verification function is used to call the duplicate data verification interface in the modification function back-end template file, and pass the field value with is_name as true to the background to verify whether it is a duplicate name. The form submission function is used to call the modification data interface in the modification function back-end template file, write the data into the database, and call the function when the user clicks submit to complete the new function.
[0119] Step 445: construct a basic layer and a business layer in the modification function backend template file. The basic layer includes the modification function class. The business layer inherits the basic layer, and the business layer is used to cover or extend the modification function class in the basic layer.
[0120] Step 446: Construct a primary key query single data interface, a duplicate data verification interface and a data modification interface in the business layer of the modification function backend template file, so as to display the query results on the front-end page based on the data in the primary key query data model received by the front-end page, and write the modified data received by the front-end page into the data model.
[0121] The primary key query single data interface (such as ${tableBean.camelName} / get.do) uses the primary key field tableBean.primaryColumnName as the input parameter of get.do in the BaseController layer to receive the data to be queried from the front end. After receiving the parameter, the BaseService layer directly calls the BaseDao layer, where the SQL statement is spliced to query the value corresponding to the primary key field and construct the model object. The SQL statement template is select*fromtable_name where${tableBean.primaryColumnNam}=?.
[0122] Verify the duplicate data interface (such as ${tableBean.camelName} / exists.do). In the BaseController layer, traverse all the field information in the local variable tableBean.columns, find the name field with is_name as true, and use it together with tableBean.primaryColumnName as input parameters to receive the name and primary key value to be verified from the front end. In the BaseService layer, after receiving the parameters, the BaseDao layer is directly called. In the BaseDao layer, SQL statements are spliced to query whether the value corresponding to the name field exists in the table. Unlike the new function, the modified function needs to exclude the primary key before judging whether it exists in the table. The SQL statement template is select count(*)from table_name where${name_column}=? and${tableBean.primaryColumnNam}! =?.
[0123] Modify the data interface (such as ${tableBean.camelName} / update.do). In the BaseController layer, put ${tableBean.pascalName}Model into the receiving parameter. The form data passed in by the front end will be automatically filled into the Model. First, check the corresponding fields in the passed-in Model. The verification method can be verified through the annotation of the Model, or all the verification rules can be obtained by traversing the verifys of each field. Generate verification codes according to the two categories of values and strings (values only need to add size judgment, and strings need to add regular expression verification). If the verification fails, an error message is returned. After the verification is successful, the Model is passed to the Service for the next business processing. After implementing the duplicate name verification of the data in the service layer, the current time is written into the update_time in the Model, and the login user id is written into the update_user_id. In the database persistence layer, by looping through the columns in the tableBean, traverse all fields and splice them into SQL statements: update table_name set <field name 1> = <field value 1>, <field name 2> = <field value 2>.... Then substitute the Model through the code and execute the SQL statement.
[0124] (5) For the export function: the template file also includes an export function front-end template file and an export function back-end template file. Step 400 specifically includes steps 450 to 454.
[0125] Step 450: Based on the environment variables, the project structure template and the project interface path, an export function front-end template file and an export function back-end template file are established.
[0126] Step 451: copy the content of the query function front-end template file to the export function front-end template file, and construct an export button in the export function front-end template file. The template file of a single data model mainly adds an export button on the query page.
[0127] Step 452: construct an export function in the export function front-end template. The export function is used to call the export data interface in the export function back-end template file, submit multiple query conditions to the back-end for export, and automatically download Excel after export.
[0128] Step 453: construct a basic layer and a business layer in the export function backend template file. The basic layer includes the export function class. The business layer inherits the basic layer, and the business layer is used to cover or extend the export function class in the basic layer.
[0129] Step 454: construct an export data interface in the business layer of the export function backend template file to import the corresponding data into the Excel file according to the export instruction received by the front-end page.
[0130] Export data interface such as (${tableBean.camelName} / export.do), generate params class from data model, read columns in tableBean, traverse all fields, generate ${tableBean.PascalName}Params, which mainly includes code comments (field name, author, creation time), annotations for generating interface documents (field name, table name, field type), field data type, field name and field default value. In the BaseController layer, put ${tableBean.pascalName}Params into the receiving parameters, and the form data passed in by the front end will be automatically filled into params, and params will be passed to Service for the next business processing. Pass params to Dao layer in the service layer. In the database persistence layer, traverse all fields by looping through columns in tableBean to determine whether each field has a value in params. If so, splice the query conditions into the query statement to obtain the query SQL statement. Then substitute params through code and execute the SQL statement.
[0131] In this embodiment, the query is limited to 1000 rows each time to avoid too much data being found at one time and the memory cannot carry it. Each batch of data found is written into Excel for storage until the found data is less than 1000 rows, indicating that the writing is completed, and the download URL of Excel is returned to the front desk for users to download.
[0132] (6) For the import function: the template file also includes an import function front-end template file and an import function back-end template file. The template file writing process of the import function is as follows: Figure 6 Step 400 specifically includes steps 460 to 464.
[0133] Step 460: Based on the environment variables, the project structure template and the project interface path, create an import function front-end template file and an import function back-end template file.
[0134] Step 461: copy the content in the query function front-end template file to the import function front-end template file, and construct an import button in the import function front-end template file. The template file of a single data model mainly adds an import button on the query page.
[0135] Step 462: Construct an import function in the import function front-end template. The import function is used to call the import data interface in the import function back-end template file to submit the uploaded Excel to the back-end for import. If the import is successful, it will prompt success. If the import fails, it will prompt to download an Excel file. This Excel file adds annotations to the corresponding error cells based on the uploaded Excel file to prompt error information.
[0136] Step 463: construct a basic layer and a business layer in the import function backend template file. The basic layer includes the import function class. The business layer inherits the basic layer, and the business layer is used to cover or extend the import function class in the basic layer.
[0137] Step 464: construct an import data interface in the business layer of the import function backend template file to write the data in the Excel file into the data model.
[0138] Import data interface such as (${tableBean.camelName} / import.do) obtains the mapping relationship between the primary key and the primary key name of the parent table through getDictList, parses Excel to read data line by line, verifies each line of data, and converts the primary key name filled in Excel into the primary key through the mapping relationship of the parent table after the verification passes, and inserts it into the database by calling the insert method. If the verification fails or the mapping relationship cannot be found, the row and column numbers are marked. After all rows are processed, if there is an error, the error information is written to the notes of the corresponding row and column to provide users with download, modification and resubmission. At the same time, the inserted data is rolled back.
[0139] The present invention can generate project templates with basic functions of adding, deleting, modifying, checking, importing and exporting in various languages and technical document templates related to the data model by parsing the data model + template, so as to realize the generalized business logic. In addition, in order to allow users to generate codes multiple times, a two-layer inheritance code mechanism is designed, one layer is the basic layer code generated by the present invention, and the other layer is the code developed by the user's own expansion. The two sets of codes can be integrated with each other through the inheritance mechanism of Java itself. The present invention supports the generation of code from the data model, which saves a lot of time for web front-end engineers, back-end software engineers, and APP developers.
[0140] In one embodiment, a computer device is also provided, including a memory and a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps in the above-mentioned method for generating a multi-language program template based on a data model when executing the computer program.
[0141] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method for generating a multi-language program template based on a data model are implemented.
[0142] In one embodiment, a computer program product is provided, including a computer program, which implements the steps in the above-mentioned method for generating a multi-language program template based on a data model when executed by a processor.
[0143] The technical features of the above embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0144] The principles and implementation methods of the present invention are described in this article using specific examples. The description of the above embodiments is only used to help understand the method and core idea of the present invention. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as limiting the present invention.
Claims
1. A method for generating a multi-language program template based on a data model, characterized in that: The method for generating a multi-language program template based on a data model comprises: Define environment variables; the environment variables include global variables and local variables; the global variables include the Chinese name of the project, package name, interface path, service port number, database name and package name path; the local variables include the data model name, table name and field name; Determine a project structure template based on the environment variable; the project structure template is used to construct an output path of a template file; Determine a project interface path based on the environment variable; the project interface path is used to construct an access interface for a code file corresponding to a data model; the data model is a database pre-constructed according to project requirements; Based on the environment variables, the project structure template and the project interface path, a template file for realizing project functions is written to obtain a multi-language program template; the project functions include adding functions, querying functions, deleting functions, modifying functions, exporting functions and importing functions; The template files include a modification function front-end template file, a modification function back-end template file, a query function front-end template file, an import function front-end template file, and an import function back-end template file; Based on the environment variables, the project structure template and the project interface path, a template file for implementing project functions is written, specifically including: Based on the environment variables, the project structure template and the project interface path, a modification function front-end template file and a modification function back-end template file are established; For any data model, extract the field information of all fields in the data model; the field information includes field name, data type, parent table related fields, newly added components and verification rules; For any field in the data model, determine whether the field has a parent table according to the parent table associated field of the field; if the field has a parent table, construct a drop-down list box for loading the parent table in the modification function front-end template file; if the field does not have a parent table, determine whether there is a custom component according to the newly added component; if there is a custom component, construct the custom component in the modification function front-end template file; if there is no custom component, construct a radio button or input box or date selector in the modification function front-end template file according to the data type of the field; Construct a form validation rule in the modification function front-end template file according to the validation rule of the field; A primary key query function, a duplicate name verification function and a form submission function are constructed in the modification function front-end template file; the primary key query function is used to call the primary key query single data interface in the modification function back-end template file; the duplicate name verification function is used to call the duplicate data verification interface in the modification function back-end template file; the form submission function is used to call the modification data interface in the modification function back-end template file; A basic layer and a business layer are constructed in the modification function backend template file; the basic layer includes a modification function class; the business layer inherits the basic layer, and the business layer is used to cover or extend the modification function class in the basic layer; Constructing a primary key query single data interface, a duplicate data verification interface and a data modification interface in the business layer of the modification function backend template file, so as to display the query result on the front-end page according to the data in the primary key query data model received by the front-end page, and write the modified data received by the front-end page into the data model; Based on the environment variables, the project structure template and the project interface path, establish an import function front-end template file and an import function back-end template file; Copy the content in the query function front-end template file into the import function front-end template file, and construct an import button in the import function front-end template file; An import function is constructed in the import function front-end template; the import function is used to call the import data interface in the import function back-end template file; A basic layer and a business layer are constructed in the import function backend template file; the basic layer includes an import function class; the business layer inherits the basic layer, and the business layer is used to cover or extend the import function class in the basic layer; An import data interface is constructed in the business layer of the import function backend template file to write the data in the excel file into the data model.
2. The method for generating a multi-language program template based on a data model according to claim 1, characterized in that: The project structure template includes the environment variables, and the suffix of the project structure template is .ftl; the number of local variables in the project structure template is the same as the number of data models.
3. The method for generating a multi-language program template based on a data model according to claim 1, characterized in that: The template files also include a new function front-end template file and a new function back-end template file; Based on the environment variables, the project structure template and the project interface path, a template file for implementing project functions is written, which also includes: Based on the environment variables, the project structure template and the project interface path, a new function front-end template file and a new function back-end template file are established; For any data model, extract the field information of all fields in the data model; the field information includes field name, data type, parent table related fields, newly added components and verification rules; For any field in the data model, determine whether the field has a parent table according to the parent table associated field of the field; if the field has a parent table, construct a drop-down list box for loading the parent table in the new function front-end template file; if the field does not have a parent table, determine whether there is a custom component according to the new component; if there is a custom component, construct the custom component in the new function front-end template file; if there is no custom component, construct a radio button or input box or date selector in the new function front-end template file according to the data type of the field; Construct a form validation rule in the new function front-end template file according to the validation rule of the field; Construct a duplicate name verification function and a form submission function in the new function front-end template file; the duplicate name verification function is used to call the duplicate data verification interface in the new function back-end template file; the form submission function is used to call the data insertion interface in the new function back-end template file; A basic layer and a business layer are constructed in the new function backend template file; the basic layer includes the new function class; the business layer inherits the basic layer, and the business layer is used to cover or extend the new function class in the basic layer; A duplicate data verification interface and a data insertion interface are constructed in the business layer of the new function back-end template file to write the data received by the front-end page into the data model.
4. The method for generating a multi-language program template based on a data model according to claim 1, characterized in that: The template file also includes a query function backend template file; Based on the environment variables, the project structure template and the project interface path, a template file for implementing project functions is written, which also includes: Based on the environment variables, the project structure template and the project interface path, a query function front-end template file and a query function back-end template file are established; For any data model, extract the field information of all fields in the data model; the field information includes field name, data type, parent table associated field, verification rules and viewing components; For any field in the data model, determine whether the field has a parent table according to the parent table associated field of the field; if the field has a parent table, construct a drop-down list box for loading the parent table in the query function front-end template file; if the field does not have a parent table, determine whether there is a custom display component according to the viewing component; if there is a custom display component, construct the custom display component in the query function front-end template file; if there is no custom display component, construct a radio button or input box or date selector in the query function front-end template file according to the data type of the field to construct query conditions on the front-end page; For any field in the data model, determine whether the field has a parent table; if the field has a parent table, construct a label for displaying the parent table name in the query function front-end template file; if the field has no parent table and the field has a custom display component, construct a custom display component in the query function front-end template file to construct a list header on the front-end page; Constructing a query paging function in the query function front-end template file; the query paging function is used to call the query data interface in the query function back-end template file; A basic layer and a business layer are constructed in the query function backend template file; the basic layer includes a query function class; the business layer inherits the basic layer, and the business layer is used to cover or extend the query function class in the basic layer; A query data interface is constructed in the business layer of the query function backend template file to query the fields in the data model according to the query conditions received by the frontend page, and fill the query results into a list.
5. The method for generating a multi-language program template based on a data model according to claim 4, characterized in that: The template file also includes a deletion function front-end template file and a deletion function back-end template file; Based on the environment variables, the project structure template and the project interface path, a template file for implementing project functions is written, which also includes: Based on the environment variables, the project structure template and the project interface path, a deletion function front-end template file and a deletion function back-end template file are established; Copy the content of the query function front-end template file into the delete function front-end template file, and construct a check box corresponding to the primary key field in the data model in the delete function front-end template file; A primary key deletion function is constructed in the deletion function front-end template file; the primary key deletion function is used to call a batch deletion interface in the deletion function back-end template file; A basic layer and a business layer are constructed in the deletion function backend template file; the basic layer includes a deletion function class; the business layer inherits the basic layer, and the business layer is used to cover or extend the deletion function class in the basic layer; A batch deletion interface is constructed in the business layer of the deletion function backend template file to delete the data in the data model according to the primary key field received by the frontend page.
6. The method for generating a multi-language program template based on a data model according to claim 4, characterized in that: The template file also includes an export function front-end template file and an export function back-end template file; Based on the environment variables, the project structure template and the project interface path, a template file for implementing project functions is written, which also includes: Based on the environment variables, the project structure template and the project interface path, an export function front-end template file and an export function back-end template file are established; Copy the content of the query function front-end template file into the export function front-end template file, and construct an export button in the export function front-end template file; Constructing an export function in the export function front-end template; the export function is used to call the export data interface in the export function back-end template file; A basic layer and a business layer are constructed in the export function backend template file; the basic layer includes an export function class; the business layer inherits the basic layer, and the business layer is used to cover or extend the export function class in the basic layer; An export data interface is constructed in the business layer of the export function backend template file to write corresponding data into an excel file according to the export instruction received by the front-end page.
7. A computer device comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the method for generating a multi-language program template based on a data model as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Java-based quick code generation method
CN110941423A
Method and system for converting natural language into program code
CN112306497A
