perf:优化代码
This commit is contained in:
@@ -1,55 +0,0 @@
|
|||||||
package com.xiang.xservice.application.generator.config;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSONReader;
|
|
||||||
import com.alibaba.fastjson2.JSONWriter;
|
|
||||||
import com.alibaba.fastjson2.support.config.FastJsonConfig;
|
|
||||||
import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
/**
|
|
||||||
* 2019-2-11 liutf WebMvcConfig 整合 cors 和 SpringMvc MessageConverter
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class WebMvcConfig implements WebMvcConfigurer {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
||||||
registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* FASTJSON2升级 by https://zhengkai.blog.csdn.net/
|
|
||||||
* https://blog.csdn.net/moshowgame/article/details/138013669
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
||||||
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
|
|
||||||
//自定义配置...
|
|
||||||
FastJsonConfig config = new FastJsonConfig();
|
|
||||||
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
||||||
|
|
||||||
// 添加更多解析特性以提高容错性
|
|
||||||
config.setReaderFeatures(
|
|
||||||
JSONReader.Feature.FieldBased,
|
|
||||||
JSONReader.Feature.SupportArrayToBean,
|
|
||||||
JSONReader.Feature.InitStringFieldAsEmpty
|
|
||||||
);
|
|
||||||
|
|
||||||
config.setWriterFeatures(
|
|
||||||
JSONWriter.Feature.WriteMapNullValue,
|
|
||||||
JSONWriter.Feature.PrettyFormat
|
|
||||||
);
|
|
||||||
|
|
||||||
converter.setFastJsonConfig(config);
|
|
||||||
converter.setDefaultCharset(StandardCharsets.UTF_8);
|
|
||||||
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
|
|
||||||
converters.add(0, converter);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -50,7 +50,6 @@ public class CodeGenServiceImpl implements CodeGenService {
|
|||||||
// 3. generate the code by freemarker templates with parameters .
|
// 3. generate the code by freemarker templates with parameters .
|
||||||
// Freemarker根据参数和模板生成代码
|
// Freemarker根据参数和模板生成代码
|
||||||
Map<String, String> result = getResultByParams(paramInfo.getOptions());
|
Map<String, String> result = getResultByParams(paramInfo.getOptions());
|
||||||
log.info("table:{} - time:{} ", MapUtil.getString(result, "tableName"), System.currentTimeMillis());
|
|
||||||
return Result.success(result);
|
return Result.success(result);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("代码生成失败", e);
|
log.error("代码生成失败", e);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import com.xiang.xservice.application.generator.entity.dto.ParamInfo;
|
|||||||
import com.xiang.xservice.application.generator.service.parser.SqlParserService;
|
import com.xiang.xservice.application.generator.service.parser.SqlParserService;
|
||||||
import com.xiang.xservice.application.generator.util.MapUtil;
|
import com.xiang.xservice.application.generator.util.MapUtil;
|
||||||
import com.xiang.xservice.application.generator.util.StringUtilsPlus;
|
import com.xiang.xservice.application.generator.util.StringUtilsPlus;
|
||||||
import com.xiang.xservice.application.generator.util.mysqlJavaTypeUtil;
|
import com.xiang.xservice.application.generator.util.Mysql2JavaTypeUtil;
|
||||||
import com.xiang.xservice.basic.exception.BusinessException;
|
import com.xiang.xservice.basic.exception.BusinessException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.sf.jsqlparser.parser.CCJSqlParserManager;
|
import net.sf.jsqlparser.parser.CCJSqlParserManager;
|
||||||
@@ -219,7 +219,7 @@ public class SqlParserServiceImpl implements SqlParserService {
|
|||||||
}
|
}
|
||||||
String originTableName = tableName;
|
String originTableName = tableName;
|
||||||
//ignore prefix
|
//ignore prefix
|
||||||
if(tableName!=null && StringUtilsPlus.isNotNull(MapUtil.getString(paramInfo.getOptions(),"ignorePrefix"))){
|
if(tableName!=null && StringUtils.isNotBlank(MapUtil.getString(paramInfo.getOptions(),"ignorePrefix"))){
|
||||||
tableName = tableName.replaceAll(MapUtil.getString(paramInfo.getOptions(),"ignorePrefix"),"");
|
tableName = tableName.replaceAll(MapUtil.getString(paramInfo.getOptions(),"ignorePrefix"),"");
|
||||||
}
|
}
|
||||||
// class Name
|
// class Name
|
||||||
@@ -361,8 +361,8 @@ public class SqlParserServiceImpl implements SqlParserService {
|
|||||||
}
|
}
|
||||||
//swagger class
|
//swagger class
|
||||||
String swaggerClass = "string" ;
|
String swaggerClass = "string" ;
|
||||||
if(mysqlJavaTypeUtil.getMysqlSwaggerTypeMap().containsKey(mysqlType)){
|
if(Mysql2JavaTypeUtil.getMysqlSwaggerTypeMap().containsKey(mysqlType)){
|
||||||
swaggerClass = mysqlJavaTypeUtil.getMysqlSwaggerTypeMap().get(mysqlType);
|
swaggerClass = Mysql2JavaTypeUtil.getMysqlSwaggerTypeMap().get(mysqlType);
|
||||||
}
|
}
|
||||||
// field class
|
// field class
|
||||||
// int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
|
// int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
|
||||||
@@ -371,8 +371,8 @@ public class SqlParserServiceImpl implements SqlParserService {
|
|||||||
//2018-11-22 lshz0088 处理字段类型的时候,不严谨columnLine.contains(" int") 类似这种的,可在前后适当加一些空格之类的加以区分,否则当我的字段包含这些字符的时候,产生类型判断问题。
|
//2018-11-22 lshz0088 处理字段类型的时候,不严谨columnLine.contains(" int") 类似这种的,可在前后适当加一些空格之类的加以区分,否则当我的字段包含这些字符的时候,产生类型判断问题。
|
||||||
//2020-05-03 MOSHOW.K.ZHENG 优化对所有类型的处理
|
//2020-05-03 MOSHOW.K.ZHENG 优化对所有类型的处理
|
||||||
//2020-10-20 zhengkai 新增包装类型的转换选择
|
//2020-10-20 zhengkai 新增包装类型的转换选择
|
||||||
if(mysqlJavaTypeUtil.getMysqlJavaTypeMap().containsKey(mysqlType)){
|
if(Mysql2JavaTypeUtil.getMysqlJavaTypeMap().containsKey(mysqlType)){
|
||||||
fieldClass = mysqlJavaTypeUtil.getMysqlJavaTypeMap().get(mysqlType);
|
fieldClass = Mysql2JavaTypeUtil.getMysqlJavaTypeMap().get(mysqlType);
|
||||||
}
|
}
|
||||||
// field comment,MySQL的一般位于field行,而pgsql和oralce多位于后面。
|
// field comment,MySQL的一般位于field行,而pgsql和oralce多位于后面。
|
||||||
String fieldComment = null;
|
String fieldComment = null;
|
||||||
|
|||||||
@@ -1,45 +1,19 @@
|
|||||||
|
|
||||||
package com.xiang.xservice.application.generator.util;
|
package com.xiang.xservice.application.generator.util;
|
||||||
|
|
||||||
|
import joptsimple.internal.Strings;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class MapUtil {
|
public class MapUtil {
|
||||||
public static String getString(Map map, String key) {
|
public static String getString(Map<String, Object> map, String key) {
|
||||||
if (map != null && map.containsKey(key)) {
|
if (map != null && map.containsKey(key)) {
|
||||||
try {
|
try {
|
||||||
return map.get(key).toString();
|
return map.get(key).toString();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
return Strings.EMPTY;
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Integer getInteger(Map map, String key) {
|
|
||||||
if (map != null && map.containsKey(key)) {
|
|
||||||
try {
|
|
||||||
return Integer.valueOf(map.get(key).toString());
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Boolean getBoolean(Map map, String key) {
|
|
||||||
if (map != null && map.containsKey(key)) {
|
|
||||||
try {
|
|
||||||
return Boolean.parseBoolean(map.get(key).toString()) || "true".equals(map.get(key).toString());
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return Strings.EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.xiang.xservice.application.generator.util;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public final class mysqlJavaTypeUtil {
|
public final class Mysql2JavaTypeUtil {
|
||||||
public static final HashMap<String, String> mysqlJavaTypeMap = new HashMap<String, String>();
|
public static final HashMap<String, String> mysqlJavaTypeMap = new HashMap<String, String>();
|
||||||
public static final HashMap<String, String> mysqlSwaggerTypeMap = new HashMap<String, String>();
|
public static final HashMap<String, String> mysqlSwaggerTypeMap = new HashMap<String, String>();
|
||||||
|
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package com.xiang.xservice.application.generator.util;
|
package com.xiang.xservice.application.generator.util;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
public class StringUtilsPlus {
|
public class StringUtilsPlus {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 首字母大写
|
* 首字母大写
|
||||||
*
|
*
|
||||||
* @param str
|
* @param str 字符串
|
||||||
*
|
*
|
||||||
* @return
|
* @return 首字母大写
|
||||||
*/
|
*/
|
||||||
public static String upperCaseFirst(String str) {
|
public static String upperCaseFirst(String str) {
|
||||||
if (str == null || str.trim().isEmpty()) {
|
if (str == null || str.trim().isEmpty()) {
|
||||||
@@ -19,25 +21,24 @@ public class StringUtilsPlus {
|
|||||||
/**
|
/**
|
||||||
* 首字母小写
|
* 首字母小写
|
||||||
*
|
*
|
||||||
* @param str
|
* @param str 字符串
|
||||||
*
|
*
|
||||||
* @return
|
* @return 首字母小写
|
||||||
*/
|
*/
|
||||||
public static String lowerCaseFirst(String str) {
|
public static String lowerCaseFirst(String str) {
|
||||||
//2019-2-10 解决StringUtils.lowerCaseFirst潜在的NPE异常@liutf
|
|
||||||
return (str != null && str.length() > 1) ? str.substring(0, 1).toLowerCase() + str.substring(1) : "";
|
return (str != null && str.length() > 1) ? str.substring(0, 1).toLowerCase() + str.substring(1) : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下划线,转换为驼峰式
|
* 下划线,转换为驼峰式
|
||||||
*
|
*
|
||||||
* @param underscoreName
|
* @param underscoreName 下划线字符串
|
||||||
*
|
*
|
||||||
* @return
|
* @return 驼峰式
|
||||||
*/
|
*/
|
||||||
public static String underlineToCamelCase(String underscoreName) {
|
public static String underlineToCamelCase(String underscoreName) {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
if (underscoreName != null && underscoreName.trim().length() > 0) {
|
if (StringUtils.isNotBlank(underscoreName)) {
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
for (int i = 0; i < underscoreName.length(); i++) {
|
for (int i = 0; i < underscoreName.length(); i++) {
|
||||||
char ch = underscoreName.charAt(i);
|
char ch = underscoreName.charAt(i);
|
||||||
@@ -58,8 +59,6 @@ public class StringUtilsPlus {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 转 user_name 风格
|
* 转 user_name 风格
|
||||||
* <p>
|
|
||||||
* 不管原始是什么风格
|
|
||||||
*/
|
*/
|
||||||
public static String toUnderline(String str, boolean upperCase) {
|
public static String toUnderline(String str, boolean upperCase) {
|
||||||
if (str == null || str.trim().isEmpty()) {
|
if (str == null || str.trim().isEmpty()) {
|
||||||
@@ -91,7 +90,8 @@ public class StringUtilsPlus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* any str ==> lowerCamel
|
* 任何字符串 转驼峰
|
||||||
|
* @param str 任何字符串
|
||||||
*/
|
*/
|
||||||
public static String toLowerCamel(String str) {
|
public static String toLowerCamel(String str) {
|
||||||
if (str == null || str.trim().isEmpty()) {
|
if (str == null || str.trim().isEmpty()) {
|
||||||
@@ -122,9 +122,4 @@ public class StringUtilsPlus {
|
|||||||
|
|
||||||
return lowerCaseFirst(result.toString());
|
return lowerCaseFirst(result.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNotNull(String str) {
|
|
||||||
return org.apache.commons.lang3.StringUtils.isNotEmpty(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.xiang.xservice.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebMvcConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
|
registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user