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
package com.tanpu.community.dao;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
public class CodeAutoGenerator {
public static void main(String[] args) {
String currentPath = System.getProperty("user.dir");
String codeBaseHome = "/community-service/src/main/java";
String author = "xudong";
String mysqlUserName = "tamp_admin";
String mysqlPassword = "@imeng123";
String jdbcUrl = "jdbc:mysql://rm-uf6r22t3d798q4kmkao.mysql.rds.aliyuncs.com:3306/tamp_community";
// String[] tables = new String[]{"theme"};
String[] tables = new String[]{"visit_log"};
String basePackage = "com.tanpu.community";
String mapperPackage = "dao.mapper.community";
String entityPackage = "dao.entity.community";
String xmlPath = "D:\\IdeaProjects\\tanpu-community\\community-service\\src\\main\\resources\\mapper\\community";
AutoGenerator autoGenerator = new AutoGenerator();
//全局配置
GlobalConfig gc = new GlobalConfig();
String path = currentPath + codeBaseHome;
gc.setOutputDir(path); //生成文件输出根目录
gc.setOpen(false);//生成完成后不弹出文件框
gc.setAuthor(author);
gc.setFileOverride(true); //文件覆盖
gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setSwagger2(true);
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setEntityName("%sEntity");
autoGenerator.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL); //设置数据库类型,我是postgresql
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(mysqlUserName);
dsc.setPassword(mysqlPassword);
dsc.setUrl(jdbcUrl); //指定数据库
autoGenerator.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel); // 表名生成策略
strategy.setInclude(tables); // 需要生成的表
// strategy.setSuperEntityClass(BaseEntity.class);
// strategy.setSuperEntityColumns("create_by", "update_by", "create_time", "update_time", "delete_tag");
// strategy.setSuperServiceClass(null);
// strategy.setSuperServiceImplClass(null);
// strategy.setSuperMapperClass(null);
autoGenerator.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(basePackage);
// pc.setController(controllerPackage);
// pc.setService(servicePackage);
pc.setMapper(mapperPackage);
pc.setEntity(entityPackage);
autoGenerator.setPackageInfo(pc);
// 模板配置
TemplateConfig tc = new TemplateConfig();
tc.setController(null);
tc.setService(null);
tc.setServiceImpl(null);
tc.setXml(null);
autoGenerator.setTemplate(tc);
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return xmlPath + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
autoGenerator.setCfg(cfg);
// 执行生成
autoGenerator.execute();
}
}