【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) {
	}
}
相关推荐
suweijie7682 小时前
SpringCloudAlibaba | Sentinel从基础到进阶
java·大数据·sentinel
公贵买其鹿3 小时前
List深拷贝后,数据还是被串改
java
xlsw_6 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹7 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭8 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫8 小时前
泛型(2)
java
超爱吃士力架8 小时前
邀请逻辑
java·linux·后端
南宫生8 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石8 小时前
12/21java基础
java
李小白668 小时前
Spring MVC(上)
java·spring·mvc