• 9.基于SpringBoot3+MybatisPlus定制化代码生成器类


    我们在3.基于SpringBoot3集成MybatisPlus中讲到自定义代码生成器,但是往往遗留代码生成的类格式或者命名不符合要求,需要手工修改,但是当表很多时就比较头痛,所以我们自定义模板在进行代码生成

    1. 新建MyTemplateEngine.java类

    里面大多实现直接拷贝自VelocityTemplateEngine.java, 只是增加了反射,修改包名,搜索修改地方即可找到两处位置

    public class MyTemplateEngine extends AbstractTemplateEngine {
    
        /**
         * 批量输出 java xml 文件
         */
        @NotNull
        public AbstractTemplateEngine batchOutput() {
            try {
                ConfigBuilder config = this.getConfigBuilder();
                List<TableInfo> tableInfoList = config.getTableInfoList();
                tableInfoList.forEach(tableInfo -> {
                    Map<String, Object> objectMap = this.getObjectMap(config, tableInfo);
                    // 修改地方一:通过反射修改属性值,替换不需要的前缀
                    Class<? extends  TableInfo> clazz = tableInfo.getClass();
                    try {
                        Field name = clazz.getDeclaredField("name");
                        name.setAccessible(true);
                        name.set(tableInfo, tableInfo.getName().replaceFirst("tb_", ""));
    
                        Field entityName = clazz.getDeclaredField("entityName");
                        entityName.setAccessible(true);
                        entityName.set(tableInfo, tableInfo.getEntityName().replaceFirst("Tb", ""));
    
                        Field mapperName = clazz.getDeclaredField("mapperName");
                        mapperName.setAccessible(true);
                        mapperName.set(tableInfo, tableInfo.getMapperName().replaceFirst("Tb", ""));
    
                        Field xmlName = clazz.getDeclaredField("xmlName");
                        xmlName.setAccessible(true);
                        xmlName.set(tableInfo, tableInfo.getXmlName().replaceFirst("Tb", ""));
    
                        Field serviceName = clazz.getDeclaredField("serviceName");
                        serviceName.setAccessible(true);
                        serviceName.set(tableInfo, tableInfo.getServiceName().replaceFirst("ITb", ""));
    
                        Field serviceImplName = clazz.getDeclaredField("serviceImplName");
                        serviceImplName.setAccessible(true);
                        serviceImplName.set(tableInfo, tableInfo.getServiceImplName().replaceFirst("Tb", ""));
    
                        Field controllerName = clazz.getDeclaredField("controllerName");
                        controllerName.setAccessible(true);
                        controllerName.set(tableInfo, tableInfo.getControllerName().replaceFirst("Tb", ""));
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    Optional.ofNullable(config.getInjectionConfig()).ifPresent(t -> {
                        // 添加自定义属性
                        t.beforeOutputFile(tableInfo, objectMap);
                        // 输出自定义文件
                        outputCustomFile(t.getCustomFiles(), tableInfo, objectMap);
                    });
    		// 修改地方二
                    objectMap.put("entity", String.valueOf(objectMap.get("entity")).replaceFirst("Tb", ""));
                    // entity
                    outputEntity(tableInfo, objectMap);
                    // mapper and xml
                    outputMapper(tableInfo, objectMap);
                    // service
                    outputService(tableInfo, objectMap);
                    // controller
                    outputController(tableInfo, objectMap);
                });
            } catch (Exception e) {
                throw new RuntimeException("无法创建文件,请检查配置信息!", e);
            }
            return this;
        }
    
        private VelocityEngine velocityEngine;
    
        {
            try {
                Class.forName("org.apache.velocity.util.DuckType");
            } catch (ClassNotFoundException e) {
                // velocity1.x的生成格式错乱 https://github.com/baomidou/generator/issues/5
                LOGGER.warn("Velocity 1.x is outdated, please upgrade to 2.x or later.");
            }
        }
    
        @Override
        public @NotNull MyTemplateEngine init(@NotNull ConfigBuilder configBuilder) {
            if (null == velocityEngine) {
                Properties p = new Properties();
                p.setProperty(ConstVal.VM_LOAD_PATH_KEY, ConstVal.VM_LOAD_PATH_VALUE);
                p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, StringPool.EMPTY);
                p.setProperty(Velocity.ENCODING_DEFAULT, ConstVal.UTF8);
                p.setProperty(Velocity.INPUT_ENCODING, ConstVal.UTF8);
                p.setProperty("file.resource.loader.unicode", StringPool.TRUE);
                velocityEngine = new VelocityEngine(p);
            }
            return this;
        }
    
    
        @Override
        public void writer(@NotNull Map<String, Object> objectMap, @NotNull String templatePath, @NotNull File outputFile) throws Exception {
            Template template = velocityEngine.getTemplate(templatePath, ConstVal.UTF8);
            try (FileOutputStream fos = new FileOutputStream(outputFile);
                 OutputStreamWriter ow = new OutputStreamWriter(fos, ConstVal.UTF8);
                 BufferedWriter writer = new BufferedWriter(ow)) {
                template.merge(new VelocityContext(objectMap), writer);
            }
            LOGGER.debug("模板:" + templatePath + ";  文件:" + outputFile);
        }
    
    
        @Override
        public @NotNull String templateFilePath(@NotNull String filePath) {
            final String dotVm = ".vm";
            return filePath.endsWith(dotVm) ? filePath : filePath + dotVm;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    2. 模板文件替换修改

    IDEA全局搜索需要的下图文件,拷贝至resources/templates下,然后把里面的内容根据自己需求进行模板调整

    3. 代码生成类

    之前是Swagger注解,我们调整为Springdoc注解,并对实体不额外添加后缀%s

    public class FastAutoGeneratorTest {
        public static void main(String[] args) {
            DataSourceConfig.Builder dataSourceConfig = new DataSourceConfig.Builder("jdbc:mysql://XXXX:3306/yy_dev?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai","root","Gensci@123");
            FastAutoGenerator.create(dataSourceConfig)
                    // 全局配置
                    .globalConfig((scanner, builder) -> builder.enableSpringdoc().author(scanner.apply("请输入作者名称?")))
                    // 包配置
                    .packageConfig((scanner, builder) -> builder.parent(scanner.apply("请输入包名?")))
                    // 策略配置
                    .templateEngine(new MyTemplateEngine())
                    .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名,多个英文逗号分隔?所有输入 all")))
                            .controllerBuilder().enableRestStyle().enableHyphenStyle()
                            .entityBuilder().enableLombok().enableTableFieldAnnotation().idType(IdType.INPUT).formatFileName("%s").build())
                    .execute();
        }
    
        // 处理 all 情况
        protected static List<String> getTables(String tables) {
            return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    4. 效果如图

  • 相关阅读:
    【C语言进阶】指针进阶(三)
    ffmpeg实现视频播放 ----------- Javacv
    (2022最新)Java毕业设计参考题目-题目新颖(值得收藏)
    scrapy的入门使用
    微服务架构 | 超时管理
    基于springboot的高校科研管理系统(源码+调试+LW)
    Spring boot 通过 wkhtmltopdf 实现URL转PDF
    【MySQL】多表查询
    自定义函数
    最简单检查jar包状态并实现自愈脚本
  • 原文地址:https://blog.csdn.net/SJshenjian/article/details/134429153