ReturnTmp's Blog ReturnTmp's Blog
首页
基础课程
编程语言
框架技术
运维笔记
人工智能
随笔摘录
  • 友链
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

ReturnTmp

分享有趣好玩的计算机知识
首页
基础课程
编程语言
框架技术
运维笔记
人工智能
随笔摘录
  • 友链
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 书评

    • 时间管理汇总(彼得·德鲁克)
    • 《人月神话》 汇总精要
  • 刷题

  • 科研

  • Win

  • 浏览器

  • 备案注销流程
  • 开源证书使用指南
  • 『AI写作助手』辅助写作平台分享
  • Namesilo 域名购买
  • 就业方向解析
  • 英文写作排版指南
  • iOS 科学上网指南
  • 主机游戏模拟器
  • 『阿里云盘 & AList & Kodi』家庭影院搭建指南
  • 技术文档工具『Writerside』抢鲜体验
  • 雅思备考
  • Docker 常用容器部署命令
  • 笔记软件 Obsidian 快速入门指南
  • OAuth2.0 协议解析
  • 芋道框架学习
  • 一键改包
  • 代码生成
  • MP 代码生成
    • 前言
    • 步骤
    • 配置
    • 补充
    • 参考链接
  • IDEA 格式化问题
  • 右键删除或新增 Open Folder as Intellij IDEA Project
  • 提升认知,推荐15个面向开发者的中文播客
  • GPT小说生成
  • certificate has expired 证书过期
  • Druid mysql 连接失败问题
  • 开源项目疑问
  • 腾讯云域名转到阿里云
  • 百度网盘加速
  • 随笔摘录
ReturnTmp
2023-11-27
目录

MP 代码生成

# 前言

官方链接:代码生成器(新) | MyBatis-Plus (baomidou.com) (opens new window)

代码仓库:generator/mybatis-plus-generator (opens new window)

# 步骤

依赖引入

<dependency>  
    <groupId>com.baomidou</groupId>  
    <artifactId>mybatis-plus-generator</artifactId>  
    <version>3.5.1</version>  
</dependency>  
<!-- 模板引擎 -->  
<dependency>  
    <groupId>org.apache.velocity</groupId>  
    <artifactId>velocity-engine-core</artifactId>  
    <version>2.0</version>  
</dependency>
1
2
3
4
5
6
7
8
9
10
11

注:官方没有引入模板引擎依赖,但是不引入会爆出错误

BaseGeneratorTest

/**  
 * 基础测试类  
 */  
public class BaseGeneratorTest {  
  
  
    /**  
     * 策略配置  
     */  
    protected static StrategyConfig.Builder strategyConfig() {  
        return new StrategyConfig.Builder();  
    }  
  
    /**  
     * 全局配置  
     */  
    protected static GlobalConfig.Builder globalConfig() {  
        return new GlobalConfig.Builder();  
    }  
  
    /**  
     * 包配置  
     */  
    protected static PackageConfig.Builder packageConfig() {  
        return new PackageConfig.Builder();  
    }  
  
    /**  
     * 模板配置  
     */  
    protected static TemplateConfig.Builder templateConfig() {  
        return new TemplateConfig.Builder();  
    }  
  
    /**  
     * 注入配置  
     */  
    protected static InjectionConfig.Builder injectionConfig() {  
        // 测试自定义输出文件之前注入操作,该操作再执行生成代码前 debug 查看  
        return new InjectionConfig.Builder().beforeOutputFile((tableInfo, objectMap) -> {  
            System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());  
        });  
    }  
}
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

MySQLGeneratorTest

/**
 * MySQL 代码生成
 */
public class MySQLGeneratorTest extends BaseGeneratorTest {

    /**
     * 数据源配置
     */
    private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://localhost:3306/compkey?serverTimezone=Asia/Shanghai", "root", "111111")
            .schema("compkey")
            .build();

    @Test
    public void testSimple() {
        AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
        generator.strategy(strategyConfig().build());
        generator.global(globalConfig().build());
        generator.execute();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

这是最基础的代码生成器,运行之后即可在目录 D:\com\baomidou 下生成 controller, entity, mapper(子文件 xml 中含有 xml 文件), service 层代码

然后给 mapper 文件夹下的 xml 文件夹重命名为 mapper(虽然不重命名也可以,但是 mapper 名称是约定俗称),然后给重命名后的 mapper 文件夹移动到 resources 下,最后在 application.yaml 中添加

mybatis:
  mapper-locations: classpath:mapper/*.xml
1
2

之后主启动类添加 @MapperScan

注:这里不添加 @MapperScan 也可以直接在 mapper 层每个类中添加 @Mapper 注解,但缺点是比较繁琐,每个类都需要添加(虽然代码生成器可以自动生成,后面会给出配置),优点是粒度更细

@SpringBootApplication
@MapperScan("com.example.compkeylab.mapper")
public class CompkeyLabApplication {

    public static void main(String[] args) {
        SpringApplication.run(CompkeyLabApplication.class, args);
    }

}
1
2
3
4
5
6
7
8
9

# 配置

博主自己的配置,读者可以自行参考

/**  
 * 基础测试类  
 */  
public class BaseGeneratorTest {  
  
  
    /**  
     * 策略配置  
     */  
    protected static StrategyConfig.Builder strategyConfig() {  
        return new StrategyConfig.Builder();  
    }  
  
    /**  
     * 全局配置  
     */  
    protected static GlobalConfig.Builder globalConfig() {  
        return new GlobalConfig.Builder()  
                .author("Cheng Guanghao")  
                .enableSwagger();  
    }  
  
    /**  
     * 包配置  
     */  
    protected static PackageConfig.Builder packageConfig() {  
        return new PackageConfig.Builder()  
                .parent("com.example")  
                .moduleName("compkeylab")  
                .xml("mapper.mapper")  
                .other("other");  
    }  
  
    /**  
     * 模板配置  
     */  
    protected static TemplateConfig.Builder templateConfig() {  
        return new TemplateConfig.Builder();  
    }  
  
    /**  
     * 注入配置  
     */  
    protected static InjectionConfig.Builder injectionConfig() {  
        // 测试自定义输出文件之前注入操作,该操作再执行生成代码前 debug 查看  
        return new InjectionConfig.Builder().beforeOutputFile((tableInfo, objectMap) -> {  
            System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());  
        });  
    }  
}
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
/**
 * MySQL 代码生成
 */
public class MySQLGeneratorTest extends BaseGeneratorTest {

    /**
     * 数据源配置
     */
    private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://localhost:3306/compkey?serverTimezone=Asia/Shanghai", "root", "111111")
            .schema("compkey")
            .build();

    @Test
    public void testSimple() {
        AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
        generator.strategy(strategyConfig().build());
        generator.global(globalConfig().build());
        generator.packageInfo(packageConfig().build());
        generator.execute();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 补充

MyBatis 中 xml 是比注解优先级更高的

推荐 IDEA 插件 :better-mybatis-generator,可以实现 mapper,entity 层代码实现

如果是文件夹直接 move 到 IDEA 中,大概率会遇到 Cannot move 问题,可以使用 idea 中新建相同空白文件夹,然后移动文件到空白文件夹中

或者是直接在文件资源管理器中移动(推荐)

# 参考链接

编辑 (opens new window)
上次更新: 2024/01/02, 01:26:45
代码生成
IDEA 格式化问题

← 代码生成 IDEA 格式化问题→

最近更新
01
百度网盘加速
03-24
02
新版 PyCharm 设置 Conda 虚拟环境
03-24
03
腾讯云域名转到阿里云
03-24
更多文章>
Theme by Vdoing | Copyright © 2023-2024 ReturnTmp | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式