|
@@ -0,0 +1,437 @@
|
|
|
+import static com.medipath.backstage.core.ProjectConstant.BASE_PACKAGE;
|
|
|
+import static com.medipath.backstage.core.ProjectConstant.CONTROLLER_PACKAGE;
|
|
|
+import static com.medipath.backstage.core.ProjectConstant.MAPPER_INTERFACE_REFERENCE;
|
|
|
+import static com.medipath.backstage.core.ProjectConstant.MAPPER_PACKAGE;
|
|
|
+import static com.medipath.backstage.core.ProjectConstant.MODEL_PACKAGE;
|
|
|
+import static com.medipath.backstage.core.ProjectConstant.SERVICE_IMPL_PACKAGE;
|
|
|
+import static com.medipath.backstage.core.ProjectConstant.SERVICE_PACKAGE;
|
|
|
+
|
|
|
+import static com.medipath.backstage.core.ProjectConstant.SERVICE_DTO_PACKAGE;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.mybatis.generator.api.MyBatisGenerator;
|
|
|
+import org.mybatis.generator.config.*;
|
|
|
+import org.mybatis.generator.internal.DefaultShellCallback;
|
|
|
+
|
|
|
+import com.google.common.base.CaseFormat;
|
|
|
+
|
|
|
+import freemarker.template.TemplateExceptionHandler;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
|
|
|
+ */
|
|
|
+public class CodeGenerator {
|
|
|
+ // JDBC配置,请修改为你项目的实际配置
|
|
|
+ private static final String JDBC_URL = "jdbc:mysql://nj-cdb-9dj4cs2z.sql.tencentcdb.com:63883/backstage";
|
|
|
+ private static final String JDBC_USERNAME = "root";
|
|
|
+ private static final String JDBC_PASSWORD = "wukong000";
|
|
|
+ private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
|
|
|
+
|
|
|
+ private static final String APPLICATION = "backstage";
|
|
|
+
|
|
|
+ private static final String PROJECT_PATH = System.getProperty("user.dir")+"\\"+APPLICATION+"-service";// 项目在硬盘上的基础路径
|
|
|
+ private static final String PROJECT_INTERFACE_PATH = System.getProperty("user.dir")+"\\"+APPLICATION+"-interface";// 项目在硬盘上的基础路径
|
|
|
+ private static final String PROJECT_API_PATH = System.getProperty("user.dir")+"\\"+APPLICATION+"-api";// 项目在硬盘上的基础路径
|
|
|
+ private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/test/resources/generator/template";// 模板位置
|
|
|
+
|
|
|
+ private static final String JAVA_PATH = "/src/main/java"; // java文件路径
|
|
|
+ private static final String RESOURCES_PATH = "/src/main/resources";// 资源文件路径
|
|
|
+
|
|
|
+ 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 PACKAGE_PATH_DTO = packageConvertPath(SERVICE_DTO_PACKAGE);// 生成的Service实现存放路径
|
|
|
+
|
|
|
+
|
|
|
+ private static final String AUTHOR = "CodeGenerator";// @author
|
|
|
+ private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());// @date
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ //genCode("c_customer","c_customer_protocol","c_customer_address","c_user","c_user_bank_card","c_user_operator","c_bcard_list");
|
|
|
+ // genCode("输入表名","输入自定义Model名称");
|
|
|
+ genCode("m_mytest");
|
|
|
+ //genCode("c_member_coupon");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。 如输入表名称 "t_user_detail" 将生成
|
|
|
+ * TUserDetail、TUserDetailMapper、TUserDetailService ...
|
|
|
+ *
|
|
|
+ * @param tableNames 数据表名称...
|
|
|
+ */
|
|
|
+ public static void genCode(String... tableNames) {
|
|
|
+ for (String tableName : tableNames) {
|
|
|
+ // System.out.println(tableNameConvertUpperCamel(tableName.substring(2,tableName.length())));
|
|
|
+ genCode2(tableName, tableNameConvertUpperCamel(tableName.substring(2, tableName.length())));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 首字母大写
|
|
|
+ public static String captureName(String name) {
|
|
|
+ name = name.substring(0, 1).toUpperCase() + name.substring(1);
|
|
|
+ return name;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过数据表名称,和自定义的 Model 名称生成代码 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User"
|
|
|
+ * 将生成 User、UserMapper、UserService ...
|
|
|
+ *
|
|
|
+ * @param tableName 数据表名称
|
|
|
+ * @param modelName 自定义的 Model 名称
|
|
|
+ */
|
|
|
+ public static void genCode2(String tableName, String modelName) {
|
|
|
+ genModelAndMapper(tableName, modelName);
|
|
|
+ genService(tableName, modelName);
|
|
|
+ genController(tableName, modelName);
|
|
|
+ //genClient(tableName, modelName);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void genModelAndMapper(String tableName, String modelName) {
|
|
|
+ Context context = new Context(ModelType.FLAT);
|
|
|
+ context.setId("Potato");
|
|
|
+ context.setTargetRuntime("MyBatis3Simple");
|
|
|
+ context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
|
|
|
+ context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
|
|
|
+
|
|
|
+ JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
|
|
|
+ jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
|
|
|
+ jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
|
|
|
+ jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
|
|
|
+ jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
|
|
|
+ context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
|
|
|
+
|
|
|
+ PluginConfiguration pluginConfiguration = new PluginConfiguration();
|
|
|
+ pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
|
|
|
+ pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE);
|
|
|
+ context.addPluginConfiguration(pluginConfiguration);
|
|
|
+
|
|
|
+ JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
|
|
|
+ javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
|
|
|
+ javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
|
|
|
+ context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
|
|
|
+
|
|
|
+ SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
|
|
|
+ sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
|
|
|
+ sqlMapGeneratorConfiguration.setTargetPackage("mapper");
|
|
|
+ context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
|
|
|
+
|
|
|
+ JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
|
|
|
+ javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
|
|
|
+ javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE);
|
|
|
+ javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
|
|
|
+ context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
|
|
|
+
|
|
|
+ TableConfiguration tableConfiguration = new TableConfiguration(context);
|
|
|
+ tableConfiguration.setTableName(tableName);
|
|
|
+ tableConfiguration.setDomainObjectName(modelName);
|
|
|
+ tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
|
|
|
+ context.addTableConfiguration(tableConfiguration);
|
|
|
+
|
|
|
+ List<String> warnings;
|
|
|
+ MyBatisGenerator generator;
|
|
|
+ try {
|
|
|
+ Configuration config = new Configuration();
|
|
|
+ config.addContext(context);
|
|
|
+ config.validate();
|
|
|
+
|
|
|
+ boolean overwrite = true;
|
|
|
+ DefaultShellCallback callback = new DefaultShellCallback(overwrite);
|
|
|
+ warnings = new ArrayList<String>();
|
|
|
+ generator = new MyBatisGenerator(config, callback, warnings);
|
|
|
+ generator.generate(null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("生成Model和Mapper失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
|
|
|
+ throw new RuntimeException("生成Model和Mapper失败:" + warnings);
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(modelName))
|
|
|
+ modelName = tableNameConvertUpperCamel(tableName);
|
|
|
+ System.out.println(modelName + ".java 生成成功");
|
|
|
+ System.out.println(modelName + "Mapper.java 生成成功");
|
|
|
+ System.out.println(modelName + "Mapper.xml 生成成功");
|
|
|
+ System.out.println(modelName + "开始生成interface----------main-----------");
|
|
|
+// context = new Context(ModelType.FLAT);
|
|
|
+// context.setId("Potato");
|
|
|
+// context.setTargetRuntime("MyBatis3Simple");
|
|
|
+// context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
|
|
|
+// context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
|
|
|
+//
|
|
|
+// context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
|
|
|
+//
|
|
|
+// CommentGeneratorConfiguration myComment = new CommentGeneratorConfiguration();
|
|
|
+// myComment.setConfigurationType("com.conpany.project.MyCommentGenerator");
|
|
|
+// context.setCommentGeneratorConfiguration(myComment);
|
|
|
+//
|
|
|
+// javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
|
|
|
+// javaModelGeneratorConfiguration.setTargetProject(PROJECT_INTERFACE_PATH + JAVA_PATH);
|
|
|
+// javaModelGeneratorConfiguration.setTargetPackage(SERVICE_DTO_PACKAGE);
|
|
|
+// context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
|
|
|
+//
|
|
|
+// tableConfiguration = new TableConfiguration(context);
|
|
|
+// tableConfiguration.setTableName(tableName);
|
|
|
+// tableConfiguration.setDomainObjectName(modelName+"DPO");
|
|
|
+// tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
|
|
|
+// context.addTableConfiguration(tableConfiguration);
|
|
|
+//
|
|
|
+//
|
|
|
+// try {
|
|
|
+// Configuration config = new Configuration();
|
|
|
+// config.addContext(context);
|
|
|
+// config.validate();
|
|
|
+//
|
|
|
+// boolean overwrite = true;
|
|
|
+// DefaultShellCallback callback = new DefaultShellCallback(overwrite);
|
|
|
+// warnings = new ArrayList<String>();
|
|
|
+// generator = new MyBatisGenerator(config, callback, warnings);
|
|
|
+// generator.generate(null);
|
|
|
+// } catch (Exception e) {
|
|
|
+// throw new RuntimeException("生成DPO失败", e);
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (StringUtils.isEmpty(modelName))
|
|
|
+// modelName = tableNameConvertUpperCamel(tableName);
|
|
|
+// System.out.println(modelName + ".java 生成成功 --------DPO------");
|
|
|
+
|
|
|
+ context = new Context(ModelType.FLAT);
|
|
|
+ context.setId("Potato");
|
|
|
+ context.setTargetRuntime("MyBatis3Simple");
|
|
|
+ context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
|
|
|
+ context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
|
|
|
+
|
|
|
+ context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
|
|
|
+
|
|
|
+ CommentGeneratorConfiguration myComment2 = new CommentGeneratorConfiguration();
|
|
|
+ myComment2.setConfigurationType("com.conpany.project.MyCommentGenerator");
|
|
|
+ context.setCommentGeneratorConfiguration(myComment2);
|
|
|
+
|
|
|
+ PluginConfiguration serializablePlugin2 = new PluginConfiguration();
|
|
|
+ serializablePlugin2.setConfigurationType("com.conpany.project.SerializablePlugin");
|
|
|
+ context.addPluginConfiguration(serializablePlugin2);
|
|
|
+
|
|
|
+ javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
|
|
|
+ javaModelGeneratorConfiguration.setTargetProject(PROJECT_API_PATH + JAVA_PATH);
|
|
|
+ javaModelGeneratorConfiguration.setTargetPackage(SERVICE_DTO_PACKAGE);
|
|
|
+ context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
|
|
|
+
|
|
|
+ tableConfiguration = new TableConfiguration(context);
|
|
|
+ tableConfiguration.setTableName(tableName);
|
|
|
+ tableConfiguration.setDomainObjectName(modelName+"DTO");
|
|
|
+ tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
|
|
|
+ context.addTableConfiguration(tableConfiguration);
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ Configuration config = new Configuration();
|
|
|
+ config.addContext(context);
|
|
|
+ config.validate();
|
|
|
+
|
|
|
+ boolean overwrite = true;
|
|
|
+ DefaultShellCallback callback = new DefaultShellCallback(overwrite);
|
|
|
+ warnings = new ArrayList<String>();
|
|
|
+ generator = new MyBatisGenerator(config, callback, warnings);
|
|
|
+ generator.generate(null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("生成DTO失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(modelName))
|
|
|
+ modelName = tableNameConvertUpperCamel(tableName);
|
|
|
+ System.out.println(modelName + ".java 生成成功 --------DTO------");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成请求客户端
|
|
|
+ * @param tableName
|
|
|
+ * @param modelName
|
|
|
+ */
|
|
|
+ public static void genClient(String tableName, String modelName) {
|
|
|
+ try {
|
|
|
+ freemarker.template.Configuration cfg = getConfiguration();
|
|
|
+
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("date", DATE);
|
|
|
+ data.put("author", AUTHOR);
|
|
|
+ String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName)
|
|
|
+ : modelName;
|
|
|
+ data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
|
|
|
+ data.put("modelNameUpperCamel", modelNameUpperCamel);
|
|
|
+ data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
|
|
|
+ data.put("basePackage", BASE_PACKAGE);
|
|
|
+ data.put("application", APPLICATION);
|
|
|
+
|
|
|
+ File file = new File(
|
|
|
+ PROJECT_INTERFACE_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "ServiceClient.java");
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ cfg.getTemplate("client.ftl").process(data, new FileWriter(file));
|
|
|
+ System.out.println(modelNameUpperCamel + "Client.java 生成成功");
|
|
|
+
|
|
|
+// File file1 = new File(
|
|
|
+// PROJECT_INTERFACE_PATH + JAVA_PATH + PACKAGE_PATH_DTO + modelNameUpperCamel + "Dto.java");
|
|
|
+// if (!file1.getParentFile().exists()) {
|
|
|
+// file1.getParentFile().mkdirs();
|
|
|
+// }
|
|
|
+// cfg.getTemplate("dto.ftl").process(data, new FileWriter(file1));
|
|
|
+// System.out.println(modelNameUpperCamel + "Dto.java 生成成功");
|
|
|
+
|
|
|
+// Context context = new Context(ModelType.FLAT);
|
|
|
+// context.setId("Potato");
|
|
|
+// context.setTargetRuntime("MyBatis3Simple");
|
|
|
+// context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
|
|
|
+// context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
|
|
|
+//
|
|
|
+// JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
|
|
|
+// jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
|
|
|
+// jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
|
|
|
+// jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
|
|
|
+// jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
|
|
|
+// context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
|
|
|
+//
|
|
|
+// JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
|
|
|
+// javaModelGeneratorConfiguration.setTargetProject(PROJECT_INTERFACE_PATH + JAVA_PATH);
|
|
|
+// javaModelGeneratorConfiguration.setTargetPackage(SERVICE_DTO_PACKAGE);
|
|
|
+// context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
|
|
|
+//
|
|
|
+// TableConfiguration tableConfiguration = new TableConfiguration(context);
|
|
|
+// tableConfiguration.setTableName(tableName);
|
|
|
+// tableConfiguration.setDomainObjectName(modelName);
|
|
|
+// tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
|
|
|
+// context.addTableConfiguration(tableConfiguration);
|
|
|
+//
|
|
|
+// List<String> warnings;
|
|
|
+// MyBatisGenerator generator;
|
|
|
+// try {
|
|
|
+// Configuration config = new Configuration();
|
|
|
+// config.addContext(context);
|
|
|
+// config.validate();
|
|
|
+//
|
|
|
+// boolean overwrite = true;
|
|
|
+// DefaultShellCallback callback = new DefaultShellCallback(overwrite);
|
|
|
+// warnings = new ArrayList<String>();
|
|
|
+// generator = new MyBatisGenerator(config, callback, warnings);
|
|
|
+// generator.generate(null);
|
|
|
+// } catch (Exception e) {
|
|
|
+// throw new RuntimeException("生成Model失败", e);
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
|
|
|
+// throw new RuntimeException("生成Model失败:" + warnings);
|
|
|
+// }
|
|
|
+// if (StringUtils.isEmpty(modelName))
|
|
|
+// modelName = tableNameConvertUpperCamel(tableName);
|
|
|
+// System.out.println(modelName + ".java 生成成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("生成interface失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void genService(String tableName, String modelName) {
|
|
|
+ try {
|
|
|
+ freemarker.template.Configuration cfg = getConfiguration();
|
|
|
+
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("date", DATE);
|
|
|
+ data.put("author", AUTHOR);
|
|
|
+ String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName)
|
|
|
+ : modelName;
|
|
|
+ data.put("modelNameUpperCamel", modelNameUpperCamel);
|
|
|
+ data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
|
|
|
+ data.put("basePackage", BASE_PACKAGE);
|
|
|
+
|
|
|
+ File file = new File(
|
|
|
+ PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java");
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ cfg.getTemplate("service.ftl").process(data, new FileWriter(file));
|
|
|
+ System.out.println(modelNameUpperCamel + "Service.java 生成成功");
|
|
|
+
|
|
|
+ File file1 = new File(
|
|
|
+ PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE_IMPL + modelNameUpperCamel + "ServiceImpl.java");
|
|
|
+ if (!file1.getParentFile().exists()) {
|
|
|
+ file1.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ cfg.getTemplate("service-impl.ftl").process(data, new FileWriter(file1));
|
|
|
+ System.out.println(modelNameUpperCamel + "ServiceImpl.java 生成成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("生成Service失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void genController(String tableName, String modelName) {
|
|
|
+ try {
|
|
|
+ freemarker.template.Configuration cfg = getConfiguration();
|
|
|
+
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("date", DATE);
|
|
|
+ data.put("author", AUTHOR);
|
|
|
+ String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName)
|
|
|
+ : modelName;
|
|
|
+ data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
|
|
|
+ data.put("modelNameUpperCamel", modelNameUpperCamel);
|
|
|
+ data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
|
|
|
+ data.put("basePackage", BASE_PACKAGE);
|
|
|
+
|
|
|
+ File file = new File(
|
|
|
+ PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java");
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ // cfg.getTemplate("controller-restful.ftl").process(data, new
|
|
|
+ // FileWriter(file));
|
|
|
+ cfg.getTemplate("controller.ftl").process(data, new FileWriter(file));
|
|
|
+
|
|
|
+ System.out.println(modelNameUpperCamel + "Controller.java 生成成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("生成Controller失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static freemarker.template.Configuration getConfiguration() throws IOException {
|
|
|
+ freemarker.template.Configuration cfg = new freemarker.template.Configuration(
|
|
|
+ freemarker.template.Configuration.VERSION_2_3_23);
|
|
|
+ cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH));
|
|
|
+ cfg.setDefaultEncoding("UTF-8");
|
|
|
+ cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
|
|
|
+ return cfg;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String tableNameConvertLowerCamel(String tableName) {
|
|
|
+ return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String tableNameConvertUpperCamel(String tableName) {
|
|
|
+ return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String tableNameConvertMappingPath(String tableName) {
|
|
|
+ tableName = tableName.toLowerCase();// 兼容使用大写的表名
|
|
|
+ return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String modelNameConvertMappingPath(String modelName) {
|
|
|
+ String tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, modelName);
|
|
|
+ return tableNameConvertMappingPath(tableName);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String packageConvertPath(String packageName) {
|
|
|
+ return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|