Mybatis Plus Generator代码生成器

Mybatis Plus Generator代码生成器


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GeneratorMain {
    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
        tables.add("t_user");
//        tables.add("p_question");
//        tables.add("p_answer");
//        tables.add("p_correct");

        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/generator_demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"
                        , "root"
                        , "root")
                .globalConfig(builder -> {
                    builder.author("ximen") // 设置类上注释作者
                            // 设置作者下的创建时间
                            .commentDate("yyyy-MM-dd")
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(System.getProperty("user.dir")+"\\src\\main\\java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.ximen")
                            // 模块包名
                            .moduleName("demo")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.Impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.xml,System.getProperty("user.dir")+"\\src\\main\\resources\\mapper"));
                })
                .strategyConfig(builder -> {
                    // 需要生成表的列表
                    builder.addInclude(tables)
                            // 过滤掉表的前缀去生成实体类
                            .addTablePrefix("t_")

                            // service策略配置
                            .serviceBuilder()
                            // 设置service类名,%s适配表名
                            .formatServiceFileName("%sService")
                            // 设置impl类名,%s适配表名
                            .formatServiceImplFileName("%sServiceImpl")

                            // entity实体类策略配置
                            .entityBuilder()
                            // 开启Lombok
                            .enableLombok()
                            // 定义逻辑删除字段
//                            .logicDeleteColumnName("deleted")
                            // 属性上加说明注解
                            .enableTableFieldAnnotation()

                            // controller策略配置
                            .controllerBuilder()
                            // 设置controller类名,%s适配表名
                            .formatFileName("%sController")
                            // 开启RestController
                            .enableRestStyle()

                            // mapper策略配置
                            .mapperBuilder()
                            // 生成通用的resultMap
                            .enableBaseResultMap()
                            // 设置继承的父类
                            .superClass(BaseMapper.class)
                            // 设置mapper类名,%s适配表名
                            .formatMapperFileName("%sMapper")
                            // 开启@Mapper注解
                            .enableMapperAnnotation()
                            // 设置mapper的xml名,%s适配表名
                            .formatXmlFileName("%sMapper");
                })
                // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }
}

本文作者:-西门-

本文链接:https://www.cnblogs.com/ximensama/p/15957548.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   -西门-  阅读(296)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 キルミーのベイベー! 爱杀宝贝
キルミーのベイベー! - 爱杀宝贝
00:00 / 00:00
An audio error has occurred.