提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright (c) 2011-2020, hubin (jobob@qq.com). |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
5 |
* use this file except in compliance with the License. You may obtain a copy of |
|
6 |
* the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
12 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
13 |
* License for the specific language governing permissions and limitations under |
|
14 |
* the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.gen.core.engine; |
|
17 |
|
|
18 |
import cn.stylefeng.guns.gen.modular.model.FieldConfig; |
|
19 |
import cn.stylefeng.guns.gen.modular.model.GenSessionFieldConfigs; |
|
20 |
import cn.stylefeng.guns.gen.core.util.FieldsConfigHolder; |
|
21 |
import cn.stylefeng.guns.gen.core.util.TableInfoUtil; |
|
22 |
import com.baomidou.mybatisplus.core.toolkit.StringPool; |
|
23 |
import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
|
24 |
import com.baomidou.mybatisplus.generator.config.ConstVal; |
|
25 |
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder; |
|
26 |
import com.baomidou.mybatisplus.generator.config.po.TableInfo; |
|
27 |
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine; |
|
28 |
import org.apache.velocity.Template; |
|
29 |
import org.apache.velocity.VelocityContext; |
|
30 |
import org.apache.velocity.app.Velocity; |
|
31 |
import org.apache.velocity.app.VelocityEngine; |
|
32 |
|
|
33 |
import java.io.BufferedWriter; |
|
34 |
import java.io.FileOutputStream; |
|
35 |
import java.io.OutputStreamWriter; |
|
36 |
import java.util.List; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.Properties; |
|
39 |
import java.util.stream.Collectors; |
|
40 |
|
|
41 |
/** |
|
42 |
* <p> |
|
43 |
* 重写Mybatis-Plus的Velocity模板引擎实现 |
|
44 |
* </p> |
|
45 |
* |
|
46 |
* @author hubin, fengshuonan |
|
47 |
* @since 2018-01-10 |
|
48 |
*/ |
|
49 |
public class GunsMpVelocityTemplateEngine extends VelocityTemplateEngine { |
|
50 |
|
|
51 |
private static final String DOT_VM = ".vm"; |
|
52 |
private VelocityEngine velocityEngine; |
|
53 |
|
|
54 |
@Override |
|
55 |
public VelocityTemplateEngine init(ConfigBuilder configBuilder) { |
|
56 |
super.init(configBuilder); |
|
57 |
if (null == velocityEngine) { |
|
58 |
Properties p = new Properties(); |
|
59 |
p.setProperty(ConstVal.VM_LOAD_PATH_KEY, ConstVal.VM_LOAD_PATH_VALUE); |
|
60 |
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, StringPool.EMPTY); |
|
61 |
p.setProperty(Velocity.ENCODING_DEFAULT, ConstVal.UTF8); |
|
62 |
p.setProperty(Velocity.INPUT_ENCODING, ConstVal.UTF8); |
|
63 |
p.setProperty("file.resource.loader.unicode", StringPool.TRUE); |
|
64 |
velocityEngine = new VelocityEngine(p); |
|
65 |
} |
|
66 |
return this; |
|
67 |
} |
|
68 |
|
|
69 |
|
|
70 |
@Override |
|
71 |
public void writer(Map<String, Object> objectMap, String templatePath, String outputFile) throws Exception { |
|
72 |
if (StringUtils.isEmpty(templatePath)) { |
|
73 |
return; |
|
74 |
} |
|
75 |
Template template = velocityEngine.getTemplate(templatePath, ConstVal.UTF8); |
|
76 |
try (FileOutputStream fos = new FileOutputStream(outputFile); |
|
77 |
OutputStreamWriter ow = new OutputStreamWriter(fos, ConstVal.UTF8); |
|
78 |
BufferedWriter writer = new BufferedWriter(ow)) { |
|
79 |
template.merge(new VelocityContext(objectMap), writer); |
|
80 |
} |
|
81 |
logger.debug("模板:" + templatePath + "; 文件:" + outputFile); |
|
82 |
} |
|
83 |
|
|
84 |
|
|
85 |
@Override |
|
86 |
public String templateFilePath(String filePath) { |
|
87 |
if (null == filePath || filePath.contains(DOT_VM)) { |
|
88 |
return filePath; |
|
89 |
} |
|
90 |
return filePath + DOT_VM; |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* 重写父类的方法,增加一个变量,为了在mapping.xml的Base_Column_List增加base前缀 |
|
95 |
* |
|
96 |
* @author fengshuonan |
|
97 |
* @Date 2019/1/10 12:51 PM |
|
98 |
*/ |
|
99 |
@Override |
|
100 |
public Map<String, Object> getObjectMap(TableInfo tableInfo) { |
|
101 |
Map<String, Object> objectMap = super.getObjectMap(tableInfo); |
|
102 |
objectMap.put("tableRebuild", TableInfoUtil.getFieldNames(tableInfo)); |
|
103 |
|
|
104 |
//获取当前线程变量获取的字段配置信息 |
|
105 |
GenSessionFieldConfigs genSessionFieldConfigs = FieldsConfigHolder.get(); |
|
106 |
List<FieldConfig> fieldConfigs = genSessionFieldConfigs.getFieldConfigs(tableInfo.getName()); |
|
107 |
|
|
108 |
//带条件配置的字段 |
|
109 |
List<FieldConfig> conditionFields = fieldConfigs.stream().filter(FieldConfig::getQueryConditionFlag) |
|
110 |
.collect(Collectors.toList()); |
|
111 |
|
|
112 |
objectMap.put("mapperConditions", conditionFields); |
|
113 |
return objectMap; |
|
114 |
} |
|
115 |
} |