需求
当前端新增一个第三方接口的时候,后端要对应的生成一个新的controller/生成一个新的模块新的controller
解决办法
使用mybatis-plus的代码生成工具进行实现
mybatis plus 代码生成
我们只需要通过填空的方式去配置数据源(DataSource),全局配置(GlobalConfig),包配置(PackageConfig),策略配置(StrategyConfig)和模板引擎配置(TemplateEngine)即可。
官方文档上也给出了对于每一个配置我们可以进行什么操作。
- 代码如下
//1、配置数据源
FastAutoGenerator.create("url", "username", "password")
//2、全局配置
.globalConfig(...)
//3、包配置
.packageConfig(...)
//4、策略配置
.strategyConfig(...)
//5、模板引擎配置
.templateEngine(...)
//6、执行
.execute();
具体实战
只需要生成controller,service,vo,就算去掉mapper,实体,也会默认自动生成,使用FileSystemUtils.deleteRecursively 方法删除文件夹
- 核心代码如下
//1、配置数据源
FastAutoGenerator.create(url, username, password)
//2、全局配置
.globalConfig((builder) -> {
builder.author(props.getProperty("author"))
.dateType(DateType.TIME_PACK)
.enableSwagger()
.outputDir(this.getOutputDir())
.disableOpenDir();
})
//3、包配置
.packageConfig((builder) -> {
builder.parent(this.packageName)
.controller("controller")
.service("service")
.serviceImpl("service.impl");
})
//4、策略配置
.strategyConfig((builder) -> {
builder.addTablePrefix(this.tablePrefix)
.addInclude(this.includeTables)
.controllerBuilder()
.formatFileName("%sController")
.enableRestStyle()
.enableHyphenStyle()
.fileOverride();
})
//5、模板引擎配置
.templateConfig((builder) -> {
builder.disable(new TemplateType[]{TemplateType.ENTITY})
.controller("/templates/controller2.java.vm");
})
//6, 自定义注入配置
.injectionConfig((builder) -> {
builder.beforeOutputFile((tableInfo, objectMap) -> {
System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
}).customMap(customMap).customFile(customFile);
})
//7. 自定义模版支持(DTO\VO等)配置
.templateEngine(new VelocityTemplateEngine() {
@Override
protected void outputCustomFile(Map<String, String> customFile, TableInfo tableInfo, Map<String, Object> objectMap) {
String entityName = tableInfo.getEntityName();
String entityNameLower = tableInfo.getEntityName().toLowerCase();
customFile.forEach((key, value) -> {
String outputPath = this.getPathInfo(OutputFile.other);
objectMap.put("entityKey", entityNameLower);
if (StringUtil.equals(key, "menu.sql")) {
objectMap.put("menuId", IdWorker.getId());
objectMap.put("addMenuId", IdWorker.getId());
objectMap.put("editMenuId", IdWorker.getId());
objectMap.put("removeMenuId", IdWorker.getId());
objectMap.put("viewMenuId", IdWorker.getId());
outputPath = NewBladeCodeGenerator.this.getOutputDir() + "/" + "sql" + "/" + entityNameLower + ".menu.sql";
}
this.outputFile(new File(String.valueOf(outputPath)), objectMap, value, Boolean.TRUE);
});
}
}).execute();
FileSystemUtils.deleteRecursively(new File(""));
模版文件 controller2.java.vm 复制以下代码,放到resources下的templates,没有的话建文件夹
把import里面的报名改成自己的
package $!{package.Controller};
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import javax.validation.Valid;
import ***包名.R;
import org.springframework.web.bind.annotation.*;
import ***包名.DataServiceVO;
import ***包名.IDataServiceApiService;
#if(!$!{superEntityClass})
#end
/**
* $!{table.comment} 控制器
*
* @author $!{author}
* @since $!{date}
*/
@RestController
@AllArgsConstructor
@RequestMapping("#if($!{hasServiceName})/$!{serviceName}#end/$!{entityKey}")
@Api(value = "$!{table.comment}", tags = "$!{table.comment}接口")
#if($!{superControllerClass})
public class $!{table.controllerName} extends $!{superControllerClass} {
#else
public class $!{table.controllerName} {
#end
private final IDataServiceApiService dataServiceApiService;
@RequestMapping("/$!{requestMappingUrl}")
@PreAuth("hasPermission('$!{preAuthName}')")
public R apiTest(@Valid @RequestBody DataServiceVO dataService) {
return R.data(dataServiceApiService.apiTest(dataService));
}
}
推荐下
国产笔记软件flowus
用着挺不错的.如果感兴趣可以体验.邀请码 4X2W8F
https://flowus.cn/login?code=4X2W8F
想参考全部原文的话可以联系我