mirror of
https://github.com/moshowgame/SpringBootCodeGenerator.git
synced 2026-03-22 15:39:04 +08:00
Merge pull request #83 from gaohanghang/master
1. 通过 InetAddress 类动态获取主机的ip地址 2. 折叠 catch 块
This commit is contained in:
@@ -4,9 +4,12 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import java.net.Inet4Address;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description 动态获取tomcat启动端口,控制台打印项目访问地址
|
* @Description 通过实现ApplicationListener接口动态获取tomcat启动端口和访问路径,通过InetAddress类获取主机的ip地址,最后控制台打印项目访问地址
|
||||||
* @Author Gao Hang Hang
|
* @Author Gao Hang Hang
|
||||||
* @Date 2019-12-27 14:37
|
* @Date 2019-12-27 14:37
|
||||||
**/
|
**/
|
||||||
@@ -14,15 +17,20 @@ import org.springframework.stereotype.Component;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
|
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
|
||||||
|
|
||||||
private int serverPort;
|
|
||||||
private String serverPath;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(WebServerInitializedEvent event) {
|
public void onApplicationEvent(WebServerInitializedEvent event) {
|
||||||
this.serverPort = event.getWebServer().getPort();
|
try {
|
||||||
//新增动态path by zhengkai
|
InetAddress inetAddress = Inet4Address.getLocalHost();
|
||||||
this.serverPath = event.getApplicationContext().getApplicationName();
|
// 主机ip地址
|
||||||
log.info("项目启动启动成功!访问地址: http://localhost:{}{}", serverPort,serverPath);
|
String hostAddress = inetAddress.getHostAddress();
|
||||||
|
// tomcat启动端口
|
||||||
|
int serverPort = event.getWebServer().getPort();
|
||||||
|
// 新增动态path by zhengkai
|
||||||
|
String serverPath = event.getApplicationContext().getApplicationName();
|
||||||
|
log.info("项目启动成功!访问地址: http://{}:{}{}", hostAddress, serverPort, serverPath);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,90 +1,87 @@
|
|||||||
package com.softdev.system.generator.controller;
|
package com.softdev.system.generator.controller;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.softdev.system.generator.entity.ClassInfo;
|
import com.softdev.system.generator.entity.ClassInfo;
|
||||||
import com.softdev.system.generator.entity.ParamInfo;
|
import com.softdev.system.generator.entity.ParamInfo;
|
||||||
import com.softdev.system.generator.entity.ReturnT;
|
import com.softdev.system.generator.entity.ReturnT;
|
||||||
import com.softdev.system.generator.service.GeneratorService;
|
import com.softdev.system.generator.service.GeneratorService;
|
||||||
import com.softdev.system.generator.util.CodeGenerateException;
|
import com.softdev.system.generator.util.CodeGenerateException;
|
||||||
import com.softdev.system.generator.util.TableParseUtil;
|
import com.softdev.system.generator.util.TableParseUtil;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* spring boot code generator
|
* spring boot code generator
|
||||||
* @author zhengk/moshow
|
* @author zhengk/moshow
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class IndexController {
|
public class IndexController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private GeneratorService generatorService;
|
private GeneratorService generatorService;
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public String index() {
|
public String index() {
|
||||||
return "index";
|
return "index";
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/genCode")
|
@PostMapping("/genCode")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ReturnT<Map<String, String>> codeGenerate(@RequestBody ParamInfo paramInfo ) {
|
public ReturnT<Map<String, String>> codeGenerate(@RequestBody ParamInfo paramInfo ) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (StringUtils.isBlank(paramInfo.getTableSql())) {
|
if (StringUtils.isBlank(paramInfo.getTableSql())) {
|
||||||
return new ReturnT<>(ReturnT.FAIL_CODE, "表结构信息不可为空");
|
return new ReturnT<>(ReturnT.FAIL_CODE, "表结构信息不可为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse table
|
// parse table
|
||||||
ClassInfo classInfo = null;
|
ClassInfo classInfo = null;
|
||||||
switch (paramInfo.getDataType()){
|
switch (paramInfo.getDataType()){
|
||||||
//parse json
|
//parse json
|
||||||
case "json":classInfo = TableParseUtil.processJsonToClassInfo(paramInfo);break;
|
case "json":classInfo = TableParseUtil.processJsonToClassInfo(paramInfo);break;
|
||||||
//parse sql by regex
|
//parse sql by regex
|
||||||
case "sql-regex":classInfo = TableParseUtil.processTableToClassInfoByRegex(paramInfo);break;
|
case "sql-regex":classInfo = TableParseUtil.processTableToClassInfoByRegex(paramInfo);break;
|
||||||
//default parse sql by java
|
//default parse sql by java
|
||||||
default : classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);break;
|
default : classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// process the param
|
// process the param
|
||||||
Map<String, Object> params = new HashMap<String, Object>(8);
|
Map<String, Object> params = new HashMap<String, Object>(8);
|
||||||
params.put("classInfo", classInfo);
|
params.put("classInfo", classInfo);
|
||||||
params.put("tableName", classInfo==null?System.currentTimeMillis():classInfo.getTableName());
|
params.put("tableName", classInfo==null?System.currentTimeMillis():classInfo.getTableName());
|
||||||
params.put("authorName", paramInfo.getAuthorName());
|
params.put("authorName", paramInfo.getAuthorName());
|
||||||
params.put("packageName", paramInfo.getPackageName());
|
params.put("packageName", paramInfo.getPackageName());
|
||||||
params.put("returnUtil", paramInfo.getReturnUtil());
|
params.put("returnUtil", paramInfo.getReturnUtil());
|
||||||
params.put("swagger", paramInfo.isSwagger());
|
params.put("swagger", paramInfo.isSwagger());
|
||||||
|
|
||||||
//log the params
|
//log the params
|
||||||
//log.info(JSON.toJSONString(paramInfo));
|
//log.info(JSON.toJSONString(paramInfo));
|
||||||
|
|
||||||
log.info("generator table:"+(classInfo==null?"":classInfo.getTableName())
|
log.info("generator table:"+(classInfo==null?"":classInfo.getTableName())
|
||||||
+",field size:"+((classInfo==null||classInfo.getFieldList()==null)?"":classInfo.getFieldList().size()));
|
+",field size:"+((classInfo==null||classInfo.getFieldList()==null)?"":classInfo.getFieldList().size()));
|
||||||
|
|
||||||
// generate the code 需要加新的模板请在里面改
|
// generate the code 需要加新的模板请在里面改
|
||||||
Map<String, String> result = generatorService.getResultByParams(params);
|
Map<String, String> result = generatorService.getResultByParams(params);
|
||||||
|
|
||||||
return new ReturnT<>(result);
|
return new ReturnT<>(result);
|
||||||
} catch (IOException | TemplateException e) {
|
} catch (IOException | TemplateException | CodeGenerateException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
|
return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
|
||||||
} catch (CodeGenerateException e) {
|
}
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user