|
|
@@ -0,0 +1,249 @@
|
|
|
+package com.deer.wms.generator2;
|
|
|
+
|
|
|
+import com.deer.wms.generator2.configurer.DatasourceConfigurer;
|
|
|
+import com.deer.wms.generator2.configurer.Table;
|
|
|
+import com.google.common.base.CaseFormat;
|
|
|
+import freemarker.template.TemplateExceptionHandler;
|
|
|
+import org.mybatis.generator.api.MyBatisGenerator;
|
|
|
+import org.mybatis.generator.config.*;
|
|
|
+import org.mybatis.generator.internal.DefaultShellCallback;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
|
|
|
+ *
|
|
|
+ * Created by Floki on 2017/7/30.
|
|
|
+ */
|
|
|
+public class Generator {
|
|
|
+
|
|
|
+ private static final String JAVA_PATH = "/src/main/java"; //java文件路径
|
|
|
+ //private static final String RESOURCES_PATH = "/src/main/resources";//资源文件路径
|
|
|
+ private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径
|
|
|
+ private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/test/resources/generator/template";//模板位置
|
|
|
+ private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//@date
|
|
|
+ /**
|
|
|
+ * 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
|
|
|
+ * 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
|
|
|
+ *
|
|
|
+ * @param configurer 生成代码的配置信息
|
|
|
+ */
|
|
|
+ public static void genCode(DatasourceConfigurer configurer) {
|
|
|
+ if (null == configurer) {
|
|
|
+ System.out.println("没有生成代码的配置文件......");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Table> tables = configurer.getTables();
|
|
|
+ if (null == tables || tables.isEmpty()) {
|
|
|
+ System.out.println("没有生成代码的数据表......");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ tables.forEach(table -> genCode(configurer, table));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过数据表名称,和自定义的 Model 名称生成代码
|
|
|
+ * 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
|
|
|
+ * @param configurer 配置文件
|
|
|
+ * @param table 需要生成代码的数据表信息
|
|
|
+ */
|
|
|
+ public static void genCode(DatasourceConfigurer configurer, Table table) {
|
|
|
+ Long start = System.currentTimeMillis();
|
|
|
+ genModelAndMapper(configurer, table.getTableName(), table.getModelName(), table.getColumn());
|
|
|
+ genCriteria(configurer, table.getTableName(), table.getModelName());
|
|
|
+ genService(configurer, table.getTableName(), table.getModelName(), table.getType());
|
|
|
+ genController(configurer, table.getTableName(), table.getModelName(), table.getType());
|
|
|
+ System.out.println("耗时 " + (System.currentTimeMillis() - start) + " 毫秒");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void genModelAndMapper(DatasourceConfigurer configurer, String tableName, String modelName, String column) {
|
|
|
+ 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(configurer.getUrl());
|
|
|
+ jdbcConnectionConfiguration.setUserId(configurer.getUserName());
|
|
|
+ jdbcConnectionConfiguration.setPassword(configurer.getPassword());
|
|
|
+ jdbcConnectionConfiguration.setDriverClass(configurer.getDiverClassName());
|
|
|
+ context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
|
|
|
+
|
|
|
+ PluginConfiguration pluginConfiguration = new PluginConfiguration();
|
|
|
+ pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
|
|
|
+ pluginConfiguration.addProperty("mappers", "Mapper");
|
|
|
+ context.addPluginConfiguration(pluginConfiguration);
|
|
|
+
|
|
|
+ JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
|
|
|
+ javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + configurer.getModuleName() + JAVA_PATH);
|
|
|
+ javaModelGeneratorConfiguration.setTargetPackage(configurer.getModelPackage());
|
|
|
+ context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
|
|
|
+
|
|
|
+ SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
|
|
|
+ sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + configurer.getModuleName() + JAVA_PATH);
|
|
|
+ sqlMapGeneratorConfiguration.setTargetPackage(configurer.getMapperPackage());
|
|
|
+ context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
|
|
|
+
|
|
|
+ JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
|
|
|
+ javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + configurer.getModuleName() + JAVA_PATH);
|
|
|
+ javaClientGeneratorConfiguration.setTargetPackage(configurer.getDaoPackage());
|
|
|
+ javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
|
|
|
+ context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
|
|
|
+
|
|
|
+ TableConfiguration tableConfiguration = new TableConfiguration(context);
|
|
|
+ tableConfiguration.setTableName(tableName);
|
|
|
+ tableConfiguration.setDomainObjectName(modelName);
|
|
|
+ tableConfiguration.setGeneratedKey(new GeneratedKey(column, "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<>();
|
|
|
+ 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 生成成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void genService(DatasourceConfigurer configurer, String tableName, String modelName, String type) {
|
|
|
+ try {
|
|
|
+ freemarker.template.Configuration cfg = getConfiguration();
|
|
|
+
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("date", DATE);
|
|
|
+ data.put("author", configurer.getAuthor());
|
|
|
+ String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
|
|
|
+ data.put("modelNameUpperCamel", modelNameUpperCamel);
|
|
|
+ data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
|
|
|
+ data.put("basePackage", configurer.getBasePackage());
|
|
|
+ data.put("type", type);
|
|
|
+
|
|
|
+ String servicePathPackage = packageConvertPath(configurer.getServicePackage());//生成的Service存放路径
|
|
|
+ File file = new File(PROJECT_PATH + configurer.getModuleName() + JAVA_PATH + servicePathPackage + 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 生成成功");
|
|
|
+
|
|
|
+ String serviceImplPathPackage = packageConvertPath(configurer.getServiceImplPackage());//生成的Service实现存放路径
|
|
|
+ File file1 = new File(PROJECT_PATH + configurer.getModuleName() + JAVA_PATH + serviceImplPathPackage + 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(DatasourceConfigurer configurer, String tableName, String modelName, String type) {
|
|
|
+ try {
|
|
|
+ freemarker.template.Configuration cfg = getConfiguration();
|
|
|
+
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("date", DATE);
|
|
|
+ data.put("author", configurer.getAuthor());
|
|
|
+ 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", configurer.getBasePackage());
|
|
|
+ data.put("type", type);
|
|
|
+
|
|
|
+ String controllerPathPackage = packageConvertPath(configurer.getControllerPackage());//生成的Controller存放路径
|
|
|
+ File file = new File(PROJECT_PATH + configurer.getModuleName() + JAVA_PATH + controllerPathPackage + 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void genCriteria(DatasourceConfigurer configurer, String tableName, String modelName) {
|
|
|
+ try {
|
|
|
+ freemarker.template.Configuration cfg = getConfiguration();
|
|
|
+
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("date", DATE);
|
|
|
+ data.put("author", configurer.getAuthor());
|
|
|
+ String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
|
|
|
+ data.put("modelNameUpperCamel", modelNameUpperCamel);
|
|
|
+ data.put("basePackage", configurer.getBasePackage());
|
|
|
+
|
|
|
+ String criteriaPathPackage = packageConvertPath(configurer.getModelPackage());//生成的criteria存放路径
|
|
|
+ File file = new File(PROJECT_PATH + configurer.getModuleName() + JAVA_PATH + criteriaPathPackage + modelNameUpperCamel + "Criteria.java");
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ cfg.getTemplate("criteria.ftl").process(data, new FileWriter(file));
|
|
|
+
|
|
|
+ System.out.println(modelNameUpperCamel + "Criteria.java 生成成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("生成Criteria失败", 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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|