CN115562642A - Code generating method based on database and Mustache template engine - Google Patents
Code generating method based on database and Mustache template engine Download PDFInfo
- Publication number
- CN115562642A CN115562642A CN202211334666.1A CN202211334666A CN115562642A CN 115562642 A CN115562642 A CN 115562642A CN 202211334666 A CN202211334666 A CN 202211334666A CN 115562642 A CN115562642 A CN 115562642A
- Authority
- CN
- China
- Prior art keywords
- template
- character
- database
- code
- token
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/20—Software design
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/25—Integrating or interfacing systems involving database management systems
- G06F16/252—Integrating or interfacing systems involving database management systems between a Database Management System and a front-end application
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/10—Text processing
- G06F40/166—Editing, e.g. inserting or deleting
- G06F40/186—Templates
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/38—Creation or generation of source code for implementing user interfaces
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/44—Encoding
- G06F8/447—Target code generation
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Databases & Information Systems (AREA)
- Health & Medical Sciences (AREA)
- Audiology, Speech & Language Pathology (AREA)
- Computational Linguistics (AREA)
- General Health & Medical Sciences (AREA)
- Artificial Intelligence (AREA)
- Data Mining & Analysis (AREA)
- Human Computer Interaction (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
本发明公开了基于数据库和Mustache模板引擎的生成代码方法,包括数据库数据读取模块,模板文件创建模块,Mustache模板引擎解析模块和文件输出模块四个功能模块,本发明不仅适用于系统工程设计中,有关使用数据库存储信息都可以将数据内容生成指定模板输出成有效文件;通过本方法对项目结构的优势是生成代码并不变更项目结构,对开发人员来讲减少了开发人员手动拼写的错误及较大工作压力,减少了测试工作中可能经常出现的问题,大大缩短了软件的开发周期,提高了产品竞争力。
The invention discloses a code generation method based on a database and a Mustache template engine, including four functional modules: a database data reading module, a template file creation module, a Mustache template engine parsing module and a file output module. The invention is not only applicable to system engineering design , related to the use of database storage information, the specified template for data content generation can be output as a valid file; the advantage of this method for the project structure is that the generated code does not change the project structure, and for developers, it reduces manual spelling errors and Higher working pressure reduces the problems that may often occur in the testing work, greatly shortens the software development cycle and improves product competitiveness.
Description
技术领域technical field
本发明涉及IT数据开发领域,特别涉及基于数据库和Mustache模板引擎的生成代码方法。The invention relates to the field of IT data development, in particular to a code generation method based on a database and a Mustache template engine.
背景技术Background technique
随着科技的飞速发展及社会生活节奏的日益提高,用户对于常用的软件使用的度和要求也随之变高,这促使企业不断更新迭代产品优化用户体验效果。With the rapid development of science and technology and the increasing pace of social life, users are more demanding and demanding of commonly used software, which prompts companies to continuously update and iterate products to optimize user experience.
在项目开发过程中业务人员在进行业务逻辑的代码开发时都是重新从数据库设计、接口设计、服务设计、数据层设计、对象实体类设计来的大部分时间花在了重复编辑相似内容,增加了大量的重复和复杂的流程。During the project development process, business personnel spend most of their time re-editing similar content repeatedly from database design, interface design, service design, data layer design, and object entity class design when developing business logic code. Lots of repetition and complicated processes.
目前市面上也存在几款生成工具,但往往都是针对特定的架构、特定的场景才能实现自动生成相应的代码,没有针对整个前后端流程代码开发的自动生成代码文件的方法,为了能更好的解适用基于大众项目架构自动生成代码文件方法或系统,对代码生成方法的创新与拓展变得尤为重要。At present, there are several generation tools on the market, but they are often aimed at specific architectures and specific scenarios to automatically generate corresponding codes. There is no method for automatically generating code files for the development of the entire front-end and back-end process codes. In order to better The solution applies to the method or system for automatically generating code files based on the public project architecture, and the innovation and expansion of code generation methods become particularly important.
频繁的迭代,快速的版本呈现,项目组开发成员压力不断增加,现有技术的上述方式所需的开发人员工作量大,版本上线时间长,因此一种高效的代码文件生成的方法及系统,在项目开发过程中有着非常重大的意义。Frequent iterations, fast version presentation, and increasing pressure on project team development members. The above-mentioned method of the prior art requires a large workload of developers and a long time for the version to go online. Therefore, an efficient method and system for generating code files, It is of great significance in the project development process.
发明内容Contents of the invention
本发明要解决的技术问题是克服现有技术的缺陷,提供基于数据库和Mustache模板引擎的生成代码方法。The technical problem to be solved by the present invention is to overcome the defects of the prior art and provide a code generation method based on database and Mustache template engine.
本发明提供了如下的技术方案:The present invention provides following technical scheme:
本发明提供基于数据库和Mustache模板引擎的生成代码方法,包括数据库数据读取模块,模板文件创建模块,Mustache模板引擎解析模块和文件输出模块四个功能模块,包括以下步骤:The present invention provides the generation code method based on database and Mustache template engine, comprises database data reading module, template file creation module, Mustache template engine parsing module and file output module four functional modules, comprises the following steps:
S1、用户根据业务需要提前创建好所需数据库表结构信息;S1. The user creates the required database table structure information in advance according to the business needs;
S2、根据项目结构为需求创建模板代码文件,包括后端所涉及的数据库持久层相关映射代码模板mapper.xml、持久层处理代码模板mapper.java、实体类BO代码模板entity.java、业务逻辑层处理代码模板service.java,以及前端所涉及的页面文件包含并不限于html,vue,js等前端页面代码文件;S2. Create a template code file for the requirements according to the project structure, including the database persistence layer related mapping code template mapper.xml involved in the backend, the persistence layer processing code template mapper.java, the entity class BO code template entity.java, and the business logic layer Processing code template service.java, and the page files involved in the front end include but are not limited to html, vue, js and other front-end page code files;
S3、通过JdbcTemplate读取配置文件连接数据库读取表结构信息,存储于数据结构之中;S3. Read the configuration file through JdbcTemplate and connect to the database to read the table structure information and store it in the data structure;
S4、通过Mustache模板引擎解析替换模板内容生成所需代码,将生成的代码输出到对应的路径下;S4. Generate the required code by parsing and replacing the template content through the Mustache template engine, and output the generated code to the corresponding path;
S5、对创建更新的代码进行编译运行发布到服务器。S5. Compile, run and publish the created and updated code to the server.
作为本发明的一种优选技术方案,步骤S1中描述数据库不限于常用的mysql,oracle,pgsql等数据库,数据库表结构根据业务需求内容设计好相应的字段。As a preferred technical solution of the present invention, the database described in step S1 is not limited to commonly used databases such as mysql, oracle, pgsql, etc., and the database table structure is designed with corresponding fields according to business requirements.
作为本发明的一种优选技术方案,步骤S2中描述的模板方案基于不限定于任何框架,根据自身项目需求及项目结构生成所需项目代码文件,包含:As a preferred technical solution of the present invention, the template solution described in step S2 is not limited to any framework, and generates the required project code files according to its own project requirements and project structure, including:
S2.1:项目结构中持久层相关代码模板的设计创建,模板名称mapper.xml主要包含xml的约束相应的增删改查等相关需求方法,需要的表名及字段使用"{{XXX}}"替换;模板名称mapper.java是一个Class对象结构包含与mapper.xml相应的方法,引入的Jar包结构及相关BO内容使用"{{XXX}}"替换;S2.1: Design and creation of persistence layer-related code templates in the project structure. The template name mapper.xml mainly includes related requirements methods such as xml constraints, corresponding additions, deletions, modifications, and queries. The required table names and fields use "{{XXX}}" Replacement; the template name mapper.java is a Class object structure containing methods corresponding to mapper.xml, and the imported Jar package structure and related BO content are replaced with "{{XXX}}";
S2.2:项目结构中业务逻辑层处理代码模板的设计创建,模板service.java是一个Class对象结构包含对外接口调用的相应方法,相应的入参出参以及实体DTO用模板字符串"{{XXX}}"替换;S2.2: In the project structure, the business logic layer handles the design and creation of code templates. The template service.java is a Class object structure containing the corresponding methods for external interface calls, corresponding input and output parameters, and template strings for entity DTO "{{ XXX}}" replace;
S2.3:项目结构中实体类代码模板的设计创建,模板entity.java实体类BO代码文件通过{{/idColumn}}、{{^idColumn}}循环与数据库表结构字段一一对应;S2.3: Design and creation of the entity class code template in the project structure, the template entity.java entity class BO code file corresponds to the database table structure fields one by one through {{/idColumn}}, {{^idColumn}} cycle;
S2.4:项目结构中前端文件模板的设计创建,页面模板文件default.html或者page.vue用"{{XXX}}"替换调<div>中的字段内容,前端逻辑JS文件api.js用"{{firstUp}}"等替换相关内容。S2.4: Design and creation of the front-end file template in the project structure, the page template file default.html or page.vue uses "{{XXX}}" to replace the field content in <div>, and the front-end logic JS file api.js "{{firstUp}}" etc. to replace relevant content.
作为本发明的一种优选技术方案,步骤S3描述通过JdbcTemplate读取配置文件连接数据库读取表结构信息,JdbcTemplate是对JDBC数据库交互操作进行封装,其主要封装了JDBC的加载驱动创建数据库DataSource数据源DriverManagerDataSource,注入JDBC驱动步骤为DriverManagerDataSource属性注入DriverClassName;包括以下:As a preferred technical solution of the present invention, step S3 describes reading the configuration file through the JdbcTemplate to connect to the database and read the table structure information. The JdbcTemplate encapsulates the JDBC database interactive operation, and it mainly encapsulates the JDBC loading driver to create the database DataSource data source. DriverManagerDataSource, inject JDBC driver step to inject DriverClassName into the DriverManagerDataSource property; including the following:
S3.1:通过new JdbcTemplate()方法创建JdbcTemplate对象,传入设计好的连接池信息包含步骤S1设计好的表名信息以及数据库的链接地址:"jdbc:mysql://127.0.0.1:3360/xxx?useUnicode=true&characterEncoding=UTF-8"等内容;S3.1: Create a JdbcTemplate object through the new JdbcTemplate() method, and pass in the designed connection pool information including the table name information designed in step S1 and the link address of the database: "jdbc:mysql://127.0.0.1:3360/ xxx?useUnicode=true&characterEncoding=UTF-8" and so on;
S3.2:编写好查询表结构信息语句"SELECT column_name`name`,column_comment`label`,data_type`type`,COLUMN_KEY`key`,EXTRA FROMinformation_schema.columnsWHERE table_name=‘XXX’and table_schema=‘XXX’";S3.2: Prepare the query table structure information statement "SELECT column_name`name`,column_comment`label`,data_type`type`,COLUMN_KEY`key`,EXTRA FROMinformation_schema.columnsWHERE table_name=‘XXX’and table_schema=‘XXX’”;
S3.3:使用现有JdbcTemplate对象通过query方法执行查询语句数据库的操作,根据表约束查询出表结构的详细信息包含业务表中字段列表包括列名、详细说明、物理类型、Java类型、Java属性名称、主键等存放在相关存储List集合列表中为后续步骤S4的使用做准备。S3.3: Use the existing JdbcTemplate object to execute the query statement database operation through the query method, and query the detailed information of the table structure according to the table constraints, including the list of fields in the business table, including column names, detailed descriptions, physical types, Java types, and Java properties The name, primary key, etc. are stored in the related storage List collection list to prepare for the use of the subsequent step S4.
作为本发明的一种优选技术方案,S4中描述的Mustache模板引擎核心是将存储于数据结构中数据表数据内容替换掉S2中描述的模板文件中的标记内容;其思想是将模板转化为tokens嵌套数组中,然后将tokens和数据相结合生成字符串,然后按照不同类型拆分后封装成数组,其保存的字符串对应Mustache引擎识别信息详细步骤如下:As a preferred technical solution of the present invention, the core of the Mustache template engine described in S4 is to replace the tag content in the template file described in S2 with the data content of the data table stored in the data structure; its idea is to convert the template into tokens In the nested array, combine tokens and data to generate strings, and then split them according to different types and package them into arrays. The stored strings correspond to Mustache engine identification information. The detailed steps are as follows:
S4.1:拆解模板字符串并分类;当选择生成代码时,程序会分顺序读取S2中已经创建好的模板,在模板字符串中使用关键变量或使用遍历、if判断的地方一定是使用"{{"和"}}"包裹着的列如一串模板字符串:"<ul>{{#name}}<li><dl><dt>{{#age}}{{#hobbys}}<dd>{{.}}</dd>{{/hobbys}}</dt></dl></li></ul>",普通字符串都是在“{{”的左边,因此可以通过查找“{{”的位置来找到普通字符串,然后进行截取;找到“}}”的位置,然后截取循环直到没有字符串可截取了,如此截取后模板字符串就变成了"<ul>{{","xxx"和"}}<li>...";S4.1: Disassemble the template string and classify it; when choosing to generate code, the program will read the templates created in S2 in order, and the place where the key variable is used in the template string or the traversal or if judgment is used must be Columns wrapped with "{{" and "}}" are like a string of template strings: "<ul>{{#name}}<li><dl><dt>{{#age}}{{#hobbys} }<dd>{{.}}</dd>{{/hobbys}}</dt></dl></li></ul>", ordinary strings are on the left side of "{{", Therefore, you can find the normal string by looking for the position of "{{", and then intercept; find the position of "}}", and then intercept the cycle until there is no string to intercept, so the template string becomes "" <ul>{{","xxx" and "}}<li>...";
S4.2:将字符串模板转换成tokens数组;扫描器接收模板内容作其构造的参数,依靠Mustache内部两个扫描器方法scan和scanUntil,scan方法的作用是使指针跳过指定标记,scanUtil方法的作用是一个一个字符遍历寻找指定标记;循环递归tokens数组,获取开始分隔符前面的字符串,将字符串以"text"存储到tokens数组中,跳过开始分隔符,获取开始分隔符与结束分隔符之间的字符串,如果起始字符是"#"将字符串以"#"存储到tokens数组中,如果起始字符是"/"将字符串以"/"存储到tokens数组中,跳过结束分隔符,得到平铺分组后的字符串数组并把将字符串tokens数组返回;S4.2: Convert the string template into an array of tokens; the scanner receives the template content as its construction parameter, relying on the two scanner methods scan and scanUntil inside Mustache, the function of the scan method is to make the pointer skip the specified token, and the scanUtil method The function is to traverse one character at a time to find the specified token; loop recursively into the tokens array, get the string before the start delimiter, store the string as "text" in the tokens array, skip the start delimiter, get the start delimiter and the end The string between delimiters, if the starting character is "#", store the string as "#" in the tokens array, if the starting character is "/", store the string as "/" in the tokens array, Skip the end delimiter, get the tiled and grouped string array and return the string tokens array;
S4.3:分组组装tokens数组;将S4.2返回的tokens字符串数组运用数据结构栈的思想(先进后出/后进先出)遇见"#"字符就压栈,遇见"/"字符就出栈,"#"字符和"/"字符之间的内容都是其子元素;遇到"#"字符就将当前token(token为tokens循环后的单个字符串属性)推入进栈数组中,并将结果收集器设置为刚入堆的token的子集,遇到"/"字符就将栈数组中最新入栈的那个移除掉,并将结果收集器设置为栈数组中堆顶那个token的子集,或者是最终的结构集,如果不是"#"、"/"字符则直接将当前这个token添加进结果集中,返回组装好的tokens结果集数组;S4.3: Assemble the tokens array by grouping; use the tokens string array returned by S4.2 using the idea of data structure stack (FIFO/LIFO) to push the stack when it encounters a "#" character, and to exit when it encounters a "/" character The stack, the content between the "#" character and the "/" character is its sub-element; when the "#" character is encountered, the current token (token is a single string attribute after the tokens cycle) is pushed into the stack array, And set the result collector as a subset of the tokens that just entered the heap, and remove the latest one in the stack array when the "/" character is encountered, and set the result collector as the token that is at the top of the stack in the stack array Subset, or the final structure set, if it is not a "#", "/" character, directly add the current token into the result set, and return the assembled tokens result set array;
S4.4:根据tokens将模板字符串渲染成代码文件;将tokens循环,"text"字符匹配普通字符串,直接拼接处理;如果是"name"字符子父对象属性访问对象属性使用现有lookup方法作用是以字符串的形式动态的访问对象上深层的属性得出解析好的字符串拼接处理,如果是"#"字符解析tokens字符串模板中的循环得出生成好字符串然后拼接处理,最终拼接处理后的字符串即为渲染后代码文件。S4.4: Render template strings into code files according to tokens; loop tokens, "text" characters match ordinary strings, and directly concatenate them; if it is "name" characters, child and parent object attributes access object attributes using the existing lookup method The role is to dynamically access the deep attributes on the object in the form of strings to obtain the parsed string splicing processing. If it is the "#" character, parse the loop in the tokens string template to generate a good string and then splicing processing, and finally The spliced string is the rendered code file.
作为本发明的一种优选技术方案,S5步骤通过FileUtils文件输出工具类的写字符串到文件,将mustache模板引擎解析模块解析好的字符串生成相应的代码文件,编译项目部署到服务器。As a kind of preferred technical scheme of the present invention, step S5 is through FileUtils file output tool class write character string to file, the character string that the mustache template engine parsing module has resolved generates corresponding code file, compiles project deployment to server.
与现有技术相比,本发明的有益效果如下:Compared with the prior art, the beneficial effects of the present invention are as follows:
1.不针对特定的架构才能实现自动生成相应的代码文件;1. The corresponding code files can be automatically generated without targeting a specific architecture;
2.通过Mustache模板引擎解析可以生成前端代码和后端代码,文件类型没有局限性;2. The front-end code and back-end code can be generated through the analysis of the Mustache template engine, and the file type has no limitation;
3.自动部署到路径地址,实现代码发布一键化、智能化,简化了部署提高了效率;3. Automatically deploy to the path address, realize one-click and intelligent code release, simplify deployment and improve efficiency;
4.本方案代码生成方法是一套单独的方法,不需要在项目中增加额外的Jar包等工具;与目前市面常见的与项目整合引入外在其他Jar包的代码生成方法不同,与需求项目彻底解耦,互不影响不对原结构造成任何影响;4. The code generation method of this solution is a separate method, which does not require additional tools such as Jar packages in the project; it is different from the common code generation methods in the market that integrate with projects and introduce other external Jar packages. Complete decoupling, no mutual influence and no impact on the original structure;
5.本方法不仅适用于系统工程设计中,有关使用数据库存储信息都可以将数据内容生成指定模板输出成有效文件;通过本方法对项目结构的优势是生成代码并不变更项目结构,对开发人员来讲减少了开发人员手动拼写的错误及较大工作压力,减少了测试工作中可能经常出现的问题,大大缩短了软件的开发周期,提高了产品竞争力。5. This method is not only applicable to system engineering design, but also can generate a specified template for data content and output it into a valid file related to the use of database storage information; the advantage of this method for project structure is that the generated code does not change the project structure, and it is beneficial to developers In terms of reducing manual spelling errors and greater work pressure of developers, reducing possible frequent problems in testing work, greatly shortening the software development cycle, and improving product competitiveness.
附图说明Description of drawings
附图用来提供对本发明的进一步理解,并且构成说明书的一部分,与本发明的实施例一起用于解释本发明,并不构成对本发明的限制。在附图中:The accompanying drawings are used to provide a further understanding of the present invention, and constitute a part of the description, and are used together with the embodiments of the present invention to explain the present invention, and do not constitute a limitation to the present invention. In the attached picture:
图1是本发明的工作流程图;Fig. 1 is a work flow chart of the present invention;
图2为本发明所述配置数据库表结构信息页面展示图;Fig. 2 is the display diagram of configuration database table structure information page of the present invention;
图3是本发明的整体结构流程图。Fig. 3 is a flow chart of the overall structure of the present invention.
具体实施方式detailed description
以下结合附图对本发明的优选实施例进行说明,应当理解,此处所描述的优选实施例仅用于说明和解释本发明,并不用于限定本发明。其中附图中相同的标号全部指的是相同的部件。The preferred embodiments of the present invention will be described below in conjunction with the accompanying drawings. It should be understood that the preferred embodiments described here are only used to illustrate and explain the present invention, and are not intended to limit the present invention. The same reference numerals in the drawings all refer to the same components.
实施例1Example 1
如图1-3,本发明基于数据库表自动生成代码文件的方法及系统,该方法适应常用前后端代码框架开发,如图3所示,包括以下步骤:As shown in Fig. 1-3, the method and system for automatically generating code files based on database tables in the present invention, the method is suitable for the development of commonly used front-end and back-end code frameworks, as shown in Fig. 3, including the following steps:
S1:用户根据业务需要提前创建好所需数据库业务表结构信息;数据库不限于Mysql,Oracle,Pgsql等,本实例采用Mysql数据库。S1: The user creates the required database business table structure information in advance according to the business needs; the database is not limited to Mysql, Oracle, Pgsql, etc. This example uses the Mysql database.
S2:根据项目结构为业务表设计代码模板方案,包括后端所涉及的数据库持久层相关映射代码模板mapper.xml、持久层处理代码模板mapper.java、实体类BO代码模板entity.java、dto.java、业务逻辑层处理代码模板service.java、serviceImpl.java,以及前端所涉及的页面文件包含并不限于前端表单页面展示代码模板default.html,page.vue,页面逻辑代码模板api.js等前端页面代码文件。S2: Design a code template scheme for the business table according to the project structure, including the database persistence layer-related mapping code template mapper.xml involved in the backend, the persistence layer processing code template mapper.java, and the entity class BO code template entity.java, dto. Java, business logic layer processing code template service.java, serviceImpl.java, and the page files involved in the front end include but are not limited to the front-end form page display code template default.html, page.vue, page logic code template api.js and other front-end Page code file.
S2.1:项目结构中持久层相关代码模板的设计创建,模板名称mapper.xml主要包含xml的约束相应的增删改查等相关需求方法,需要的表名使用"{{tableName}}"及返回值使用"{{firstUp}}"等字段替换;模板名称mapper.java是一个Class对象结构包含与mapper.xml相应的方法,引入的Jar包结构及相关BO内容使用"{{firstDown}}"替换;S2.1: Design and creation of persistence layer-related code templates in the project structure. The template name mapper.xml mainly includes related requirements methods such as xml constraints, corresponding additions, deletions, modifications, and queries. The required table names use "{{tableName}}" and return Values are replaced by fields such as "{{firstUp}}"; the template name mapper.java is a Class object structure containing methods corresponding to mapper.xml, and the imported Jar package structure and related BO content are replaced by "{{firstDown}}" ;
S2.2:项目结构中业务逻辑层处理代码模板的设计创建,模板service.java是一个Class对象结构包含对外接口调用的相应方法,相应的入参出参以及实体DTO用模板字符串"{{firstUp}}"、"{{firstDown}}"填充;S2.2: In the project structure, the business logic layer handles the design and creation of code templates. The template service.java is a Class object structure containing the corresponding methods for external interface calls, corresponding input and output parameters, and template strings for entity DTO "{{ firstUp}}", "{{firstDown}}" fill;
S2.3:项目结构中实体类代码模板的设计创建,模板entity.java实体类BO代码文件通过代码模块S2.3: Design and creation of the entity class code template in the project structure, the template entity.java entity class BO code file passes through the code module
"{{/idColumn}}{{^idColumn}}"{{/idColumn}}{{^idColumn}}
private{{javaType}}{{prop}};private{{javaType}}{{prop}};
{{/idColumn}}{{/columns}}";{{/idColumn}}{{/columns}}";
循环出与数据库表结构一一对应的字段;Loop out the fields corresponding to the database table structure one by one;
S2.4:项目结构中前端文件模板的设计创建,页面模板文件default.html或者page.vue用"{{prop}}"替换表内变量值。列如某个"input"标签:“<inputclass="form-control input-sm"id="update_{{prop}}"name="{{prop}}"placeholder="{{label}}"type="text"data-bv-checkUnsafeLetters="true"required="required"/>”中的字段内容,前端逻辑JS文件api.js也类似。S2.4: The design and creation of the front-end file template in the project structure, the page template file default.html or page.vue replaces the variable value in the table with "{{prop}}". For example, an "input" label: "<inputclass="form-control input-sm"id="update_{{prop}}"name="{{prop}}"placeholder="{{label}}"type ="text"data-bv-checkUnsafeLetters="true"required="required"/>", the front-end logic JS file api.js is also similar.
S3:通过JdbcTemplate读取配置文件连接数据库读取表结构信息。S3: Read the configuration file through JdbcTemplate to connect to the database and read the table structure information.
S3.1:通过new JdbcTemplate()方法创建JdbcTemplate对象,传入设计好的连接池信息包含步骤S1设计好的表名信息以及数据库的链接地址:"jdbc:mysql://127.0.0.1:3360/xxx?useUnicode=true&characterEncoding=UTF-8"等内容;S3.1: Create a JdbcTemplate object through the new JdbcTemplate() method, and pass in the designed connection pool information including the table name information designed in step S1 and the link address of the database: "jdbc:mysql://127.0.0.1:3360/ xxx?useUnicode=true&characterEncoding=UTF-8" and so on;
S3.2:编写好查询表结构信息语句"SELECT column_name`name`,column_comment`label`,data_type`type`,COLUMN_KEY`key`,EXTRA FROMinformation_schema.columnsWHERE table_name=?and table_schema=?"S3.2: Prepare the query table structure information statement "SELECT column_name`name`,column_comment`label`,data_type`type`,COLUMN_KEY`key`,EXTRA FROMinformation_schema.columnsWHERE table_name=?and table_schema=?"
S3.3:使用现有JdbcTemplate对象通过query方法执行查询语句数据库的操作,根据表约束查询出表结构的详细信息包含业务表中字段列表包括列名、详细说明、物理类型、Java类型、Java属性名称、主键等存放在相关存储List集合列表中为后续步骤S4的使用做准备。S3.3: Use the existing JdbcTemplate object to execute the query statement database operation through the query method, and query the detailed information of the table structure according to the table constraints, including the list of fields in the business table, including column names, detailed descriptions, physical types, Java types, and Java properties The name, primary key, etc. are stored in the related storage List collection list to prepare for the use of the subsequent step S4.
S4:通过Mustache模板替换模板内容生成所需代码,将生成的代码输出到对应的路径下更新发布。S4: Generate the required code by replacing the template content with the Mustache template, and output the generated code to the corresponding path for update and release.
S4.1:拆解模板字符串并分类。当选择生成代码时,程序会分顺序读取S2中已经创建好的模板,在模板字符串中使用关键变量或使用遍历、if判断的地方一定是使用"{{"和"}}"包裹着的列如一串表单"form"模板字符串:"<form class="form-horizontal bv-form"id="insert-form"method="post">{{#columns}}<divclass="form-group"><label class="col-lg-2 control-label"><font color="red">*</font>{{label}}</label><div class="col-lg-8"><input class="form-control input-sm"id="update_{{prop}}"name="{{prop}}placeholder="{{label}}"type="text"data-bv-checkUnsafeLetters="true"required="required"/></div></div>{{/columns}}</form>",普通字符串都是在“{{”的左边,因此可以通过查找“{{”的位置来找到普通字符串,然后进行截取。找到“}}”的位置,然后截取循环直到没有字符串可截取了,如此截取后模板字符串就变成了需要的页面代码;S4.1: Disassemble template strings and classify them. When choosing to generate code, the program will read the templates that have been created in S2 in order, and the places where key variables are used in template strings or traversal and if judgments are used must be wrapped with "{{" and "}}" The columns are like a string of form "form" template strings: "<form class="form-horizontal bv-form"id="insert-form"method="post">{{#columns}}<divclass="form- group"><label class="col-lg-2 control-label"><font color="red">*</font>{{label}}</label><div class="col-lg-8 "><input class="form-control input-sm"id="update_{{prop}}"name="{{prop}}placeholder="{{label}}"type="text"data-bv- checkUnsafeLetters="true"required="required"/></div></div>{{/columns}}</form>", ordinary strings are on the left side of "{{", so you can find " {{" to find the normal string, and then intercept. Find the position of "}}", and then intercept the loop until there is no string to intercept, so that the template string becomes the required page code after interception;
S4.2:将字符串模板转换成tokens数组。扫描器接收模板内容作其构造的参数,依靠Mustache内部两个扫描器方法scan和scanUntil,scan方法的作用是使指针跳过指定标记,scanUtil方法的作用是一个一个字符遍历寻找指定标记,循环递归方法获取开始分隔符与结束分隔符之间的字符串以及分隔符之前和之后的字符串用"#"、"/"等相关字符重新组装,并把将字符串返回;S4.2: Convert the string template into an array of tokens. The scanner receives the template content as its construction parameter, relying on the two scanner methods scan and scanUntil inside Mustache, the function of the scan method is to make the pointer skip the specified mark, and the function of the scanUtil method is to traverse characters one by one to find the specified mark, loop recursion The method obtains the string between the start delimiter and the end delimiter, as well as the string before and after the delimiter, reassembles it with "#", "/" and other related characters, and returns the string;
S4.3:分组组装tokens数组。遇到"#"字符就将当前token推入进栈数组中,并将结果收集器设置为刚入栈的token的子集,遇到"/"字符就将栈数组中最新入栈的那个移除掉,并将结果收集器设置为栈数组中栈顶那个token的子集,或者是最终的结构集,如果不是"#"、"/"字符则直接将当前这个token添加进结果集中,返回组装好的tokens数组;S4.3: Group and assemble tokens array. When a "#" character is encountered, the current token is pushed into the stack array, and the result collector is set as a subset of the token that was just pushed into the stack. When a "/" character is encountered, the latest token in the stack array is moved to the stack. Remove it, and set the result collector as a subset of the token at the top of the stack in the stack array, or the final structure set. If it is not a "#" or "/" character, directly add the current token into the result set and return The assembled tokens array;
S4.4:根据tokens将模板字符串渲染成代码文件。将tokens循环,普通字符串,直接拼接处理。如果是子父对象属性访问对象属性使用lookup方法以字符串的形式动态的访问对象上深层的属性,解析字符串模板中的循环得出最终生成好字符串最终渲染成为代码文件。S4.4: Render template strings into code files according to tokens. Loop tokens, ordinary strings, and directly concatenate them. If the property of the parent object is used to access the property of the object, use the lookup method to dynamically access the deep properties of the object in the form of a string, parse the loop in the string template, and finally generate a good string and finally render it as a code file.
S5:将代码文件输出到指定目录,重新发布代码进行编译部署。S5: Output the code file to the specified directory, republish the code for compilation and deployment.
最后应说明的是:以上所述仅为本发明的优选实施例而已,并不用于限制本发明,尽管参照前述实施例对本发明进行了详细的说明,对于本领域的技术人员来说,其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换。凡在本发明的精神和原则之内,所作的任何修改、等同替换、改进等,均应包含在本发明的保护范围之内。Finally, it should be noted that: the above is only a preferred embodiment of the present invention, and is not intended to limit the present invention. Although the present invention has been described in detail with reference to the foregoing embodiments, for those skilled in the art, it still The technical solutions recorded in the foregoing embodiments may be modified, or some technical features thereof may be equivalently replaced. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention shall be included within the protection scope of the present invention.
Claims (6)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211334666.1A CN115562642A (en) | 2022-10-28 | 2022-10-28 | Code generating method based on database and Mustache template engine |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211334666.1A CN115562642A (en) | 2022-10-28 | 2022-10-28 | Code generating method based on database and Mustache template engine |
Publications (1)
Publication Number | Publication Date |
---|---|
CN115562642A true CN115562642A (en) | 2023-01-03 |
Family
ID=84769490
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202211334666.1A Pending CN115562642A (en) | 2022-10-28 | 2022-10-28 | Code generating method based on database and Mustache template engine |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN115562642A (en) |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN117539450A (en) * | 2024-01-09 | 2024-02-09 | 云筑信息科技(成都)有限公司 | Code generation method based on WPS loading item |
CN117573622A (en) * | 2024-01-15 | 2024-02-20 | 北京持安科技有限公司 | Method and device for automatically cleaning abandoned interfaces in front-end projects |
CN118250306A (en) * | 2024-04-02 | 2024-06-25 | 四川蓉电科技发展有限公司 | Panoramic visualization method of computer room based on VUE |
-
2022
- 2022-10-28 CN CN202211334666.1A patent/CN115562642A/en active Pending
Cited By (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN117539450A (en) * | 2024-01-09 | 2024-02-09 | 云筑信息科技(成都)有限公司 | Code generation method based on WPS loading item |
CN117539450B (en) * | 2024-01-09 | 2024-05-10 | 云筑信息科技(成都)有限公司 | Code generation method based on WPS loading item |
CN117573622A (en) * | 2024-01-15 | 2024-02-20 | 北京持安科技有限公司 | Method and device for automatically cleaning abandoned interfaces in front-end projects |
CN117573622B (en) * | 2024-01-15 | 2024-04-09 | 北京持安科技有限公司 | Method and device for automatically cleaning abandoned interfaces in front-end projects |
CN118250306A (en) * | 2024-04-02 | 2024-06-25 | 四川蓉电科技发展有限公司 | Panoramic visualization method of computer room based on VUE |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN115562642A (en) | Code generating method based on database and Mustache template engine | |
Huck et al. | Jedi: Extracting and synthesizing information from the web | |
US9804837B2 (en) | System and method for creating, managing, and reusing schema type definitions in services oriented architecture services, grouped in the form of libraries | |
US7890479B2 (en) | Efficient XML schema validation of XML fragments using annotated automaton encoding | |
US8103705B2 (en) | System and method for storing text annotations with associated type information in a structured data store | |
US7975220B2 (en) | Apparatus, program product and method for structured document management | |
US7627589B2 (en) | High performance XML storage retrieval system and method | |
US8397157B2 (en) | Context-free grammar | |
CN107391153B (en) | Code generation method and device based on Spring and MyBatis framework integration | |
JPH0512095A (en) | Means and method for encoding, storing, and restoring hierarchical structure data processing information for computer system | |
WO2005029222A2 (en) | Method and system for the specification of interface definitions and business rules | |
US20090077009A1 (en) | System and method for storage, management and automatic indexing of structured documents | |
GB2423387A (en) | Application-Generic Sequence Diagram Generator Driven by a Non-Proprietary Language | |
US20060122973A1 (en) | Mechanism for defining queries in terms of data objects | |
US11768834B1 (en) | Storing and querying general data type documents in SQL relational databases | |
US20100153430A1 (en) | Method of and Apparatus for Extraction and Analysis of Macro Operations within Query Language Statement | |
CN115202626A (en) | Low-code front-end development method supporting multi-technology stack components | |
CN113312373A (en) | Method and equipment for analyzing data structured query statement | |
CN113704575A (en) | SQL method, device, equipment and storage medium for analyzing XML and Java files | |
Panchenko et al. | Precise and scalable querying of syntactical source code patterns using sample code snippets and a database | |
CN114816356A (en) | System and method for generating HTTP request code based on interface document | |
Ilk et al. | Semantic enrichment process: An approach to software component reuse in modernizing enterprise systems | |
US20090307187A1 (en) | Tree automata based methods for obtaining answers to queries of semi-structured data stored in a database environment | |
Kosanović et al. | Applang–A DSL for specification of mobile applications for android platform based on textX | |
Škrbić et al. | Bibliographic records editor in XML native environment |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |