该项目的目的是目前在工作中,碰到了越来越多的可重复性代码,每次针对一个新的需求时,都需要重新手写一遍(service\controller\vo\dto等),极其繁琐,因此开发了本项目
template-factory
是一个maven plugin
组件,开发理念是采用模板组
+模板Render
组件的模式进行开发,模板引擎使用的是国产JFinal中的Enjoy.
template-factory
中,每一个生成的目标文件,都需要一个Render组件进行包装及渲染,template-factory
负责将请求参数的上下文向下传递,由开发者自己实现具体的业务规则接口设计如下:
public interface TemplateFactory {
/**
* 当前Factory的别名,业务组名称,唯一即可,不要和内置的Factory重复
* @return
*/
String getAliasName();
/**
* 获取当前模板的Render实例
* @return
*/
Map<String, TemplateFactoryRender> getRenders();
/**
* 调用本Factory下的Render组件进行输出
* @param templateContext
*/
void apply(TemplateContext templateContext);
}
接口设计如下:
public interface TemplateFactoryRender {
/**
* 构造当前模板的属性
* @return
*/
Map<String,Object> 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
为例,代码如下:
/***
*
* @since:template-factory 1.0
* @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a>
* 2020/06/07 17:28
*/
public class AideStoreServiceTemplateRender extends AbstractTemplateRender{
Logger logger= LoggerFactory.getLogger(AideStoreServiceTemplateRender.class);
@Override
public Map<String, Object> buildKeys(TemplateContext templateContext, TemplateConfiguration templateConfiguration) {
Map<String,Object> 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));
}
}
首先,在Maven项目中添加plugin组件依赖,配置如下:
<plugin>
<groupId>com.github.xiaoymin</groupId>
<artifactId>template-factory-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<!--template-factory默认的配置文件是在resouces目录下,如果你讲配置文件放在某个文件夹里,则需要在此配置xml的具体路径-->
<configurationFile>src/main/resources/templateFactoryConfig.xml</configurationFile>
</configuration>
</plugin>
参数说明:
template-factory
插件的配置文件路径,如果你讲该文件存放在src/main/resources
目录下,且文件名称为templateFactoryConfig.xml
,则该参数可以不用配置,否则你需要指定具体的路径templateFactoryConfig.xml
完整配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<templateFactoryConfiguration>
<!-- JDBC路径 -->
<jdbcPathLocation>D:\Users\xiaoymin\.m2\repository\org\mariadb\jdbc\mariadb-java-client\2.4.2\mariadb-java-client-2.4.2.jar</jdbcPathLocation>
<!--项目、开发者信息,这些信息会配置在生成的java文件中去,如果你不配置该属性,则默认是templateFactory的作者(xiaoymin@foxmail.com)-->
<developerConfiguration name="xiaoymin" email="xiaoymin7@foxmail.com"></developerConfiguration>
<context id="test">
<!-- 数据库JDBC配置 -->
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="org.mariadb.jdbc.Driver" connectionURL="jdbc:mariadb://localhost:3306/knife4j_admin" userId="root" password="123456">
</jdbcConnection>
<!--模板输出-->
<templateFactory module="用户" tableName="admin_user" alias="AideStoreFactory" type="">
<template alias="add" domainObjectName="AdminUserAddVo" packageName="com.example.demo"></template>
<template alias="update" domainObjectName="AdminUserUpdateVo" packageName="com.example.demo"></template>
<template alias="search" domainObjectName="AdminUserSearchVo" packageName="com.example.demo"></template>
<template alias="dto" domainObjectName="AdminUserDTO" packageName="com.example.demo"></template>
<template alias="service" domainObjectName="AdminUser" mapperName="User" packageName="com.example.demo"></template>
<template alias="serviceImpl" domainObjectName="AdminUser" mapperName="User" packageName="com.example.demo"></template>
<template alias="controller" domainObjectName="AdminUser" module="用户信息" api="/admin/product" packageName="com.example.demo"></template>
</templateFactory>
</context>
</templateFactoryConfiguration>
jdbcPathLocation
当前数据库的驱动Jar包路径,必须写完整路径
developerConfiguration
开发者详细,如果模板是生成Java文件,在请求上下文中,你可以根据此来构建输出到.java文件中的开发者信息中去
主要包含的参数:
参数 | 说明 |
---|---|
name | 开发者名称 |
开发者邮箱地址 |
context
context是配置中具体需要进行模板输出的一组,你可以配置多个,针对不同的数据源来生成
包含的属性如下:
jdbcConnection
数据库连接的基础配置信息,主要包括:
templateFactory
该接口是由用户自行配置模板,可以配置多个,一般是以数据库表为基础,当然,tableName属性并不是必须,如果你的模板输出并不需要使用到数据库,那么可以不用配置上面的jdbcConnection的数据库配置
template-factory
已经内置实现的factory满足你的要求,那么,你可以使用,否则需要自行实现,自定义即可template-factory
中内置实现的,该参数可以不填template
template节点是一个模板组下的真正的模板配置,属性如下:
目前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接口 |
如果你在工作中碰见很多重复性的代码而又懒得写,不妨将你的代码通过PR提交上来,一般一周一个版本提交到Maven中央仓库
在以后的工作中,高效的写代码,快乐的工作和生活~~!
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。