From b00626fc16f6a8d36c28d57005d677ece2220baa Mon Sep 17 00:00:00 2001 From: lihengming <89921218@qq.com> Date: Fri, 30 Jun 2017 11:11:30 +0800 Subject: [PATCH] update > optimize CodeGenerator,simplify project config. --- .../company/project/core/ProjectConstant.java | 9 ++++-- src/test/java/CodeGenerator.java | 28 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/company/project/core/ProjectConstant.java b/src/main/java/com/company/project/core/ProjectConstant.java index 2d0ea5f..7f58cc3 100644 --- a/src/main/java/com/company/project/core/ProjectConstant.java +++ b/src/main/java/com/company/project/core/ProjectConstant.java @@ -4,8 +4,13 @@ package com.company.project.core; * 项目常量 */ public final class ProjectConstant { - public static final String BASE_PACKAGE = "com.company.project";//项目基础包名称,根据自己的公司项目修改 - public static final String MAPPER_PACKAGE = BASE_PACKAGE + ".dao";//Mapper所在包 + public static final String BASE_PACKAGE = "com.company.project";//项目基础包名称,根据自己公司的项目修改 + public static final String MODEL_PACKAGE = BASE_PACKAGE + ".model";//Model所在包 + public static final String MAPPER_PACKAGE = BASE_PACKAGE + ".dao";//Mapper所在包 + public static final String SERVICE_PACKAGE = BASE_PACKAGE + ".service";//Service所在包 + public static final String SERVICE_IMPL_PACKAGE = SERVICE_PACKAGE + ".impl";//ServiceImpl所在包 + public static final String CONTROLLER_PACKAGE = BASE_PACKAGE + ".web";//Controller所在包 + public static final String MAPPER_INTERFACE_REFERENCE = BASE_PACKAGE + ".core.Mapper";//Mapper插件基础接口的完全限定名 } diff --git a/src/test/java/CodeGenerator.java b/src/test/java/CodeGenerator.java index bf8ef6d..6d386c5 100644 --- a/src/test/java/CodeGenerator.java +++ b/src/test/java/CodeGenerator.java @@ -1,4 +1,3 @@ -import com.company.project.core.ProjectConstant; import freemarker.template.TemplateExceptionHandler; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.*; @@ -10,6 +9,8 @@ import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; +import static com.company.project.core.ProjectConstant.*; + /** * 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。 */ @@ -26,10 +27,9 @@ public class CodeGenerator { private static final String JAVA_PATH = "\\src\\main\\java"; //java文件路径 private static final String RESOURCES_PATH = "\\src\\main\\resources";//资源文件路径 - private static final String BASE_PACKAGE_PATH = "\\com\\company\\project";//项目基础包路径 - private static final String PACKAGE_PATH_SERVICE = BASE_PACKAGE_PATH + "\\service\\";//生成的Service存放路径 - private static final String PACKAGE_PATH_SERVICE_IMPL = BASE_PACKAGE_PATH + "\\service\\impl\\";//生成的Service实现存放路径 - private static final String PACKAGE_PATH_CONTROLLER = BASE_PACKAGE_PATH + "\\web\\";//生成的Controller实现存放路径 + private static final String PACKAGE_PATH_SERVICE = packageConvertPath(SERVICE_PACKAGE);//生成的Service存放路径 + private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE);//生成的Service实现存放路径 + private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE);//生成的Controller存放路径 private static final String AUTHOR = "CodeGenerator";//@author private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//@date @@ -63,12 +63,12 @@ public class CodeGenerator { PluginConfiguration pluginConfiguration = new PluginConfiguration(); pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin"); - pluginConfiguration.addProperty("mappers", ProjectConstant.MAPPER_INTERFACE_REFERENCE); + pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE); context.addPluginConfiguration(pluginConfiguration); JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration(); javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH); - javaModelGeneratorConfiguration.setTargetPackage(ProjectConstant.MODEL_PACKAGE); + javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE); context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration); SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration(); @@ -78,7 +78,7 @@ public class CodeGenerator { JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration(); javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH); - javaClientGeneratorConfiguration.setTargetPackage(ProjectConstant.MAPPER_PACKAGE); + javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE); javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER"); context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration); @@ -106,6 +106,11 @@ public class CodeGenerator { if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) { throw new RuntimeException("生成Model和Mapper失败:" + warnings); } + + String modelName = tableNameConvertUpperCamel(tableName); + System.out.println(modelName + ".java 生成成功"); + System.out.println(modelName + "Mapper.java 生成成功"); + System.out.println(modelName + "Mapper.xml 生成成功"); } public static void genService(String tableName) { @@ -118,7 +123,7 @@ public class CodeGenerator { String modelNameUpperCamel = tableNameConvertUpperCamel(tableName); data.put("modelNameUpperCamel", modelNameUpperCamel); data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName)); - data.put("basePackage", ProjectConstant.BASE_PACKAGE); + data.put("basePackage", BASE_PACKAGE); File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java"); if (!file.getParentFile().exists()) { @@ -151,7 +156,7 @@ public class CodeGenerator { String modelNameUpperCamel = tableNameConvertUpperCamel(tableName); data.put("modelNameUpperCamel", modelNameUpperCamel); data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName)); - data.put("basePackage", ProjectConstant.BASE_PACKAGE); + data.put("basePackage", BASE_PACKAGE); File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java"); if (!file.getParentFile().exists()) { @@ -206,4 +211,7 @@ public class CodeGenerator { return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName); } + private static String packageConvertPath(String packageName) { + return String.format("\\%s\\", packageName.contains(".") ? packageName.replaceAll("\\.", "\\\\") : packageName); + } }