mybaits plus 代码生成器
# 说明
注意
该文只适用 3.5.1 以上版本,对历史版本的不兼容
只对ORACLE做了微调
# 基本信息
mybatis plus 代码生成器官方地址: (opens new window) https://github.com/baomidou/generator/tree/7295148b874f9251b8fbe3f3ec897b784a052b8f
官方示例地址: (opens new window) https://github.com/baomidou/generator/tree/7295148b874f9251b8fbe3f3ec897b784a052b8f/mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/generator/samples
# pom引入
<mybatis-plus-boot.version>3.5.1</mybatis-plus-boot.version>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus-boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</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
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 代码微调
# BaseGeneratorTest.java
import com.baomidou.mybatisplus.generator.config.*;
import org.apache.ibatis.jdbc.ScriptRunner;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
/**
* 基础测试类
*
* @author lanjerry
* @since 3.5.3
*/
public class BaseGeneratorTest {
//
// /**
// * 执行数据库脚本
// */
// protected static void initDataSource(DataSourceConfig dataSourceConfig) throws SQLException {
// Connection conn = dataSourceConfig.getConn();
// InputStream inputStream = H2CodeGeneratorTest.class.getResourceAsStream("/sql/init.sql");
// ScriptRunner scriptRunner = new ScriptRunner(conn);
// scriptRunner.setAutoCommit(true);
// scriptRunner.runScript(new InputStreamReader(inputStream));
// conn.close();
// }
/**
* 策略配置
*/
protected static StrategyConfig.Builder strategyConfig() {
return new StrategyConfig.Builder().addInclude("USER");//类名需要大写
}
/**
* 全局配置
*/
protected static GlobalConfig.Builder globalConfig() {
return new GlobalConfig.Builder().author("Felix").outputDir("F://generator");
}
/**
* 包配置
*/
protected static PackageConfig.Builder packageConfig() {
return new PackageConfig.Builder().parent("com.felix.manage");
}
/**
* 模板配置
*/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
# OracleGeneratorTest.java
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import org.junit.Test;
/**
* Oracle 代码生成
*
* @author lanjerry
* @since 3.5.3
*/
public class OracleGeneratorTest extends BaseGeneratorTest {
/**
* 数据源配置
*/
private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
.Builder("jdbc:oracle:thin:@127.0.0.1:1521/felix", "felix", "123456")
.schema("FELIX") //库名(用户名)需要大写
.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
23
24
25
26
27
28
29
30
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
上次更新: 2026/3/11 21:47:04