完整代码

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
package com.itheima.reggie;

/**
* <p>
* mysql 代码生成器演示例子
* </p>
*/
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

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

/**
* <p>
* mysql 代码生成器演示例子
* </p>
*/
public class MysqlGenerator {

/**
* RUN THIS
*/
public static void main(String[] args) {
List<String> tables = new ArrayList<>();
//添加需要生成的表
tables.add("dish");
tables.add("setmeal");


FastAutoGenerator.create("jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true","root","Xuwei19941214~")
.globalConfig(builder -> {
builder.author("威少") //作者
.outputDir("/Users/xuwei/Desktop/项目/reggie_take_out/src/main/java") //输出路径(写到java目录)
.commentDate("yyyy-MM-dd");
// .enableSwagger() //开启swagger
// .fileOverride(); //开启覆盖之前生成的文件

})
.packageConfig(builder -> {
builder.parent("com.itheima")
.moduleName("reggie")
.entity("entity")
.service("service")
.serviceImpl("service.impl")
.controller("controller")
.mapper("mapper")
.xml("mapper.xml")
.pathInfo(Collections.singletonMap(OutputFile.mapperXml,"/Users/xuwei/Desktop/项目/reggie_take_out/src/main/resources/mapper"));
})
.strategyConfig(builder -> {
builder.addInclude(tables)
// .addTablePrefix("p_")
.serviceBuilder()
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImpl")
.entityBuilder()
.enableLombok()
.logicDeleteColumnName("is_deleted")
.enableTableFieldAnnotation()
.controllerBuilder()
// 映射路径使用连字符格式,而不是驼峰
.enableHyphenStyle()
.formatFileName("%sController")
.enableRestStyle()
.mapperBuilder()
//生成通用的resultMap
.enableBaseResultMap()
.superClass(BaseMapper.class)
.formatMapperFileName("%sMapper")
.enableMapperAnnotation()
.formatXmlFileName("%sMapper");
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}


}

导入依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>

添加生成数据库表

image-20221105211001754

全局配置

image-20221105211544956

包配置

image-20221105211836590

策略配置

421d182015b73dce9227ee817e164a02

官网参考

https://baomidou.com/pages/981406/#%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE-datasourceconfig