Disclosure of Invention
The invention provides a template-based back-end code generation method and system, aiming at overcoming the problems of code redundancy, low development efficiency and long development period existing in back-end development in the prior art.
In one aspect, the present invention provides a template-based backend code generation method, including:
s1, acquiring a service module contained in the service requirement according to the service requirement provided by a user, and inquiring a service template corresponding to the service module from a preset template library;
and S2, when the service template corresponding to the service module is inquired, importing the service template into a back end code editing interface by using a preset template import grammar, and generating a back end code corresponding to the service requirement.
Preferably, the step S2 is followed by:
s3, obtaining the target service corresponding to the service requirement, packaging the back end code corresponding to the service requirement into the target template corresponding to the target service, and storing the mark of the target service and the target template in the template library in a correlation manner.
Preferably, the step S3 is followed by:
s4, receiving a request of a user for the target service, obtaining the template content of the target template corresponding to the target service from the template library, obtaining the functional module contained in the template content, calling a preset analysis engine according to the functional module, analyzing the template content by using the analysis engine, and feeding back the analysis result to the user.
Preferably, the parsing the template content with the parsing engine in step S4 further comprises:
analyzing the template content by using a Freemarker engine to obtain a corresponding JSON array; and analyzing the JSON array by using the analysis engine, and calling a corresponding data source analyzer according to the data source type in the template content to analyze the JSON array to obtain an analysis result.
Preferably, the step S1 of querying the service template corresponding to the service module from a preset template library further includes:
acquiring identification information of the service module, inquiring a template identification which is the same as the identification information from a preset database according to the identification information, and acquiring a corresponding service template according to the template identification.
Preferably, the step S2 further includes: when the service template corresponding to the service module is not inquired, acquiring a service code edited by a programmer according to the service module, packaging the service code into the service template corresponding to the service module, and storing the identification information of the service module and the service template in the template library in an associated manner.
In one aspect, the present invention provides a template-based backend code generation system, including:
the template query module is used for acquiring a service module contained in the service requirement according to the service requirement provided by a user and querying a service template corresponding to the service module from a preset template library;
and the code generation module is used for importing the service template into a back-end code editing interface by using a preset template import syntax when the service template corresponding to the service module is inquired, and generating a back-end code corresponding to the service requirement.
Preferably, the method further comprises the following steps:
and the encapsulation storage module is used for acquiring the target service corresponding to the service requirement, encapsulating the back-end code corresponding to the service requirement into a target template corresponding to the target service, and storing the identifier of the target service and the target template in the template library in an associated manner.
Preferably, the method further comprises the following steps:
and the template analysis module is used for receiving a request of a user for the target service, acquiring the template content of a target template corresponding to the target service from the template library, acquiring a functional module contained in the template content, calling a corresponding analysis engine according to the functional module, analyzing the template content by using the analysis engine, and feeding back an analysis result to the user.
In one aspect, the present invention provides a device for a template-based backend code generation method, including:
at least one processor; and
at least one memory communicatively coupled to the processor, wherein:
the memory stores program instructions executable by the processor, the processor being capable of performing any of the methods described above when invoked by the processor.
The invention provides a template-based back-end code generation method and a template-based back-end code generation system.A service module contained in a service demand is acquired according to the service demand provided by a user, and a service template corresponding to the service module is inquired from a preset template library; when the service template corresponding to the service module is inquired, the preset template import grammar is utilized to import the service template into the back-end code editing interface, and the back-end code corresponding to the service requirement can be generated. The method effectively avoids the code redundancy phenomenon existing in the back-end development through the reuse of the template, and improves the overall development efficiency to a certain extent; meanwhile, by importing the templates in the front-end template library, developers can write the back-end codes and test the back-end codes at the same time without recompiling and releasing, and the whole development period is effectively shortened.
Detailed Description
The following detailed description of embodiments of the present invention is provided in connection with the accompanying drawings and examples. The following examples are intended to illustrate the invention but are not intended to limit the scope of the invention.
Fig. 1 is a schematic overall flow chart of a method for generating a back-end code based on a template according to an embodiment of the present invention, and as shown in fig. 1, the present invention provides a method for generating a back-end code based on a template, including:
s1, acquiring a service module contained in the service requirement according to the service requirement provided by a user, and inquiring a service template corresponding to the service module from a preset template library;
and S2, when the service template corresponding to the service module is inquired, importing the service template into a back end code editing interface by using a preset template import grammar, and generating a back end code corresponding to the service requirement.
Specifically, in the process of system development, a developer acquires a service requirement provided by a user, inputs the service requirement into the system, analyzes a service module included in the service requirement according to the service requirement, and queries a service template corresponding to the service module from a preset template library. The preset template library is a front-end database, and developers write corresponding back-end codes aiming at different business requirements in the historical development process, and encapsulate the back-end codes into business templates which are stored in the preset template library.
Further, when a service template corresponding to a service module is queried in a preset template library, the queried service template is imported into a back-end code editing interface by using a preset template import syntax, that is, codes do not need to be rewritten for the service module, but only the template import code corresponding to the service module needs to be written by using the preset template import syntax, so that the back-end code corresponding to the service requirement can be generated. The template import syntax is preset by a developer, and a specific expression form of the template import syntax can be set according to actual requirements, which is not specifically limited herein. For example, the grammar for template import may be embodied in the form of:
wherein type represents the type of the template; the name represents the name of the template, and the template is uniquely identified through the type of the template and the name of the template.
In order to better understand the above method steps, the following examples are now described:
for example, in a real estate system, the service requirements provided by a user are: sending the viewing amount of the newly added house source to the user in the form of an email, and aiming at the service demand system, analyzing that the service demand comprises two service modules which are respectively: (1) acquiring the source of the newly added house with the watching amount; (2) and sending the information to the user in the form of mail. On the basis, according to the two service modules, the service templates corresponding to the two service modules are inquired from a preset template library. If the two service modules are realized in the historical development process, the preset template library stores the service templates corresponding to the two service modules. When the service templates corresponding to the two service modules are inquired, the preset template grammar is used for compiling the import code, so that the service templates corresponding to the two service modules are imported into a back-end code editing interface, and the back-end code corresponding to the service requirement can be organized.
The invention provides a template-based back-end code generation method, which comprises the steps of acquiring a service module contained in a service demand according to the service demand provided by a user, and inquiring a service template corresponding to the service module from a preset template library; when the service template corresponding to the service module is inquired, the preset template import grammar is utilized to import the service template into the back-end code editing interface, and the back-end code corresponding to the service requirement can be generated. The method effectively avoids the code redundancy phenomenon existing in the back-end development through the reuse of the template, and improves the overall development efficiency to a certain extent; meanwhile, by importing the templates in the front-end template library, developers can write the back-end codes and test the back-end codes at the same time without recompiling and releasing, and the whole development period is effectively shortened.
Based on any one of the above embodiments, there is provided a template-based backend code generation method, where after the step S2, the method further includes: s3, obtaining the target service corresponding to the service requirement, packaging the back end code corresponding to the service requirement into the target template corresponding to the target service, and storing the mark of the target service and the target template in the template library in a correlation manner.
Specifically, on the basis of the above technical solution, after the back-end code corresponding to the service requirement is generated, in order to facilitate subsequent multiplexing of the back-end code corresponding to the service requirement, in this embodiment, a target service corresponding to the service requirement is determined according to the service requirement, and a service identifier corresponding to the target service is obtained; and meanwhile, encapsulating the back-end code corresponding to the service requirement into a target template corresponding to the target service, and finally storing the identifier of the target service and the target template in a preset template library in an associated manner.
Further, in the subsequent development process, when a developer needs to write a back-end code for a new service requirement, if the target service exists in a service module included in the new service requirement, the developer can directly import a target template corresponding to the target service from a template library by using a preset template import syntax, so that the multiplexing of the target template can be realized.
According to the template-based back-end code generation method, after the back-end codes corresponding to the business requirements are generated, the back-end codes corresponding to the business requirements are packaged into the templates which are correspondingly stored in the preset template library, so that developers can conveniently multiplex the back-end codes corresponding to the business requirements in the subsequent development process, and the development efficiency is improved.
Based on any one of the above embodiments, there is provided a template-based backend code generation method, where after the step S3, the method further includes: s4, receiving a request of a user for the target service, obtaining the template content of the target template corresponding to the target service from the template library, obtaining the functional module contained in the template content, calling a preset analysis engine according to the functional module, analyzing the template content by using the analysis engine, and feeding back the analysis result to the user.
Specifically, on the basis of storing the target template corresponding to the target service in a preset template library, when a user initiates a request for the target service, the system receives the request of the user for the target service, and obtains the target template corresponding to the target service from the preset template library, thereby obtaining the template content corresponding to the target template. The template content may be HTML, XML or JSON text, and may also include freemaker template code, which is not limited herein.
Further, in this embodiment, the template content corresponding to each template in the preset template library is written according to a unified service logic, where the unified service logic may be, for example: step one, processing request parameters and verifying user authority; step two, acquiring data from each data source; and step three, processing the data according to the service logic, organizing the data into a certain form and the like. Wherein the step corresponds to the realization of the initialized function; step two, the function of data acquisition is realized; and step three realizes the function of data processing. That is, each template content corresponds to a plurality of function modules, and separators can be set between the function modules in the programming process, so as to separate the function modules.
On the basis of the above technical solution, in this embodiment, the function modules included in the template content corresponding to the target service are obtained according to the separators in the template content, that is, the template content is divided into a plurality of function modules according to specific function implementations. Different functional modules need different analysis engines to analyze the functional modules, so that the corresponding analysis engines are called for the obtained functional modules, the corresponding analysis engines are used for analyzing each functional module one by one, finally, the content of the whole template is analyzed, corresponding analysis results are obtained, and the analysis results are fed back to a user, namely, the user request is responded. The analysis engines are preset for each functional module and are pre-stored in the database, so that when different service requests are processed, the corresponding analysis engines can be called from the database for analysis for the same functional module, namely, the multiplexing of the analysis engines is realized.
According to the template-based back-end code generation method, when the request of a user for the target service is processed, the template content of the target template corresponding to the target service can be directly obtained from the template library without recompiling and releasing, so that the whole development period is effectively shortened; and meanwhile, the functional module contained in the template content is obtained, and the preset analysis engine is called correspondingly according to the functional module to analyze the template content, so that the multiplexing of the analysis engine is effectively realized, and the overall development efficiency is further improved.
Based on any one of the above embodiments, in providing a template-based backend code generation method, the parsing, by using the parsing engine, the template content in step S4 further includes: analyzing the template content by using a Freemarker engine to obtain a corresponding JSON array; and analyzing the JSON array by using the analysis engine, and calling a corresponding data source analyzer according to the data source type in the template content to analyze the JSON array to obtain an analysis result.
Specifically, the specific implementation process of parsing the template content by using the parsing engine is as follows:
firstly, a Freemarker engine is used for carrying out preliminary analysis on template contents, special characters representing numerical values in the template contents are analyzed into specific numerical values, and then JSON arrays corresponding to the template contents are obtained. On the basis, the JSON array is analyzed by using the obtained analysis engine corresponding to each functional module, the data source type contained in the template content is obtained simultaneously in the analysis process, the corresponding data source analyzer is called according to the data source type to analyze each element in the JSON array, the analysis result is finally obtained, and the analysis result can be stored in a mapping table in a key value pair mode. In addition, in other embodiments, when the template content is preliminarily analyzed, in addition to invoking the freemaker engine, a Velocity engine may also be invoked, which may be set according to actual requirements, and this is not specifically limited here.
According to the template-based rear-end code generation method, when a user request for a target service is processed, template contents of a target template corresponding to the target service are obtained from a template library, the template contents are preliminarily analyzed by a Freemarker engine to obtain a corresponding JSON array, the JSON array is analyzed by an analysis engine, a corresponding data source analyzer is called according to data source types in the template contents to analyze the JSON array, an analysis result is finally obtained, analysis of the template contents is effectively achieved, reuse of the analysis engine is effectively achieved, and overall development efficiency is further improved.
Based on any of the above embodiments, a template-based backend code generation method is provided, where the querying, in step S1, a service template corresponding to the service module from a preset template library further includes: acquiring identification information of the service module, inquiring a template identification which is the same as the identification information from a preset database according to the identification information, and acquiring a corresponding service template according to the template identification.
Specifically, in a preset template library, for each template stored therein, a template identifier corresponding to each template is stored in an associated manner, the template identifier includes a template type and a template name, and each template is uniquely identified by the template type and the template name. On this basis, when the service template corresponding to the service module needs to be queried from the pre-stored database, the specific process is as follows: firstly, obtaining the identification information of a service module, then inquiring a template identification which is the same as the identification information from a preset database according to the identification information, and then obtaining a corresponding service template according to the template identification.
In addition, in the preset template library, for each template stored therein, a template summary and the like corresponding to each template may be stored in association, and the setting may be performed according to actual requirements, which is not specifically limited herein.
The template-based back-end code generation method provided by the invention uniquely identifies each module in the preset template library, can inquire the template identification with the same identification information as the service module from the preset database when inquiring the service template corresponding to the service module, and then obtains the corresponding service template according to the template identification, thereby effectively realizing the reuse of the template, avoiding the code redundancy phenomenon existing in the back-end development and improving the overall development efficiency to a certain extent.
Based on any one of the above embodiments, there is provided a template-based backend code generation method, where the step S2 further includes: when the service template corresponding to the service module is not inquired, acquiring a service code edited by a programmer according to the service module, packaging the service code into the service template corresponding to the service module, and storing the identification information of the service module and the service template in the template library in an associated manner.
Specifically, a service module included in a service requirement is analyzed according to the service requirement provided by a user, a service template corresponding to the service module is queried from a preset template library, and when the service template corresponding to the service module is not queried, a programmer needs to write a corresponding service code according to the service module. On the basis, the system acquires the service code edited by the programmer according to the service module, encapsulates the service code into a service template corresponding to the service module, uses the identification information of the service module as a template identification, and stores the template identification and the service template in a preset template library in an associated manner, so that the subsequent multiplexing of the service template is facilitated.
For example, common service templates include an initialization template, a data acquisition template, and a data processing template, and some service requirements may include the following service modules: the data processing result needs to be output in the form of mail. If the service template corresponding to the service module does not exist in the template library at this time, the programmer needs to write the service code corresponding to the service module, encapsulate the service code into the service template corresponding to the service module, and store the identification information of the service module and the service template in the template library in an associated manner.
The invention provides a template-based back-end code generation method, which comprises the steps of analyzing a service module contained in a service demand according to the service demand provided by a user, inquiring a service template corresponding to the service module from a preset template library, acquiring a service code edited by a programmer according to the service module, packaging the service code into the service template corresponding to the service module, and storing identification information of the service module and the service template in the template library in an associated manner, thereby being beneficial to realizing the reuse of the template in the subsequent process, effectively avoiding the code redundancy phenomenon existing in the back-end development process, and improving the overall development efficiency to a certain extent; meanwhile, the template is stored in the front-end template library, so that a developer can write the back-end code and test the back-end code at the same time without compiling and releasing the back-end code, and the whole development period is effectively shortened.
Fig. 2 is a schematic diagram of an overall structure of a template-based backend code generation system according to an embodiment of the present invention, and as shown in fig. 2, the present invention provides a template-based backend code generation system, including:
the template query module 1 is used for acquiring a service module contained in a service demand according to the service demand provided by a user, and querying a service template corresponding to the service module from a preset template library;
and the code generation module 2 is used for importing the service template into a back-end code editing interface by using a preset template import syntax when the service template corresponding to the service module is inquired, and generating a back-end code corresponding to the service requirement.
Specifically, the present invention provides a template-based backend code generation system, which includes a template query module 1 and a code generation module 2, and the template-based backend code generation method in any of the above embodiments is implemented by each module, and is specifically implemented as follows:
in the process of system development, a developer acquires a service requirement provided by a user, the developer inputs the service requirement into the module query module 1, the module query module 1 analyzes a service module contained in the service requirement according to the service requirement, and then queries a service template corresponding to the service module from a preset template library. The preset template library is a front-end database, and developers write corresponding back-end codes aiming at different business requirements in the historical development process, and encapsulate the back-end codes into business templates which are stored in the preset template library.
Further, when the template query module 1 queries a service template corresponding to a service module in a preset template library, the code generation module 2 imports the queried service template into a back-end code editing interface by using a preset template import syntax, that is, codes do not need to be rewritten for the service module, but only the template import code corresponding to the service module is compiled by using the preset template import syntax, so that the back-end code corresponding to the service requirement can be generated. The template import syntax is preset by a developer, and a specific expression form of the template import syntax can be set according to actual requirements, which is not specifically limited herein. For example, the grammar for template import may be embodied in the form of:
wherein type represents the type of the template; the name represents the name of the template, and the template is uniquely identified through the type of the template and the name of the template.
The invention provides a template-based back-end code generation system, which acquires a service module contained in a service demand according to the service demand provided by a user, and queries a service template corresponding to the service module from a preset template library; when the service template corresponding to the service module is inquired, the preset template import grammar is utilized to import the service template into the back-end code editing interface, and the back-end code corresponding to the service requirement can be generated. The method effectively avoids the code redundancy phenomenon existing in the back-end development through the reuse of the template, and improves the overall development efficiency to a certain extent; meanwhile, by importing the templates in the front-end template library, developers can write the back-end codes and test the back-end codes at the same time without recompiling and releasing, and the whole development period is effectively shortened.
Based on any of the above embodiments, there is provided a template-based backend code generation system, further including: and the encapsulation storage module 3 is used for acquiring the target service corresponding to the service requirement, encapsulating the back-end code corresponding to the service requirement into a target template corresponding to the target service, and storing the identifier of the target service and the target template in the template library in an associated manner.
Specifically, on the basis of the above technical solution, after the code generation module 2 generates the backend code corresponding to the service requirement, in order to facilitate subsequent multiplexing of the backend code corresponding to the service requirement, in this embodiment, the encapsulation storage module 3 is used to determine the target service corresponding to the service requirement according to the service requirement, and obtain the service identifier corresponding to the target service; and meanwhile, encapsulating the back-end code corresponding to the service requirement into a target template corresponding to the target service, and finally storing the identifier of the target service and the target template in a preset template library in an associated manner.
Further, in the subsequent development process, when a developer needs to write a back-end code for a new service requirement, if the target service exists in a service module included in the new service requirement, the developer can directly import a target template corresponding to the target service from a template library by using a preset template import syntax, so that the multiplexing of the target template can be realized.
According to the template-based back-end code generation system, after the back-end codes corresponding to the business requirements are generated, the back-end codes corresponding to the business requirements are packaged into the templates which are correspondingly stored in the preset template library, so that developers can conveniently multiplex the back-end codes corresponding to the business requirements in the subsequent development process, and the development efficiency is improved.
Based on any of the above embodiments, there is provided a template-based backend code generation system, further including: and the template analysis module 4 is used for receiving a request of a user for the target service, acquiring the template content of a target template corresponding to the target service from the template library, acquiring a functional module contained in the template content, calling a corresponding analysis engine according to the functional module, analyzing the template content by using the analysis engine, and feeding back an analysis result to the user.
Specifically, on the basis of storing the target template corresponding to the target service in a preset template library, when a user initiates a request for the target service, the template analysis module 4 is used to receive the request of the user for the target service, and obtain the target template corresponding to the target service from the preset template library, so as to obtain the template content corresponding to the target template. The template content may be HTML, XML or JSON text, and may also include freemaker template code, which is not limited herein.
Further, in this embodiment, the template content corresponding to each template in the preset template library is written according to a unified service logic, where the unified service logic may be, for example: step one, processing request parameters and verifying user authority; step two, acquiring data from each data source; and step three, processing the data according to the service logic, organizing the data into a certain form and the like. Wherein the step corresponds to the realization of the initialized function; step two, the function of data acquisition is realized; and step three realizes the function of data processing. That is, each template content corresponds to a plurality of function modules, and separators can be set between the function modules in the programming process, so as to separate the function modules.
On the basis of the above technical solution, in this embodiment, the template parsing module 4 is used to obtain the functional modules included in the template content corresponding to the target service according to the separators in the template content, that is, the template content is divided into a plurality of functional modules according to specific functions. Different functional modules need different analysis engines to analyze the functional modules, so that the corresponding analysis engines are called for the obtained functional modules, the corresponding analysis engines are used for analyzing each functional module one by one, finally, the content of the whole template is analyzed, corresponding analysis results are obtained, and the analysis results are fed back to a user, namely, the user request is responded. The analysis engines are preset for each functional module and are pre-stored in the database, so that when different service requests are processed, the corresponding analysis engines can be called from the database for analysis for the same functional module, namely, the multiplexing of the analysis engines is realized.
According to the template-based back-end code generation system, when the request of a user for the target service is processed, the template content of the target template corresponding to the target service can be directly obtained from the template library without recompiling and releasing, so that the whole development period is effectively shortened; and meanwhile, the functional module contained in the template content is obtained, and the preset analysis engine is called correspondingly according to the functional module to analyze the template content, so that the multiplexing of the analysis engine is effectively realized, and the overall development efficiency is further improved.
Fig. 3 is a block diagram illustrating an apparatus of a template-based backend code generation method according to an embodiment of the present invention. Referring to fig. 3, the apparatus of the template-based backend code generation method includes: a processor (processor)31, a memory (memory)32, and a bus 33; wherein, the processor 31 and the memory 32 complete the communication with each other through the bus 33; the processor 31 is configured to call program instructions in the memory 32 to perform the methods provided by the above-mentioned method embodiments, for example, including: acquiring a service module contained in a service demand according to the service demand provided by a user, and inquiring a service template corresponding to the service module from a preset template library; and when the service template corresponding to the service module is inquired, importing the service template into a back-end code editing interface by using a preset template import grammar to generate a back-end code corresponding to the service requirement.
The present embodiment discloses a computer program product comprising a computer program stored on a non-transitory computer readable storage medium, the computer program comprising program instructions which, when executed by a computer, enable the computer to perform the method provided by the above-mentioned method embodiments, for example, comprising: acquiring a service module contained in a service demand according to the service demand provided by a user, and inquiring a service template corresponding to the service module from a preset template library; and when the service template corresponding to the service module is inquired, importing the service template into a back-end code editing interface by using a preset template import grammar to generate a back-end code corresponding to the service requirement.
The present embodiments provide a non-transitory computer-readable storage medium storing computer instructions that cause the computer to perform the methods provided by the above method embodiments, for example, including: acquiring a service module contained in a service demand according to the service demand provided by a user, and inquiring a service template corresponding to the service module from a preset template library; and when the service template corresponding to the service module is inquired, importing the service template into a back-end code editing interface by using a preset template import grammar to generate a back-end code corresponding to the service requirement.
Those of ordinary skill in the art will understand that: all or part of the steps for implementing the method embodiments may be implemented by hardware related to program instructions, and the program may be stored in a computer readable storage medium, and when executed, the program performs the steps including the method embodiments; and the aforementioned storage medium includes: various media that can store program codes, such as ROM, RAM, magnetic or optical disks.
The above-described embodiments of the device and the like of the template-based backend code generation method are merely illustrative, where the units described as separate components may or may not be physically separate, and the components displayed as the units may or may not be physical units, may be located in one place, or may also be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
In summary, the method and system for generating a backend code based on a template according to the present invention acquire a service module included in a service requirement according to the service requirement provided by a user, and query a service template corresponding to the service module from a preset template library; when the service template corresponding to the service module is inquired, the preset template import grammar is utilized to import the service template into the back-end code editing interface, and the back-end code corresponding to the service requirement can be generated. The method effectively avoids the code redundancy phenomenon existing in the back-end development through the reuse of the template, and improves the overall development efficiency to a certain extent; meanwhile, by importing the templates in the front-end template library, developers can write the back-end codes and test the back-end codes at the same time without recompiling and releasing, and the whole development period is effectively shortened.
Finally, the method of the present application is only a preferred embodiment and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.