|
@@ -0,0 +1,129 @@
|
|
1
|
+package com.example.demo;
|
|
2
|
+
|
|
3
|
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
4
|
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
5
|
+import com.baomidou.mybatisplus.generator.AutoGenerator;
|
|
6
|
+import com.baomidou.mybatisplus.generator.InjectionConfig;
|
|
7
|
+import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
|
8
|
+import com.baomidou.mybatisplus.generator.config.FileOutConfig;
|
|
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.TemplateConfig;
|
|
13
|
+import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
14
|
+import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
|
15
|
+import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
|
16
|
+import org.springframework.util.StringUtils;
|
|
17
|
+
|
|
18
|
+import java.util.ArrayList;
|
|
19
|
+import java.util.List;
|
|
20
|
+import java.util.Scanner;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+ * @author FXF
|
|
24
|
+ * @date 2018-12-18
|
|
25
|
+ */
|
|
26
|
+public class MybatiesPlus {
|
|
27
|
+
|
|
28
|
+ * <p>
|
|
29
|
+ * 读取控制台内容
|
|
30
|
+ * </p>
|
|
31
|
+ */
|
|
32
|
+ public static String scanner(String tip) {
|
|
33
|
+ Scanner scanner = new Scanner(System.in);
|
|
34
|
+ StringBuilder help = new StringBuilder();
|
|
35
|
+ help.append("请输入" + tip + ":");
|
|
36
|
+ System.out.println(help.toString());
|
|
37
|
+ if (scanner.hasNext()) {
|
|
38
|
+ String ipt = scanner.next();
|
|
39
|
+ if (!StringUtils.isEmpty(ipt)) {
|
|
40
|
+ return ipt;
|
|
41
|
+ }
|
|
42
|
+ }
|
|
43
|
+ throw new MybatisPlusException("请输入正确的" + tip + "!");
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public static void main(String[] args) {
|
|
47
|
+
|
|
48
|
+ AutoGenerator mpg = new AutoGenerator();
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+ GlobalConfig gc = new GlobalConfig();
|
|
52
|
+ String projectPath = System.getProperty("user.dir");
|
|
53
|
+ gc.setOutputDir(projectPath + "/src/main/java");
|
|
54
|
+ gc.setAuthor("jobob");
|
|
55
|
+ gc.setOpen(false);
|
|
56
|
+ mpg.setGlobalConfig(gc);
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+ DataSourceConfig dsc = new DataSourceConfig();
|
|
60
|
+ dsc.setUrl("jdbc:mysql://rm-uf6z3z6jq11x653d77o.mysql.rds.aliyuncs.com:3306/smart_community_dev?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true");
|
|
61
|
+
|
|
62
|
+ dsc.setDriverName("com.mysql.jdbc.Driver");
|
|
63
|
+ dsc.setUsername("root");
|
|
64
|
+ dsc.setPassword("DQ@0lW##kBb2+-jPZC1s$Ma0h5$9W((q");
|
|
65
|
+ mpg.setDataSource(dsc);
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+ PackageConfig pc = new PackageConfig();
|
|
69
|
+ pc.setModuleName(scanner("模块名"));
|
|
70
|
+ pc.setParent("com.community.huiju");
|
|
71
|
+ mpg.setPackageInfo(pc);
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+ InjectionConfig cfg = new InjectionConfig() {
|
|
75
|
+ @Override
|
|
76
|
+ public void initMap() {
|
|
77
|
+
|
|
78
|
+ }
|
|
79
|
+ };
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+ String templatePath = "/templates/mapper.xml.ftl";
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+ List<FileOutConfig> focList = new ArrayList<>();
|
|
88
|
+
|
|
89
|
+ focList.add(new FileOutConfig(templatePath) {
|
|
90
|
+ @Override
|
|
91
|
+ public String outputFile(TableInfo tableInfo) {
|
|
92
|
+
|
|
93
|
+ return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
|
|
94
|
+ + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
|
|
95
|
+ }
|
|
96
|
+ });
|
|
97
|
+
|
|
98
|
+ cfg.setFileOutConfigList(focList);
|
|
99
|
+ mpg.setCfg(cfg);
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+ TemplateConfig templateConfig = new TemplateConfig();
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+ templateConfig.setXml(null);
|
|
110
|
+ mpg.setTemplate(templateConfig);
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+ StrategyConfig strategy = new StrategyConfig();
|
|
114
|
+ strategy.setNaming(NamingStrategy.underline_to_camel);
|
|
115
|
+ strategy.setColumnNaming(NamingStrategy.underline_to_camel);
|
|
116
|
+
|
|
117
|
+ strategy.setEntityLombokModel(true);
|
|
118
|
+ strategy.setRestControllerStyle(true);
|
|
119
|
+
|
|
120
|
+ strategy.setInclude(scanner("表名"));
|
|
121
|
+ strategy.setSuperEntityColumns("id");
|
|
122
|
+ strategy.setControllerMappingHyphenStyle(true);
|
|
123
|
+ strategy.setTablePrefix(pc.getModuleName() + "_");
|
|
124
|
+ mpg.setStrategy(strategy);
|
|
125
|
+ mpg.setTemplateEngine(new FreemarkerTemplateEngine());
|
|
126
|
+ mpg.execute();
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+}
|