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
| import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import javax.validation.constraints.NotNull; import java.io.File; import java.util.Map;
public class CodeVelocityTemplateEngine extends VelocityTemplateEngine {
private static final String VO_KEY = "vo.java";
private static final String PARAM_KEY = "param.java";
@Override protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) { if (!customFile.containsKey(VO_KEY) && !customFile.containsKey(VO_KEY)) { super.outputCustomFile(customFile, tableInfo, objectMap); return; } String entityName = tableInfo.getEntityName(); String otherPath = getPathInfo(OutputFile.other); if (StringUtils.isBlank(otherPath)) { return; } String parent = ((Map<String, String>) objectMap.get("package")).get("Parent"); String packageVo = parent + ".vo"; objectMap.put("packageVo", packageVo);
String packageParam = parent + ".param"; objectMap.put("packageParam", packageParam);
final String finalOtherPath = org.apache.commons.lang3.StringUtils.substringBeforeLast(new File(otherPath).getAbsolutePath(), "\\") + File.separator; customFile.forEach((key, value) -> { if (StringUtils.equals(key, VO_KEY)) { String result = finalOtherPath + "vo"; String fileName = String.format((result + File.separator + entityName + "%s"), "Vo.java"); outputFile(new File(fileName), objectMap, value); } if (StringUtils.equals(key, PARAM_KEY)) { String result = finalOtherPath + "param"; String fileName = String.format((result + File.separator + entityName + "%s"), "Param.java"); outputFile(new File(fileName), objectMap, value); }
}); }
}
|