先po張效果圖
如圖
- 輸入entity名字
-
選擇項目, 具體可以用項目中的模塊來做
這個工具目前生成的內容包含:DTO,Controller, Service, Mapper, sqlmap 另外還會根據yaml配置文件在dev環境下生成基本的數據表結構,其中包含了幾個常見接口,比如,添加、更新、刪除、根據id查詢,列表查詢這些接口。
使用方法:
1. 獨立啓動,瀏覽器訪問項目根目錄/
2. 添入實體名,屬性,並選擇對應的項目
3. (可選)在高級選項添加入註釋的作者或者表前綴等信息
4. 點擊一鍵生成
關鍵代碼
@Override
public void generate(GenerateConfig cfg) throws Exception {
long startTime = System.currentTimeMillis();
if (StringUtils.isEmpty(cfg.getModelName())) {
ExceptionHolder.throwParaErrorException();
}
Map<String, String> bindMap = getBindMap(cfg);
//生成ResultDto ExceptionHolder etc..
createCommonClass(bindMap, cfg);
//生成model
createModel(bindMap, cfg);
//生成控制器
createController(bindMap, cfg);
//生成service
//createService(bindMap, cfg);
//生成service 實現類
createServiceImpl(bindMap, cfg);
//生成dao
createMapper(bindMap, cfg);
//生成sqlmap
if (cfg.getIsCreateSqlMap()) {
createSqlMapper(bindMap, cfg);
}
//建表
if (cfg.getIsCreateTable()) {
createTable(cfg);
}
double s = (System.currentTimeMillis() - startTime) / 1000D;
System.out.println("Generate use " + s + " seconds");
}
private void createModel(Map bindMap, GenerateConfig cfg) throws FileNotFoundException {
String model = cfg.getModelName();
Template template = groupTemplate.getTemplate("/Model.tpl");
String modelPackage = getModelPackageToFileSeparator(cfg);
String packagePath = cfg.getBasePackage() + "entity" + modelPackage;
String modelPath = CHAR_SEPARATOR + model + ".java";
String path = getCreatePath(cfg, packagePath);
//fields
String fieldDisplay = getFieldDisplay(cfg);
bindMap.put("fieldDefinition", fieldDisplay);
//setter and getter
String[] split = cfg.getFieldSplit();
StringBuilder setget = new StringBuilder();
for (String e : split) {
if (e.length() > 3) {
String[] field = e.trim().replaceAll(LINE_RN, "").split(LINE_SS);
String methodName = StringUtil.toFirstUpCase(field[1]);
setget.append("\t\tpublic void set" + methodName + "(" + field[0] + " " + field[1] + ")");
setget.append("{\n\t\tthis." + field[1] + " = " + field[1] + ";\n\t}\n");
setget.append("\t\tpublic " + field[0] + " get" + methodName + "()");
setget.append("{\n\t\treturn this." + field[1] + ";\n\t}\n");
}
}
bindMap.put("setget", setget);
rend(template, path, modelPath, bindMap);
}
private void createController(Map<String, String> bindMap, GenerateConfig cfg) throws Exception {
try {
Template template = groupTemplate.getTemplate("/Controller.tpl");
String model = cfg.getModelName();
String modelPackage = getModelPackageToFileSeparator(cfg);
String packagePath = cfg.getBasePackage() + "controller" + modelPackage;
String modelPath = CHAR_SEPARATOR + model + "Controller.java";
String path = getCreatePath(cfg, packagePath);
rend(template, path, modelPath, bindMap);
} catch (Exception e) {
throw e;
}
}
/**
* 生成service
*
* @param bindMap
* @param cfg
* @throws IOException
*/
@Deprecated
private void createService(Map<String, String> bindMap, GenerateConfig cfg) throws IOException {
try {
Template template = groupTemplate.getTemplate("/Service.tpl");
String model = cfg.getModelName();
String modelPackage = getModelPackageToFileSeparator(cfg);
String packagePath = cfg.getBasePackage() + "service" + modelPackage;
String modelPath = CHAR_SEPARATOR + model + "Service.java";
String path = getCreatePath(cfg, packagePath);
rend(template, path, modelPath, bindMap);
} catch (Exception e) {
throw e;
}
}
/**
* 生成service實現類
*
* @param bindMap
* @param cfg
* @throws IOException
*/
private void createServiceImpl(Map<String, String> bindMap, GenerateConfig cfg) throws IOException {
try {
Template template = groupTemplate.getTemplate("/ServiceImpl.tpl");
String model = cfg.getModelName();
String modelPackage = getModelPackageToFileSeparator(cfg);
String packagePath = cfg.getBasePackage() + "service" + modelPackage;
String modelPath = CHAR_SEPARATOR + model + "Service.java";
String path = getCreatePath(cfg, packagePath);
rend(template, path, modelPath, bindMap);
} catch (Exception e) {
throw e;
}
}
private void createMapper(Map bindMap, GenerateConfig cfg) throws Exception {
try {
Template template = groupTemplate.getTemplate("/Mapper.tpl");
String model = cfg.getModelName();
String modelPackage = getModelPackageToFileSeparator(cfg);
String packagePath = cfg.getBasePackage() + "mapper" + modelPackage;
String modelPath = CHAR_SEPARATOR + model + "Mapper.java";
String path = getCreatePath(cfg, packagePath);
rend(template, path, modelPath, bindMap);
} catch (Exception e) {
throw e;
}
}