【java】mybatis-plus代码生成

正常的代码生成这里就不介绍了。旨在记录实现如下功能:

分布式微服务环境下,生成的entity、dto、vo、feignClient等等api模块,需要和mapper、service、controller等等分在不同的目录生成。

为什么会出现这个需求?

mybatis-plus(3.5.3.1)提供的全局配置:outputDir,该方法是entity、mapper、service、controller的文件输出目录。

如上,需要将entity和其他三个区分到不同的目录。

查看源码com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine

java 复制代码
/**
	 * 输出实体文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputEntity(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出Mapper文件(含xml)
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputMapper(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出service文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputService(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出controller文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputController(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

一般都会集成自定义的文件生成,使用模板,一般的选择就是

上述的4个engine也就是继承了AbstractTemplateEngine,可根据自己选择的模板,再次集成,重写这4个方法:outputEntity outputMapper outputService outputController ,方法内部不需要执行相关代码, 输出的逻辑放在outputCustomFile方法内即可

源码:

java 复制代码
@AllArgsConstructor
public class ByTemplateEngine extends BeetlTemplateEngine {

	private String apiOutputDir;

	private String serverOutputDir;

	@Override
	protected void outputCustomFile(List<CustomFile> customFiles, TableInfo tableInfo, Map<String, Object> objectMap) {
		String packageName = String.valueOf(objectMap.get("packageName"));
		String entityName = String.valueOf(objectMap.get("entityName"));

		customFiles.forEach(customFile -> {
			String key = customFile.getFileName();
			String value = customFile.getTemplatePath();
			String outputPath = getPathInfo(OutputFile.parent);
			objectMap.put("entityKey", entityNameLower);
			if (StringUtil.equals(key, "controller.java")) {
				outputPath =
					serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
						"controller" + StringPool.SLASH + entityName + "Controller" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "entity.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
					"entity" + StringPool.SLASH + entityName + "Entity" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "mapper.java")) {
				outputPath =
					serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
						"mapper" + StringPool.SLASH + entityName + "Mapper" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "mapper.xml")) {
				outputPath =
					serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
						"mapper" + StringPool.SLASH + entityName + "Mapper" + StringPool.DOT_XML;
			}
			if (StringUtil.equals(key, "service.java")) {
				outputPath =
					serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
						"service" + StringPool.SLASH + "I" + entityName + "Service" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "serviceImpl.java")) {
				outputPath =
					serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
						"service" + StringPool.SLASH + "impl" + StringPool.SLASH + entityName + "ServiceImpl" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "entityVO.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
					"vo" + StringPool.SLASH + entityName + "VO" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "entityDTO.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
					"dto" + StringPool.SLASH + entityName + "DTO" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "entityExcel.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
					"excel" + StringPool.SLASH + entityName + "Excel" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "wrapper.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
					"wrapper" + StringPool.SLASH + entityName + "Wrapper" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "feign.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
					"feign" + StringPool.SLASH + "I" + entityName + "Client" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "feignclient.java")) {
				outputPath =
					serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "feign"
						+ StringPool.SLASH + entityName + "Client" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "feignclientFallback.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH +
					"feign" + StringPool.SLASH + entityName + "ClientFallback" + StringPool.DOT_JAVA;
			}
			outputFile(new File(String.valueOf(outputPath)), objectMap, value, Boolean.TRUE);
		});
	}

	/**
	 * 输出实体文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputEntity(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出Mapper文件(含xml)
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputMapper(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出service文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputService(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出controller文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputController(TableInfo tableInfo, Map<String, Object> objectMap) {
	}
}
相关推荐
七星静香19 分钟前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员20 分钟前
java导出word文件(手绘)
java·开发语言·word
ZHOUPUYU20 分钟前
IntelliJ IDEA超详细下载安装教程(附安装包)
java·ide·intellij-idea
stewie624 分钟前
在IDEA中使用Git
java·git
Elaine20239139 分钟前
06 网络编程基础
java·网络
G丶AEOM41 分钟前
分布式——BASE理论
java·分布式·八股
落落鱼201341 分钟前
tp接口 入口文件 500 错误原因
java·开发语言
想要打 Acm 的小周同学呀42 分钟前
LRU缓存算法
java·算法·缓存
镰刀出海1 小时前
Recyclerview缓存原理
java·开发语言·缓存·recyclerview·android面试
阿伟*rui3 小时前
配置管理,雪崩问题分析,sentinel的使用
java·spring boot·sentinel