# template-factory **Repository Path**: dt_research_institute/template-factory ## Basic Information - **Project Name**: template-factory - **Description**: 模板工厂 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-06-07 - **Last Updated**: 2024-10-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # template-factory ## 1.背景 该项目的目的是目前在工作中,碰到了越来越多的可重复性代码,每次针对一个新的需求时,都需要重新手写一遍(service\controller\vo\dto等),极其繁琐,因此开发了本项目 ## 2.介绍 `template-factory`是一个`maven plugin`组件,开发理念是采用`模板组`+`模板Render`组件的模式进行开发,模板引擎使用的是国产JFinal中的[Enjoy](https://jfinal.com/doc/6-1). - 模板组:每个人工作中碰到的重复性工作(CRUD)不一样,因此模板组实际上是我们在工作中对于某一特定领域中,繁琐的重复性代码,我们归为一组,组成我们的模板组,属于业务归类 - 模板Render:在`template-factory`中,每一个生成的目标文件,都需要一个Render组件进行包装及渲染,`template-factory`负责将请求参数的上下文向下传递,由开发者自己实现具体的业务规则 ### 2.1模板组顶级接口设计 接口设计如下: ```java public interface TemplateFactory { /** * 当前Factory的别名,业务组名称,唯一即可,不要和内置的Factory重复 * @return */ String getAliasName(); /** * 获取当前模板的Render实例 * @return */ Map getRenders(); /** * 调用本Factory下的Render组件进行输出 * @param templateContext */ void apply(TemplateContext templateContext); } ``` ### 2.2模板Render顶级接口设计 接口设计如下: ```java public interface TemplateFactoryRender { /** * 构造当前模板的属性 * @return */ Map buildKeys(TemplateContext templateContext, TemplateConfiguration templateConfiguration); /** * 构造输出目录路径 * @param templateContext * @param templateConfiguration * @return */ String buildTargetPath(TemplateContext templateContext, TemplateConfiguration templateConfiguration); /** * 渲染 * @param templateContext * @param templateConfiguration */ void render(TemplateContext templateContext, TemplateConfiguration templateConfiguration); } ``` 每一个模板文件(开发者自行扩展的情况下)的输出都需要开发者实现该接口,方法说明如下: - `buildKeys`:主要用于构建模板引擎`Enjoy`的数据参数,构建该参数是为了在使用Enjoy渲染输出时进行参数的替换 - `buildTargetPath`:构造模板的输出路径 - `render`: 开发者实现该接口,获取模板Template对象,调用`Enjoy`引擎的方法进行模板输出 以内置的`AideStoreServiceTemplateRender`为例,代码如下: ```java /*** * * @since template-factory 1.0 * @author xiaoymin@foxmail.com * 2020/06/07 17:28 */ public class AideStoreServiceTemplateRender extends AbstractTemplateRender{ Logger logger= LoggerFactory.getLogger(AideStoreServiceTemplateRender.class); @Override public Map buildKeys(TemplateContext templateContext, TemplateConfiguration templateConfiguration) { Map map=new HashMap<>(); map.put("packageName",templateConfiguration.getPackageName()); map.put("modelName",templateConfiguration.getDomainObjectName()); map.put("modelArgumentName",StrUtil.lowerFirst(templateConfiguration.getDomainObjectName())); String description="业务Service"; //开发者信息 map.put("email",templateContext.getConfiguration().getDeveloperConfiguration().getEmail()); map.put("projectName",templateContext.getConfiguration().getMavenProject().getName()); map.put("projectVersion",templateContext.getConfiguration().getMavenProject().getVersion()); map.put("description",description); map.put("currentTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"))); return map; } @Override public String buildTargetPath(TemplateContext templateContext, TemplateConfiguration templateConfiguration) { return getDefaultPathByType(templateContext,templateConfiguration,"service",templateConfiguration.getDomainObjectName()+"Service.java"); } @Override public void render(TemplateContext templateContext, TemplateConfiguration templateConfiguration) { ISource source=new TemplateDataSource(true,templateConfiguration.getPath()); Template template=getEngine().getTemplate(source); template.render(this.buildKeys(templateContext,templateConfiguration),buildTargetPath(templateContext,templateConfiguration)); } } ``` ## 3.使用 首先,在Maven项目中添加plugin组件依赖,配置如下: ```xml com.github.xiaoymin template-factory-maven-plugin 1.0 src/main/resources/templateFactoryConfig.xml ``` 参数说明: - configurationFile:`template-factory`插件的配置文件路径,如果你讲该文件存放在`src/main/resources`目录下,且文件名称为`templateFactoryConfig.xml`,则该参数可以不用配置,否则你需要指定具体的路径 ## 4.配置文件 `templateFactoryConfig.xml`完整配置文件如下: ```xml D:\Users\xiaoymin\.m2\repository\org\mariadb\jdbc\mariadb-java-client\2.4.2\mariadb-java-client-2.4.2.jar ``` ### 4.1参数说明 **jdbcPathLocation** 当前数据库的驱动Jar包路径,必须写完整路径 **developerConfiguration** 开发者详细,如果模板是生成Java文件,在请求上下文中,你可以根据此来构建输出到.java文件中的开发者信息中去 主要包含的参数: |参数|说明| |---|----| |name|开发者名称| |email|开发者邮箱地址| **context** context是配置中具体需要进行模板输出的一组,你可以配置多个,针对不同的数据源来生成 包含的属性如下: **jdbcConnection** 数据库连接的基础配置信息,主要包括: - driverClass:当前数据库的驱动类 - connectionURL:目标数据库的连接地址 - userId:数据库访问用户名 - password:数据库访问密码 **templateFactory** 该接口是由用户自行配置模板,可以配置多个,一般是以数据库表为基础,当然,tableName属性并不是必须,如果你的模板输出并不需要使用到数据库,那么可以不用配置上面的jdbcConnection的数据库配置 - tableName:数据库表名 - factoryAlias:模板组别名,如果`template-factory`已经内置实现的factory满足你的要求,那么,你可以使用,否则需要自行实现,自定义即可 - factoryType:模板组的具体类名,开发者自行实现的需要填写完整类的路径,如果是已经在`template-factory`中内置实现的,该参数可以不填 **template** template节点是一个模板组下的真正的模板配置,属性如下: - alias:当前模板Render的别名,仅仅在该Factory组下不重名即可 - domainObjectName:实体类名称 - packageName:包路径 ### 4.2内置模板组及Render 目前`template-factory`仅仅实现了作者在工作中常见的重复性代码,目前主要实现如下: **TemplateFactory模板组** | 别名 | 类名 | | ---------------- | ------------------------------------------------------------ | | AideStoreFactory | `com.github.xiaoymin.template.factory.impl.AideStoreTemplateFactory` | **TemplateRender实现** | 别名 | 类名 | 说明 | | --------- | -------------------------------- | ------------------------------------------ | | `add` | `AideStoreAddTemplateRender` | 生成Swagger模板,主要用于新增数据库记录的Vo | | `search` | `AideStoreSearchTemplateRender` | 分页实体Vo,用于模糊查询与前端交互 | | `update` | `AideStoreUpdateTemplateRender` | 更新数据库记录的Vo | | `dto` | `AideStoreDTOTemplateRender` | 返回数据的DTO | | `service` | `AideStoreServiceTemplateRender` | 业务Service接口 | ## 5.1最后 如果你在工作中碰见很多重复性的代码而又懒得写,不妨将你的代码通过PR提交上来,一般一周一个版本提交到Maven中央仓库 在以后的工作中,高效的写代码,快乐的工作和生活~~!