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
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
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
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
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
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
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
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