feat: 生成 Java、Kotlin 两种代码。

This commit is contained in:
Suomm 2024-06-04 11:13:34 +08:00
parent b5a1ee6521
commit 9bde89f428
6 changed files with 22 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,7 +59,7 @@ public class ControllerGenerator implements IGenerator {
String controllerPackagePath = packageConfig.getControllerPackage().replace(".", "/"); String controllerPackagePath = packageConfig.getControllerPackage().replace(".", "/");
File controllerJavaFile = new File(sourceDir, controllerPackagePath + "/" + File controllerJavaFile = new File(sourceDir, controllerPackagePath + "/" +
table.buildControllerClassName() + ".java"); table.buildControllerClassName() + globalConfig.getFileType());
if (controllerJavaFile.exists() && !controllerConfig.isOverwriteEnable()) { if (controllerJavaFile.exists() && !controllerConfig.isOverwriteEnable()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -55,10 +55,10 @@ public class EntityGenerator implements IGenerator {
return; return;
} }
//生成 entity // 生成 entity
genEntityClass(table, globalConfig); genEntityClass(table, globalConfig);
//生成 base // 生成 base
genBaseClass(table, globalConfig); genBaseClass(table, globalConfig);
} }
@ -72,12 +72,12 @@ public class EntityGenerator implements IGenerator {
String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/"); String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/");
String entityClassName = table.buildEntityClassName(); String entityClassName = table.buildEntityClassName();
File entityJavaFile = new File(sourceDir, entityPackagePath + "/" + entityClassName + ".java"); File entityJavaFile = new File(sourceDir, entityPackagePath + "/" + entityClassName + globalConfig.getFileType());
if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) { if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) {
return; return;
} }
//排除忽略列 // 排除忽略列
if (globalConfig.getStrategyConfig().getIgnoreColumns() != null) { if (globalConfig.getStrategyConfig().getIgnoreColumns() != null) {
table.getColumns().removeIf(column -> globalConfig.getStrategyConfig().getIgnoreColumns().contains(column.getName().toLowerCase())); table.getColumns().removeIf(column -> globalConfig.getStrategyConfig().getIgnoreColumns().contains(column.getName().toLowerCase()));
} }
@ -95,8 +95,12 @@ public class EntityGenerator implements IGenerator {
String templatePath = this.templatePath; String templatePath = this.templatePath;
//开启生成 baseClass // 开启生成 baseClass
if (entityConfig.isWithBaseClassEnable()) { if (entityConfig.isWithBaseClassEnable()) {
if (globalConfig.getFileType() == GlobalConfig.FileType.KOTLIN) {
throw new UnsupportedOperationException("暂不支持 Kotlin 生成 WithBaseClass 模式。");
}
templatePath = this.entityWithBaseTemplatePath; templatePath = this.entityWithBaseTemplatePath;
String baseClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix(); String baseClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix();
@ -118,7 +122,7 @@ public class EntityGenerator implements IGenerator {
protected void genBaseClass(Table table, GlobalConfig globalConfig) { protected void genBaseClass(Table table, GlobalConfig globalConfig) {
EntityConfig entityConfig = globalConfig.getEntityConfig(); EntityConfig entityConfig = globalConfig.getEntityConfig();
//不需要生成 baseClass // 不需要生成 baseClass
if (!entityConfig.isWithBaseClassEnable()) { if (!entityConfig.isWithBaseClassEnable()) {
return; return;
} }
@ -135,7 +139,7 @@ public class EntityGenerator implements IGenerator {
File baseEntityJavaFile = new File(sourceDir, baseEntityPackagePath + "/" + baseEntityClassName + ".java"); File baseEntityJavaFile = new File(sourceDir, baseEntityPackagePath + "/" + baseEntityClassName + ".java");
//排除忽略列 // 排除忽略列
if (globalConfig.getStrategyConfig().getIgnoreColumns() != null) { if (globalConfig.getStrategyConfig().getIgnoreColumns() != null) {
table.getColumns().removeIf(column -> globalConfig.getStrategyConfig().getIgnoreColumns().contains(column.getName().toLowerCase())); table.getColumns().removeIf(column -> globalConfig.getStrategyConfig().getIgnoreColumns().contains(column.getName().toLowerCase()));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,7 +59,7 @@ public class MapperGenerator implements IGenerator {
String mapperPackagePath = packageConfig.getMapperPackage().replace(".", "/"); String mapperPackagePath = packageConfig.getMapperPackage().replace(".", "/");
File mapperJavaFile = new File(sourceDir, mapperPackagePath + "/" + File mapperJavaFile = new File(sourceDir, mapperPackagePath + "/" +
table.buildMapperClassName() + ".java"); table.buildMapperClassName() + globalConfig.getFileType());
if (mapperJavaFile.exists() && !mapperConfig.isOverwriteEnable()) { if (mapperJavaFile.exists() && !mapperConfig.isOverwriteEnable()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,7 +59,7 @@ public class ServiceGenerator implements IGenerator {
String servicePackagePath = packageConfig.getServicePackage().replace(".", "/"); String servicePackagePath = packageConfig.getServicePackage().replace(".", "/");
File serviceJavaFile = new File(sourceDir, servicePackagePath + "/" + File serviceJavaFile = new File(sourceDir, servicePackagePath + "/" +
table.buildServiceClassName() + ".java"); table.buildServiceClassName() + globalConfig.getFileType());
if (serviceJavaFile.exists() && !serviceConfig.isOverwriteEnable()) { if (serviceJavaFile.exists() && !serviceConfig.isOverwriteEnable()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,7 +59,7 @@ public class ServiceImplGenerator implements IGenerator {
String serviceImplPackagePath = packageConfig.getServiceImplPackage().replace(".", "/"); String serviceImplPackagePath = packageConfig.getServiceImplPackage().replace(".", "/");
File serviceImplJavaFile = new File(sourceDir, serviceImplPackagePath + "/" + File serviceImplJavaFile = new File(sourceDir, serviceImplPackagePath + "/" +
table.buildServiceImplClassName() + ".java"); table.buildServiceImplClassName() + globalConfig.getFileType());
if (serviceImplJavaFile.exists() && !serviceImplConfig.isOverwriteEnable()) { if (serviceImplJavaFile.exists() && !serviceImplConfig.isOverwriteEnable()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,7 +59,7 @@ public class TableDefGenerator implements IGenerator {
String tableDefPackagePath = packageConfig.getTableDefPackage().replace(".", "/"); String tableDefPackagePath = packageConfig.getTableDefPackage().replace(".", "/");
File tableDefJavaFile = new File(sourceDir, tableDefPackagePath + "/" + File tableDefJavaFile = new File(sourceDir, tableDefPackagePath + "/" +
table.buildTableDefClassName() + ".java"); table.buildTableDefClassName() + globalConfig.getFileType());
if (tableDefJavaFile.exists() && !tableDefConfig.isOverwriteEnable()) { if (tableDefJavaFile.exists() && !tableDefConfig.isOverwriteEnable()) {