Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

@SpringBootApplication
public class GeneratorWebApplication {
public static void main(String[] args) {
SpringApplication.run(GeneratorWebApplication.class,args);
}

public static void main(String[] args) {
SpringApplication.run(GeneratorWebApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.softdev.system.generator.config;
import com.softdev.system.generator.entity.ReturnT;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public ReturnT defaultExceptionHandler(HttpServletRequest req,Exception e) {
e.printStackTrace();
return ReturnT.ERROR(e.getMessage());
}
}
package com.softdev.system.generator.config;

import com.softdev.system.generator.entity.ReturnT;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalDefaultExceptionHandler {

@ExceptionHandler(Exception.class)
@ResponseBody
public ReturnT defaultExceptionHandler(HttpServletRequest req, Exception e) {
e.printStackTrace();
return ReturnT.ERROR(e.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
package com.softdev.system.generator.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/**
* 2019-2-11 liutf WebMvcConfig 整合 cors 和 SpringMvc MessageConverter
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

/* @Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedHeaders("x-requested-with")
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE")
.maxAge(3600);
}*/

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//FastJsonHttpMessageConverter
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);

FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setCharset(StandardCharsets.UTF_8);
fastConverter.setFastJsonConfig(fastJsonConfig);

//StringHttpMessageConverter
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setDefaultCharset(StandardCharsets.UTF_8);
stringConverter.setSupportedMediaTypes(fastMediaTypes);
converters.add(stringConverter);
converters.add(fastConverter);
}
}
package com.softdev.system.generator.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/**
* 2019-2-11 liutf WebMvcConfig 整合 cors 和 SpringMvc MessageConverter
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

/* @Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedHeaders("x-requested-with")
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE")
.maxAge(3600);
}*/

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//FastJsonHttpMessageConverter
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);

FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setCharset(StandardCharsets.UTF_8);
fastConverter.setFastJsonConfig(fastJsonConfig);

//StringHttpMessageConverter
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setDefaultCharset(StandardCharsets.UTF_8);
stringConverter.setSupportedMediaTypes(fastMediaTypes);
converters.add(stringConverter);
converters.add(fastConverter);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* spring boot code generator
*
* @author zhengk/moshow
*/
@Controller
Expand All @@ -34,29 +35,37 @@ public String index() {

@PostMapping("/genCode")
@ResponseBody
public ReturnT codeGenerate(@RequestBody ParamInfo paramInfo ) throws Exception {
public ReturnT codeGenerate(@RequestBody ParamInfo paramInfo) throws Exception {

if (paramInfo.getTableSql().trim().length()<1) {
if (paramInfo.getTableSql().trim().length() < 1) {
return ReturnT.ERROR("表结构信息不可为空");
}

//1.Parse Table Structure 表结构解析
ClassInfo classInfo = null;
switch (paramInfo.getDataType()){
switch (paramInfo.getDataType()) {
//JSON模式:parse field from json string
case "json":classInfo = TableParseUtil.processJsonToClassInfo(paramInfo);break;
case "json":
classInfo = TableParseUtil.processJsonToClassInfo(paramInfo);
break;
//INSERT SQL模式:parse field from insert sql
case "insert-sql":classInfo = TableParseUtil.processInsertSqlToClassInfo(paramInfo);break;
case "insert-sql":
classInfo = TableParseUtil.processInsertSqlToClassInfo(paramInfo);
break;
//正则表达式模式(非完善版本):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 : classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);break;
default:
classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);
break;
}

//2.Set the params 设置表格参数
Map<String, Object> params = new HashMap<String, Object>(8);
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("packageName", paramInfo.getPackageName());
params.put("returnUtil", paramInfo.getReturnUtil());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ClassInfo {

private String tableName;
private String className;
private String classComment;
private List<FieldInfo> fieldList;
private String classComment;
private List<FieldInfo> fieldList;

}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package com.softdev.system.generator.entity;

import lombok.Data;

/**
* Post data - ParamInfo
* @author zhengkai.blog.csdn.net
*/
@Data
public class ParamInfo {
private String tableSql;
private String authorName;
private String packageName;
private String returnUtil;
private String nameCaseType;
private String tinyintTransType;
private String dataType;
private boolean swagger;

@Data
public static class NAME_CASE_TYPE{
public static String CAMEL_CASE="CamelCase";
public static String UNDER_SCORE_CASE="UnderScoreCase";
public static String UPPER_UNDER_SCORE_CASE="UpperUnderScoreCase";
}
}
package com.softdev.system.generator.entity;

import lombok.Data;

/**
* Post data - ParamInfo
*
* @author zhengkai.blog.csdn.net
*/
@Data
public class ParamInfo {

private String tableSql;
private String authorName;
private String packageName;
private String returnUtil;
private String nameCaseType;
private String tinyintTransType;
private String dataType;
private boolean swagger;

@Data
public static class NAME_CASE_TYPE {
public static String CAMEL_CASE = "CamelCase";
public static String UNDER_SCORE_CASE = "UnderScoreCase";
public static String UPPER_UNDER_SCORE_CASE = "UpperUnderScoreCase";
}

}
Loading