mybatis-plus代码生成器 -2025/4/21

package com.stdu;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;

import java.sql.Types;
import java.util.Collections;

public class CodeGenerator {

    private static String[] tableArr = {
            "wxuser"
    };

    public static void main(String[] args) {
        for (String table : tableArr) {
            try {
                generateCode(table);
                System.out.println(table + "表生成成功");
            } catch (Exception e) {
                System.out.println(table + "表生成失败");
                e.printStackTrace();
            }

        }
    }

    public static void generateCode(String table){
        String database = "jdbc:mysql://localhost:3306/device_manager?useSSL=false";
        String tablePrefix = "tb_";
        // main 文件夹绝对路径
        String path = "C:\\Users\\lenovo\\Desktop\\stdu\\XYu1230\\device-manager\\device-manager-back\\dm-server\\src\\main";

        //取到当前java文件的绝对路径
        FastAutoGenerator.create(database, "root", "123456")
                .globalConfig(builder -> {
                    builder.author("XYu1230") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(path + "\\java") // 指定输出目录
                            .disableOpenDir();
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                                builder.parent("com.stdu") // 设置父包名
//                                .moduleName("system") // 设置父包模块名
                                        .pathInfo(Collections.singletonMap(OutputFile.xml, path + "\\resources\\com\\stdu\\mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude(table) // 设置需要生成的表名
                                .addTablePrefix(tablePrefix) // 设置过滤表前缀
                                .entityBuilder()
                                .enableFileOverride()
                                .enableLombok(new ClassAnnotationAttributes("@Data","lombok.Data"))
                                .controllerBuilder()
                                .enableRestStyle()
                )
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();
    }
}
posted @ 2025-04-29 09:39  XYu1230  阅读(58)  评论(0)    收藏  举报