Mybabtis-plus代码生成以及Swagger2生成api文档

本文档主要介绍了如何使用Mybatis-Plus进行代码生成,以及结合Swagger2来创建API接口文档,包括代码生成类的使用和相关配置文件的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、代码生成类

public class CodeGenerator {

    public static void main(String[] args) {


        String projectPath = System.getProperty("user.dir");

        List<FileOutConfig> fileOutConfigList = new ArrayList<>();

        // 调整 xml 生成目录演示
        fileOutConfigList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/src/main/resources/mapper/"+ tableInfo.getEntityName() + "Mapper.xml";
            }
        });


        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }

            @Override
            public void initTableMap(TableInfo tableInfo) {
                super.initTableMap(tableInfo);
                Map<String, Object> map = new HashMap<>();
                String serverName = String.valueOf(tableInfo.getServiceName().charAt(0));
                String entityName = String.valueOf(tableInfo.getEntityName().charAt(0));
                map.put("serverFiledName", tableInfo.getServiceName().replaceFirst(serverName,serverName.toLowerCase()));
                map.put("entityFiledName", tableInfo.getEntityName().replaceFirst(entityName,entityName.toLowerCase()));
                this.setMap(map);
            }
        };

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        injectionConfig.setFileOutConfigList(fileOutConfigList);
        mpg.setCfg(injectionConfig);

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController("templates/controller.java");
        templateConfig.setXml(null);

        // 选择Freemarker 引擎,默认Velocity 引擎
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.setTemplate(templateConfig);
        mpg.setCfg(injectionConfig);

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setAuthor("Nihui");

        gc.setOutputDir(projectPath + "/src/main/java");



        //是否覆盖同名文件,默认是false
        gc.setFileOverride(false);
        // 不需要ActiveRecord特性的请改为false
        gc.setActiveRecord(true);
        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(true);
        // 开启Swagger模式
        gc.setSwagger2(true);


        mpg.setGlobalConfig(gc);


        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl("jdbc:mysql://192.168.1.248:3306/db_renshe_new?useUnicode=true&useSSL=false&characterEncoding=utf-8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        PackageConfig pc = new PackageConfig();
        pc.setModuleName("api");
        pc.setParent("com.ziicoo");
        pc.setEntity("domain");

        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();

        // 设置表前缀
//        strategy.setTablePrefix(new String[]{"jqmj_"});
        // 是否采用驼峰命名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 生成表名
        strategy.setInclude(new String[]{"rcw_professional_info"});
        strategy.setRestControllerStyle(true);

        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
        // 自定义实体父类
        // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
        // 自定义实体,公共字段
        // strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
        // 自定义 mapper 父类
        // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
        // 自定义 service 父类
        // strategy.setSuperServiceClass("com.baomidou.demo.TestService");
        // 自定义 service 实现类父类
        // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
        // 自定义 controller 父类
        // strategy.setSuperControllerClass("com.baomidou.demo.TestController");
        // 【实体】是否生成字段常量(默认 false)
        // public static final String ID = "test_id";
        // strategy.setEntityColumnConstant(true);
        // 【实体】是否为构建者模型(默认 false)
        // public User setName(String name) {this.name = name; return this;}
        // strategy.setEntityBuilderModel(true);
        mpg.setStrategy(strategy);
        // 执行生成
        mpg.execute();
    }
}

二、相关配置文件
见百度网盘

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值