CodeGenerate.java 4.22 KB
Newer Older
hkl's avatar
hkl committed
1 2
package org.jeecg.codegenerate;

hkl's avatar
hkl committed
3 4 5 6 7 8 9 10 11 12 13
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
hkl's avatar
hkl committed
14 15

public class CodeGenerate {
hkl's avatar
hkl committed
16 17 18 19
    public static String url = "jdbc:mysql://47.94.207.62:3306/hzsomms?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&zeroDateTimeBehavior=convertToNull";
    public static String username = "root";
    public static String password = "superAdmin&321";

hkl's avatar
hkl committed
20
    public static void main(String[] args) {
hkl's avatar
hkl committed
21 22 23 24
        String projectPath = System.getProperty("user.dir");
        System.out.println("projectPath = " + projectPath);

        // 输出目录
hkl's avatar
hkl committed
25
        String outputDir = projectPath + "\\jeecg-module-system\\src\\main\\java\\";
hkl's avatar
hkl committed
26 27

        // 模块名
28
        String moduleName = "checkData.dynamicStaticGeometricData";
hkl's avatar
hkl committed
29 30 31

        // 表名
        String[] tables = {
32 33 34
                "t_dsg_rail_inspection_equipment",
                "t_dsg_rail_inspection_equipment_item",
                "t_dsg_rail_inspection_equipment_item_detail",
35

hkl's avatar
hkl committed
36 37
        };

hkl's avatar
hkl committed
38

hkl's avatar
hkl committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        FastAutoGenerator.create(url, username, password)
                // 全局配置
                .globalConfig(builder -> {
                    builder.author("hkl")
                            .enableSwagger()
                            .commentDate("yyyy-MM-dd")
                            .dateType(DateType.ONLY_DATE)
                            .outputDir(outputDir)
                            .build();
                })
                // 包配置
                .packageConfig(builder -> {
                    builder.moduleName(moduleName)
                            .parent("org.jeecg.modules")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .controller("controller")
                            .build();
                })
                // 设置过滤前缀
                .strategyConfig(builder -> {
                    builder.addInclude(tables) // 设置需要生成的表名
63
                            .addTablePrefix("t_sn", "t_da","t_ek","t_xd","t_dsg"); // 设置过滤表前缀
hkl's avatar
hkl committed
64

hkl's avatar
hkl committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                    // entity实体策略
                    builder.entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation();

                    // 控制层策略
                    builder.controllerBuilder()
                            .superClass(JeecgController.class)
                            .enableRestStyle();

                })
                .injectionConfig(consumer -> {
                    Map<String, String> customFile = new HashMap<>();
                    // DTO
                    customFile.put("DTO.java", "/generator/dto.java.ftl");
                    customFile.put("VO.java", "/generator/vo.java.ftl");
                    consumer.customFile(customFile).build();

                })

                // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .templateEngine(new EnhanceFreemarkerTemplateEngine())
                .execute();
    }
hkl's avatar
hkl committed
89

hkl's avatar
hkl committed
90 91 92 93 94 95 96 97 98 99 100 101 102
    /**
     * 代码生成器支持自定义[DTO\VO等]模版
     */
    public static class EnhanceFreemarkerTemplateEngine extends FreemarkerTemplateEngine {
        @Override
        protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
            String entityName = tableInfo.getEntityName();
            String otherPath = this.getPathInfo(OutputFile.other);
            customFile.forEach((key, value) -> {
                String fileName = String.format(otherPath + File.separator + entityName + "%s", key);
                this.outputFile(new File(fileName), objectMap, value);
            });
        }
hkl's avatar
hkl committed
103 104
    }
}