|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+package com.unissoft;
|
|
|
2
|
+
|
|
|
3
|
+
|
|
|
4
|
+import com.baomidou.mybatisplus.annotation.DbType;
|
|
|
5
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
6
|
+import com.baomidou.mybatisplus.generator.AutoGenerator;
|
|
|
7
|
+import com.baomidou.mybatisplus.generator.InjectionConfig;
|
|
|
8
|
+import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
|
|
9
|
+import com.baomidou.mybatisplus.generator.config.GlobalConfig;
|
|
|
10
|
+import com.baomidou.mybatisplus.generator.config.PackageConfig;
|
|
|
11
|
+import com.baomidou.mybatisplus.generator.config.StrategyConfig;
|
|
|
12
|
+import com.baomidou.mybatisplus.generator.config.rules.DateType;
|
|
|
13
|
+import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
|
|
14
|
+import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
|
|
15
|
+import freemarker.log.Logger;
|
|
|
16
|
+
|
|
|
17
|
+import java.util.Date;
|
|
|
18
|
+import java.util.HashMap;
|
|
|
19
|
+import java.util.Map;
|
|
|
20
|
+
|
|
|
21
|
+/**
|
|
|
22
|
+ * 文件描述
|
|
|
23
|
+ *
|
|
|
24
|
+ * @author tongxi.xia
|
|
|
25
|
+ * @date 2020年11月26日 15:27
|
|
|
26
|
+ */
|
|
|
27
|
+public class
|
|
|
28
|
+CodeGenerator {
|
|
|
29
|
+
|
|
|
30
|
+ private static Logger log = Logger.getLogger(CodeGenerator.class.toString());
|
|
|
31
|
+
|
|
|
32
|
+ //用户名:SUPLIS 密码:plansuplis
|
|
|
33
|
+ //用户:SUPPLAN 密码:plansup
|
|
|
34
|
+ //用户:CSGX 密码:gxcs
|
|
|
35
|
+ //用户:CSGXUSER 密码:usercsgx
|
|
|
36
|
+ //用户:PLATFORM 密码:
|
|
|
37
|
+ private static final String jdbcUserName = "root";
|
|
|
38
|
+ private static final String jdbcPassword = "123456";
|
|
|
39
|
+
|
|
|
40
|
+ //表名,可以设置多个,通过英文逗号分隔
|
|
|
41
|
+ //数据库用户密码,必须使用表的所有者(Owner)
|
|
|
42
|
+ private static final String tables = "sys_user";
|
|
|
43
|
+ private static final String moduleName = "";//模块名称,分业务,不要漏最后面一个点
|
|
|
44
|
+
|
|
|
45
|
+ private static final String tablePrefix = "";//表名前缀
|
|
|
46
|
+
|
|
|
47
|
+ /**
|
|
|
48
|
+ * 代码生成器的配置常量
|
|
|
49
|
+ */
|
|
|
50
|
+ private static final String outPutDir = "/csc-szls-generation/src/main/java";
|
|
|
51
|
+ //Oracle:jdbc:oracle:thin:@192.168.110.2:1521:ORAPLAN
|
|
|
52
|
+ //mysql:jdbc:mysql://localhost:3306/mydbone?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&pinGlobalTxToPhysicalConnection=true&autoReconnect=true
|
|
|
53
|
+ private static final String jdbcUrl = "jdbc:mysql://localhost:3306/csc-szls?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&pinGlobalTxToPhysicalConnection=true&autoReconnect=true";
|
|
|
54
|
+
|
|
|
55
|
+ //oracle.jdbc.OracleDriver
|
|
|
56
|
+ //oracle.jdbc.driver.OracleDriver
|
|
|
57
|
+ //com.mysql.cj.jdbc.Driver
|
|
|
58
|
+ private static final String jdbcDriverClassName = "com.mysql.jdbc.Driver";
|
|
|
59
|
+
|
|
|
60
|
+ private static final String parentPackage = "com.unissoft";
|
|
|
61
|
+
|
|
|
62
|
+ private static final String authorName = jdbcUserName;//作者
|
|
|
63
|
+
|
|
|
64
|
+ private static final String mapperPattern = "%sMapper";//dao文件命名格式
|
|
|
65
|
+ private static final String mapperName = moduleName + "mapper";
|
|
|
66
|
+ private static final String xmlName = mapperName;
|
|
|
67
|
+
|
|
|
68
|
+ private static final String serviceNamePattern = "%sService";//Service文件命名格式
|
|
|
69
|
+ private static final String serviceName = moduleName + "service";
|
|
|
70
|
+ private static final String implName = moduleName + "service.impl";
|
|
|
71
|
+ private static final String pojoName = moduleName + "model";
|
|
|
72
|
+ private static final String controllerName = moduleName + "controller";
|
|
|
73
|
+
|
|
|
74
|
+
|
|
|
75
|
+ // 当前工程路径 配合outPutDir使用,例如多模块开发 Demo/test1,Demo/test2
|
|
|
76
|
+ // projectPath拿到的是Demo路径,把outPutDir设置成/test1即可
|
|
|
77
|
+ private static final String projectPath = System.getProperty("user.dir");
|
|
|
78
|
+
|
|
|
79
|
+ public static void generator() {
|
|
|
80
|
+
|
|
|
81
|
+ // 全局配置
|
|
|
82
|
+ GlobalConfig gc = new GlobalConfig();
|
|
|
83
|
+ gc.setOutputDir(projectPath + outPutDir);
|
|
|
84
|
+ gc.setAuthor(authorName);
|
|
|
85
|
+ gc.setSwagger2(true);
|
|
|
86
|
+ gc.setBaseResultMap(true);
|
|
|
87
|
+ gc.setBaseColumnList(true);
|
|
|
88
|
+ // 覆盖生成的文件
|
|
|
89
|
+ gc.setFileOverride(true);
|
|
|
90
|
+ gc.setServiceName(serviceNamePattern);
|
|
|
91
|
+ gc.setMapperName(mapperPattern);
|
|
|
92
|
+ gc.setEnableCache(false);//是否在xml中添加二级缓存配置:false不生成
|
|
|
93
|
+ gc.setOpen(false);//true:生成文件后,自动打开文件夹
|
|
|
94
|
+ gc.setDateType(DateType.ONLY_DATE);//设置时间类型
|
|
|
95
|
+ gc.setSwagger2(true);//设置生成Swagger2 Api注解
|
|
|
96
|
+
|
|
|
97
|
+ // 数据源配置
|
|
|
98
|
+ DataSourceConfig dsc = new DataSourceConfig();
|
|
|
99
|
+ dsc.setDbType(DbType.MYSQL);
|
|
|
100
|
+
|
|
|
101
|
+
|
|
|
102
|
+ dsc.setUrl(jdbcUrl);
|
|
|
103
|
+ dsc.setDriverName(jdbcDriverClassName);
|
|
|
104
|
+ dsc.setUsername(jdbcUserName);
|
|
|
105
|
+ dsc.setPassword(jdbcPassword);
|
|
|
106
|
+ // dsc.setSchemaName("public");
|
|
|
107
|
+
|
|
|
108
|
+
|
|
|
109
|
+ // 包配置
|
|
|
110
|
+ PackageConfig pc = new PackageConfig();
|
|
|
111
|
+ //pc.setModuleName(scanner("模块名"));
|
|
|
112
|
+ pc.setParent(parentPackage);
|
|
|
113
|
+
|
|
|
114
|
+ pc.setParent(parentPackage);
|
|
|
115
|
+ pc.setMapper(mapperName);
|
|
|
116
|
+ pc.setEntity(pojoName);
|
|
|
117
|
+ pc.setService(serviceName);
|
|
|
118
|
+ pc.setController(controllerName);
|
|
|
119
|
+ pc.setServiceImpl(implName);
|
|
|
120
|
+ pc.setXml(xmlName);
|
|
|
121
|
+
|
|
|
122
|
+
|
|
|
123
|
+ // 策略配置
|
|
|
124
|
+ StrategyConfig strategy = new StrategyConfig();
|
|
|
125
|
+ strategy.setNaming(NamingStrategy.underline_to_camel);
|
|
|
126
|
+ strategy.setColumnNaming(NamingStrategy.underline_to_camel);
|
|
|
127
|
+ //strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
|
|
|
128
|
+ strategy.setEntityLombokModel(true);//设置Lombok模式
|
|
|
129
|
+ strategy.setRestControllerStyle(true);
|
|
|
130
|
+ strategy.setControllerMappingHyphenStyle(false);
|
|
|
131
|
+ // 公共父类
|
|
|
132
|
+ //strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
|
|
|
133
|
+ // 写于父类中的公共字段
|
|
|
134
|
+ //strategy.setSuperEntityColumns("id");
|
|
|
135
|
+ strategy.setControllerMappingHyphenStyle(true);
|
|
|
136
|
+ if (!StringUtils.isBlank(tablePrefix)) {
|
|
|
137
|
+ strategy.setTablePrefix(tablePrefix);
|
|
|
138
|
+ }
|
|
|
139
|
+ //strategy.entityTableFieldAnnotationEnable(true);
|
|
|
140
|
+ strategy.setEntityTableFieldAnnotationEnable(true);
|
|
|
141
|
+ strategy.setInclude(tables.toUpperCase().split(","));//Oracle数据,表名一定要大写,且表名要对应创建的用户,否则不生成代码
|
|
|
142
|
+ InjectionConfig injectionConfig = new InjectionConfig() {
|
|
|
143
|
+ @Override
|
|
|
144
|
+ public void initMap() {
|
|
|
145
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
146
|
+ map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
|
|
|
147
|
+ this.setMap(map);
|
|
|
148
|
+ }
|
|
|
149
|
+ };
|
|
|
150
|
+ // 代码生成器
|
|
|
151
|
+ AutoGenerator mpg = new AutoGenerator();
|
|
|
152
|
+ mpg.setGlobalConfig(gc);
|
|
|
153
|
+ mpg.setDataSource(dsc);
|
|
|
154
|
+ mpg.setPackageInfo(pc);
|
|
|
155
|
+ mpg.setStrategy(strategy);
|
|
|
156
|
+ mpg.setCfg(injectionConfig);
|
|
|
157
|
+ mpg.setTemplateEngine(new FreemarkerTemplateEngine());
|
|
|
158
|
+ mpg.execute();
|
|
|
159
|
+ log.info("代码生成已经完成。" + new Date());
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+
|
|
|
163
|
+ public static void main(String[] args) {
|
|
|
164
|
+ generator();
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+}
|