【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

/**
	 * 输出实体文件
	 *
	 * @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方法内即可

源码:

@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) {
	}
}

你可能感兴趣的:(java,mybatis,代码生成)