【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) {
	}
}
相关推荐
桦说编程3 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen3 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研3 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
记忆不曾留4 小时前
Mybatis 源码解读-SqlSession 会话源码和Executor SQL操作执行器源码
mybatis·二级缓存·sqlsession会话·executor执行器·一级缓存localcache
没有bug.的程序员4 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋4 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国5 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~5 小时前
《设计模式》装饰模式
java·设计模式
A尘埃5 小时前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统
青云交5 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图