这里使用的版本

1
mybatis-plus version 3.4.3.1
1
2
3
4
5
6
7
8
9
10
11
<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.3</version>
</dependency>
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.stark37125.core.base.cons.CommonField;
import com.stark37125.core.base.controller.MyController;
import com.stark37125.core.base.mapper.MyMapper;
import com.stark37125.core.base.model.entity.MyEntity;
import com.stark37125.core.base.service.MyService;
import com.stark37125.core.base.service.impl.MyServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author shuzhuo
*/
public class CodeGenerator {

private static Pattern humpPattern = Pattern.compile("[A-Z]");

public static String humpToLine(String str) {
Matcher matcher = humpPattern.matcher(str);
StringBuffer sb = new StringBuffer();

while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}

matcher.appendTail(sb);
return sb.toString();
}


/**
* 公共字段
*/
static String[] commonField = new String[]{CommonField.CREATE_OBJ_ID_FIELD,
CodeGenerator.humpToLine(CommonField.CREATE_OBJ_TYPE),
CodeGenerator.humpToLine(CommonField.CREATE_USER),
CodeGenerator.humpToLine(CommonField.DELETE_FLAG),
CodeGenerator.humpToLine(CommonField.OBJ_ID),
CodeGenerator.humpToLine(CommonField.OBJ_TYPE),
CodeGenerator.humpToLine(CommonField.UPDATE_USER),
CodeGenerator.humpToLine(CommonField.ID),
CodeGenerator.humpToLine(CommonField.REMARK),
"create_time",
"update_time"

};

static String basePackage = "com.stark37125.core";

static String parentPackage = basePackage + ".modules.business";

static String url = "jdbc:postgresql://localhost:5432/password-evaluation";

static String username = "postgres";

static String password = "postgresql";

public static void main(String[] args) {
String[] tableName = {"app_case_evaluation"};
String moduleName = "evaluation";

generator(tableName, moduleName);

}

private static void generator(String[] tableName, String moduleName) {
String projectPath = System.getProperty("user.dir");
FastAutoGenerator.create(new DataSourceConfig.Builder(url, username, password).schema("public"))
.globalConfig(builder -> {
builder.author(getCurrentName())
.enableSwagger()
// .fileOverride()
.outputDir(projectPath + "/src/main/java/")
.dateType(DateType.ONLY_DATE)
.disableOpenDir()
;

})
.packageConfig(builder -> {
builder.parent(parentPackage) // 设置父包名
.moduleName(moduleName) // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/main/resources/" + "/mapper/"+moduleName)) // 设置mapperXml生成路径
.mapper("dao")
;
})
.strategyConfig(builder -> {
builder.addInclude(tableName) // 设置需要生成的表名
.addTablePrefix("t_", "app_", "sys_")// 设置过滤表前缀
.entityBuilder().superClass(MyEntity.class).enableLombok().addIgnoreColumns(commonField)
.controllerBuilder().superClass(MyController.class)
.serviceBuilder().superServiceClass(MyService.class)
.superServiceImplClass(MyServiceImpl.class)
.mapperBuilder().superClass(MyMapper.class)

;
})
.execute();
}

private static String getCurrentName() {
try {
Process pop = Runtime.getRuntime().exec("git config user.name");
InputStream inputStream = pop.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line;
if ((line = br.readLine()) != null) {
return line;
}
} catch (IOException e) {
e.printStackTrace();
}

return "";
}

}