mirror of
https://github.com/moshowgame/SpringBootCodeGenerator.git
synced 2026-03-22 15:39:04 +08:00
Merge pull request #92 from gaohanghang/master
1. 将 mybatis controller 模板里,类上 @RequestMapping 注解里的 url 改为首字母小写 2. 对 java 代码进行格式化,使其看上去更加美观
This commit is contained in:
@@ -6,7 +6,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class GeneratorWebApplication {
|
public class GeneratorWebApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(GeneratorWebApplication.class, args);
|
SpringApplication.run(GeneratorWebApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ 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.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|||||||
@@ -46,4 +46,5 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
|||||||
converters.add(stringConverter);
|
converters.add(stringConverter);
|
||||||
converters.add(fastConverter);
|
converters.add(fastConverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* spring boot code generator
|
* spring boot code generator
|
||||||
|
*
|
||||||
* @author zhengk/moshow
|
* @author zhengk/moshow
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
@@ -44,13 +45,21 @@ public class IndexController {
|
|||||||
ClassInfo classInfo = null;
|
ClassInfo classInfo = null;
|
||||||
switch (paramInfo.getDataType()) {
|
switch (paramInfo.getDataType()) {
|
||||||
//JSON模式:parse field from json string
|
//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
|
//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
|
//正则表达式模式(非完善版本):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;
|
||||||
}
|
}
|
||||||
|
|
||||||
//2.Set the params 设置表格参数
|
//2.Set the params 设置表格参数
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ import lombok.Data;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Post data - ParamInfo
|
* Post data - ParamInfo
|
||||||
|
*
|
||||||
* @author zhengkai.blog.csdn.net
|
* @author zhengkai.blog.csdn.net
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class ParamInfo {
|
public class ParamInfo {
|
||||||
|
|
||||||
private String tableSql;
|
private String tableSql;
|
||||||
private String authorName;
|
private String authorName;
|
||||||
private String packageName;
|
private String packageName;
|
||||||
@@ -23,4 +25,5 @@ public class ParamInfo {
|
|||||||
public static String UNDER_SCORE_CASE = "UnderScoreCase";
|
public static String UNDER_SCORE_CASE = "UnderScoreCase";
|
||||||
public static String UPPER_UNDER_SCORE_CASE = "UpperUnderScoreCase";
|
public static String UPPER_UNDER_SCORE_CASE = "UpperUnderScoreCase";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ import java.io.Serializable;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* common returnT:公共返回封装类
|
* common returnT:公共返回封装类
|
||||||
|
*
|
||||||
* @author zhengkai.blog.csdn.net
|
* @author zhengkai.blog.csdn.net
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class ReturnT implements Serializable {
|
public class ReturnT implements Serializable {
|
||||||
|
|
||||||
public static final long serialVersionUID = 42L;
|
public static final long serialVersionUID = 42L;
|
||||||
|
|
||||||
public static final int SUCCESS_CODE = 200;
|
public static final int SUCCESS_CODE = 200;
|
||||||
@@ -27,39 +29,50 @@ public class ReturnT implements Serializable {
|
|||||||
this.code = code;
|
this.code = code;
|
||||||
this.msg = msg;
|
this.msg = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReturnT(int code, String msg, Object data) {
|
public ReturnT(int code, String msg, Object data) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.msg = msg;
|
this.msg = msg;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReturnT(Object data) {
|
public ReturnT(Object data) {
|
||||||
this.code = SUCCESS_CODE;
|
this.code = SUCCESS_CODE;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReturnT(Object data, int count) {
|
public ReturnT(Object data, int count) {
|
||||||
this.code = PAGE_CODE;
|
this.code = PAGE_CODE;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.count = count;
|
this.count = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReturnT PAGE(Object data, int count) {
|
public static ReturnT PAGE(Object data, int count) {
|
||||||
return new ReturnT(data, count);
|
return new ReturnT(data, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReturnT PAGE(Object data, long count) {
|
public static ReturnT PAGE(Object data, long count) {
|
||||||
return new ReturnT(data, Integer.parseInt(count + ""));
|
return new ReturnT(data, Integer.parseInt(count + ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReturnT SUCCESS() {
|
public static ReturnT SUCCESS() {
|
||||||
return new ReturnT(SUCCESS_CODE, OPERATION_SUCCESS);
|
return new ReturnT(SUCCESS_CODE, OPERATION_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReturnT SUCCESS(String msg) {
|
public static ReturnT SUCCESS(String msg) {
|
||||||
return new ReturnT(SUCCESS_CODE, msg);
|
return new ReturnT(SUCCESS_CODE, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReturnT SUCCESS(Object data) {
|
public static ReturnT SUCCESS(Object data) {
|
||||||
return new ReturnT(data);
|
return new ReturnT(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReturnT ERROR(String msg) {
|
public static ReturnT ERROR(String msg) {
|
||||||
return new ReturnT(FAIL_CODE, msg);
|
return new ReturnT(FAIL_CODE, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReturnT ERROR() {
|
public static ReturnT ERROR() {
|
||||||
return new ReturnT(FAIL_CODE, OBJECT_NOT_FOUND);
|
return new ReturnT(FAIL_CODE, OBJECT_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* GeneratorService
|
* GeneratorService
|
||||||
|
*
|
||||||
* @author zhengkai.blog.csdn.net
|
* @author zhengkai.blog.csdn.net
|
||||||
*/
|
*/
|
||||||
public interface GeneratorService {
|
public interface GeneratorService {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* GeneratorService
|
* GeneratorService
|
||||||
|
*
|
||||||
* @author zhengkai.blog.csdn.net
|
* @author zhengkai.blog.csdn.net
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -29,8 +30,10 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
private FreemarkerUtil freemarkerTool;
|
private FreemarkerUtil freemarkerTool;
|
||||||
|
|
||||||
String templateCpnfig = null;
|
String templateCpnfig = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从项目中的JSON文件读取String
|
* 从项目中的JSON文件读取String
|
||||||
|
*
|
||||||
* @author zhengkai.blog.csdn.net
|
* @author zhengkai.blog.csdn.net
|
||||||
*/
|
*/
|
||||||
public String getTemplateConfig() throws IOException {
|
public String getTemplateConfig() throws IOException {
|
||||||
@@ -45,8 +48,10 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
//log.info(JSON.toJSONString(templateCpnfig));
|
//log.info(JSON.toJSONString(templateCpnfig));
|
||||||
return templateCpnfig;
|
return templateCpnfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据配置的Template模板进行遍历解析,得到生成好的String
|
* 根据配置的Template模板进行遍历解析,得到生成好的String
|
||||||
|
*
|
||||||
* @author zhengkai.blog.csdn.net
|
* @author zhengkai.blog.csdn.net
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@@ -59,4 +64,5 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ package com.softdev.system.generator.util;
|
|||||||
* @author xuxueli 2018-05-02 21:10:28
|
* @author xuxueli 2018-05-02 21:10:28
|
||||||
*/
|
*/
|
||||||
public class CodeGenerateException extends RuntimeException {
|
public class CodeGenerateException extends RuntimeException {
|
||||||
|
|
||||||
private static final long serialVersionUID = 42L;
|
private static final long serialVersionUID = 42L;
|
||||||
|
|
||||||
public CodeGenerateException() {
|
public CodeGenerateException() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@@ -26,4 +28,5 @@ public class CodeGenerateException extends RuntimeException {
|
|||||||
boolean writableStackTrace) {
|
boolean writableStackTrace) {
|
||||||
super(message, cause, enableSuppression, writableStackTrace);
|
super(message, cause, enableSuppression, writableStackTrace);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,14 +23,13 @@ import java.util.Map;
|
|||||||
@Component
|
@Component
|
||||||
public class FreemarkerUtil {
|
public class FreemarkerUtil {
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Configuration configuration;
|
private Configuration configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 传入需要转义的字符串进行转义
|
* 传入需要转义的字符串进行转义
|
||||||
* 20200503 zhengkai.blog.csdn.net
|
* 20200503 zhengkai.blog.csdn.net
|
||||||
* */
|
*/
|
||||||
public static String escapeString(String originStr) {
|
public static String escapeString(String originStr) {
|
||||||
return originStr.replaceAll("井", "\\#").replaceAll("¥", "\\$");
|
return originStr.replaceAll("井", "\\#").replaceAll("¥", "\\$");
|
||||||
}
|
}
|
||||||
@@ -39,6 +38,7 @@ public class FreemarkerUtil {
|
|||||||
* freemarker config
|
* freemarker config
|
||||||
*/
|
*/
|
||||||
private static Configuration freemarkerConfig = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
|
private static Configuration freemarkerConfig = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
//2020-06-21 zhengkai 修复path问题导致jar无法运行而本地项目可以运行的bug
|
//2020-06-21 zhengkai 修复path问题导致jar无法运行而本地项目可以运行的bug
|
||||||
@@ -89,5 +89,4 @@ public class FreemarkerUtil {
|
|||||||
return htmlText;
|
return htmlText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,4 +58,5 @@ public class StringUtils {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,12 +20,14 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 表格解析Util
|
* 表格解析Util
|
||||||
|
*
|
||||||
* @author zhengkai.blog.csdn.net
|
* @author zhengkai.blog.csdn.net
|
||||||
*/
|
*/
|
||||||
public class TableParseUtil {
|
public class TableParseUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析DDL SQL生成类信息
|
* 解析DDL SQL生成类信息
|
||||||
|
*
|
||||||
* @param paramInfo
|
* @param paramInfo
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -157,7 +159,9 @@ public class TableParseUtil {
|
|||||||
&& !(columnLine.contains("primary ") && i > 3));
|
&& !(columnLine.contains("primary ") && i > 3));
|
||||||
if (specialFlag) {
|
if (specialFlag) {
|
||||||
//如果是oracle的number(x,x),可能出现最后分割残留的,x),这里做排除处理
|
//如果是oracle的number(x,x),可能出现最后分割残留的,x),这里做排除处理
|
||||||
if(columnLine.length()<5) {continue;}
|
if (columnLine.length() < 5) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
//2018-9-16 zhengkai 支持'符号以及空格的oracle语句// userid` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
|
//2018-9-16 zhengkai 支持'符号以及空格的oracle语句// userid` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
|
||||||
String columnName = "";
|
String columnName = "";
|
||||||
columnLine = columnLine.replaceAll("`", " ").replaceAll("\"", " ").replaceAll("'", "").replaceAll(" ", " ").trim();
|
columnLine = columnLine.replaceAll("`", " ").replaceAll("\"", " ").replaceAll("'", "").replaceAll(" ", " ").trim();
|
||||||
@@ -189,8 +193,7 @@ public class TableParseUtil {
|
|||||||
if (columnLine.contains(" tinyint")) {
|
if (columnLine.contains(" tinyint")) {
|
||||||
//20191115 MOSHOW.K.ZHENG 支持对tinyint的特殊处理
|
//20191115 MOSHOW.K.ZHENG 支持对tinyint的特殊处理
|
||||||
fieldClass = tinyintTransType;
|
fieldClass = tinyintTransType;
|
||||||
}
|
} else if (columnLine.contains(" int") || columnLine.contains(" smallint")) {
|
||||||
else if (columnLine.contains(" int") || columnLine.contains(" smallint")) {
|
|
||||||
fieldClass = Integer.class.getSimpleName();
|
fieldClass = Integer.class.getSimpleName();
|
||||||
} else if (columnLine.contains(" bigint")) {
|
} else if (columnLine.contains(" bigint")) {
|
||||||
fieldClass = Long.class.getSimpleName();
|
fieldClass = Long.class.getSimpleName();
|
||||||
@@ -298,8 +301,10 @@ public class TableParseUtil {
|
|||||||
|
|
||||||
return codeJavaInfo;
|
return codeJavaInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析JSON生成类信息
|
* 解析JSON生成类信息
|
||||||
|
*
|
||||||
* @param paramInfo
|
* @param paramInfo
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -327,11 +332,13 @@ public class TableParseUtil {
|
|||||||
|
|
||||||
return codeJavaInfo;
|
return codeJavaInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* parse SQL by regex
|
* parse SQL by regex
|
||||||
* @author https://github.com/ydq
|
*
|
||||||
* @param paramInfo
|
* @param paramInfo
|
||||||
* @return
|
* @return
|
||||||
|
* @author https://github.com/ydq
|
||||||
*/
|
*/
|
||||||
public static ClassInfo processTableToClassInfoByRegex(ParamInfo paramInfo) {
|
public static ClassInfo processTableToClassInfoByRegex(ParamInfo paramInfo) {
|
||||||
// field List
|
// field List
|
||||||
@@ -377,6 +384,7 @@ public class TableParseUtil {
|
|||||||
}
|
}
|
||||||
return codeJavaInfo;
|
return codeJavaInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<FieldInfo> processJsonObjectToFieldList(JSONObject jsonObject) {
|
public static List<FieldInfo> processJsonObjectToFieldList(JSONObject jsonObject) {
|
||||||
// field List
|
// field List
|
||||||
List<FieldInfo> fieldList = new ArrayList<FieldInfo>();
|
List<FieldInfo> fieldList = new ArrayList<FieldInfo>();
|
||||||
@@ -469,4 +477,5 @@ public class TableParseUtil {
|
|||||||
codeJavaInfo.setFieldList(fieldList);
|
codeJavaInfo.setFieldList(fieldList);
|
||||||
return codeJavaInfo;
|
return codeJavaInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import java.util.Map;
|
|||||||
* @date ${.now?string('yyyy-MM-dd')}
|
* @date ${.now?string('yyyy-MM-dd')}
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(value = "/${classInfo.className}")
|
@RequestMapping(value = "/${classInfo.className?uncap_first}")
|
||||||
public class ${classInfo.className}Controller {
|
public class ${classInfo.className}Controller {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
|||||||
Reference in New Issue
Block a user