提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.gen.core.generator.base; |
懒 |
2 |
|
|
3 |
|
|
4 |
import cn.stylefeng.guns.gen.core.engine.GunsMpVelocityTemplateEngine; |
|
5 |
import com.baomidou.mybatisplus.generator.AutoGenerator; |
|
6 |
import com.baomidou.mybatisplus.generator.InjectionConfig; |
|
7 |
import com.baomidou.mybatisplus.generator.config.*; |
|
8 |
import com.baomidou.mybatisplus.generator.config.po.TableInfo; |
|
9 |
import lombok.extern.slf4j.Slf4j; |
|
10 |
|
|
11 |
import java.util.HashMap; |
|
12 |
import java.util.List; |
|
13 |
import java.util.Map; |
|
14 |
|
|
15 |
/** |
|
16 |
* 代码生成器规范 |
|
17 |
* |
|
18 |
* @author fengshuonan |
|
19 |
* @date 2018-12-12-2:41 PM |
|
20 |
*/ |
|
21 |
@Slf4j |
|
22 |
public abstract class AbstractMpGenerator extends Generator { |
|
23 |
|
|
24 |
/** |
|
25 |
* mybatis-plus代码生成器配置 |
|
26 |
*/ |
|
27 |
protected GlobalConfig globalConfig = new GlobalConfig(); |
|
28 |
|
|
29 |
protected DataSourceConfig dataSourceConfig = new DataSourceConfig(); |
|
30 |
|
|
31 |
protected StrategyConfig strategyConfig = new StrategyConfig(); |
|
32 |
|
|
33 |
protected PackageConfig packageConfig = new PackageConfig(); |
|
34 |
|
|
35 |
protected TemplateConfig templateConfig = new TemplateConfig(); |
|
36 |
|
|
37 |
protected InjectionConfig injectionConfig = new InjectionConfig() { |
|
38 |
@Override |
|
39 |
public void initMap() { |
|
40 |
Map<String, String> packageInfo = this.getConfig().getPackageInfo(); |
|
41 |
if (this.getMap() != null && this.getMap().size() > 0) { |
|
42 |
for (Map.Entry<String, Object> entry : this.getMap().entrySet()) { |
|
43 |
packageInfo.put(entry.getKey(), (String) entry.getValue()); |
|
44 |
} |
|
45 |
} |
|
46 |
} |
|
47 |
}; |
|
48 |
|
|
49 |
/** |
|
50 |
* mybatis plus生成之后可以获取到他的table元数据,加以利用 |
|
51 |
*/ |
|
52 |
protected List<TableInfo> tableInfos = null; |
|
53 |
protected Map<String, Map<String, Object>> everyTableContexts = new HashMap<>(); |
|
54 |
|
|
55 |
/** |
|
56 |
* 执行mybatis-plus的代码生成 |
|
57 |
* |
|
58 |
* @author fengshuonan |
|
59 |
* @Date 2018/12/13 11:36 AM |
|
60 |
*/ |
|
61 |
@Override |
|
62 |
public void doGeneration() { |
|
63 |
|
|
64 |
beforeGeneration(); |
|
65 |
|
|
66 |
AutoGenerator autoGenerator = new AutoGenerator(); |
|
67 |
|
|
68 |
//使用重写版的mp代码生成器 |
|
69 |
autoGenerator.setTemplateEngine(new GunsMpVelocityTemplateEngine()); |
|
70 |
|
|
71 |
autoGenerator.setGlobalConfig(globalConfig); |
|
72 |
autoGenerator.setDataSource(dataSourceConfig); |
|
73 |
autoGenerator.setStrategy(strategyConfig); |
|
74 |
autoGenerator.setTemplate(templateConfig); |
|
75 |
autoGenerator.setPackageInfo(packageConfig); |
|
76 |
autoGenerator.setCfg(injectionConfig); |
|
77 |
autoGenerator.execute(); |
|
78 |
|
|
79 |
//获取table信息,用于其他代码生成 |
|
80 |
tableInfos = autoGenerator.getConfig().getTableInfoList(); |
|
81 |
|
|
82 |
//获取mp代码生成时候的所有变量 |
|
83 |
for (TableInfo tableInfo : tableInfos) { |
|
84 |
Map<String, Object> tableContextMap = autoGenerator.getTemplateEngine().getObjectMap(tableInfo); |
|
85 |
everyTableContexts.put(tableInfo.getName(), tableContextMap); |
|
86 |
} |
|
87 |
|
|
88 |
afterGeneration(); |
|
89 |
} |
|
90 |
|
|
91 |
public List<TableInfo> getTableInfos() { |
|
92 |
return tableInfos; |
|
93 |
} |
|
94 |
|
|
95 |
public Map<String, Map<String, Object>> getEveryTableContexts() { |
|
96 |
return everyTableContexts; |
|
97 |
} |
|
98 |
} |