perf:优化代码

This commit is contained in:
xiang
2026-01-30 22:50:35 +08:00
parent 3d50ec5985
commit 26474073e5
7 changed files with 37 additions and 110 deletions

View File

@@ -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);
}
}

View File

@@ -50,7 +50,6 @@ public class CodeGenServiceImpl implements CodeGenService {
// 3. generate the code by freemarker templates with parameters .
// Freemarker根据参数和模板生成代码
Map<String, String> result = getResultByParams(paramInfo.getOptions());
log.info("table:{} - time:{} ", MapUtil.getString(result, "tableName"), System.currentTimeMillis());
return Result.success(result);
} catch (Exception e) {
log.error("代码生成失败", e);

View File

@@ -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.util.MapUtil;
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 lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
@@ -219,7 +219,7 @@ public class SqlParserServiceImpl implements SqlParserService {
}
String originTableName = tableName;
//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"),"");
}
// class Name
@@ -361,8 +361,8 @@ public class SqlParserServiceImpl implements SqlParserService {
}
//swagger class
String swaggerClass = "string" ;
if(mysqlJavaTypeUtil.getMysqlSwaggerTypeMap().containsKey(mysqlType)){
swaggerClass = mysqlJavaTypeUtil.getMysqlSwaggerTypeMap().get(mysqlType);
if(Mysql2JavaTypeUtil.getMysqlSwaggerTypeMap().containsKey(mysqlType)){
swaggerClass = Mysql2JavaTypeUtil.getMysqlSwaggerTypeMap().get(mysqlType);
}
// field class
// int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
@@ -371,8 +371,8 @@ public class SqlParserServiceImpl implements SqlParserService {
//2018-11-22 lshz0088 处理字段类型的时候不严谨columnLine.contains(" int") 类似这种的,可在前后适当加一些空格之类的加以区分,否则当我的字段包含这些字符的时候,产生类型判断问题。
//2020-05-03 MOSHOW.K.ZHENG 优化对所有类型的处理
//2020-10-20 zhengkai 新增包装类型的转换选择
if(mysqlJavaTypeUtil.getMysqlJavaTypeMap().containsKey(mysqlType)){
fieldClass = mysqlJavaTypeUtil.getMysqlJavaTypeMap().get(mysqlType);
if(Mysql2JavaTypeUtil.getMysqlJavaTypeMap().containsKey(mysqlType)){
fieldClass = Mysql2JavaTypeUtil.getMysqlJavaTypeMap().get(mysqlType);
}
// field commentMySQL的一般位于field行而pgsql和oralce多位于后面。
String fieldComment = null;

View File

@@ -1,45 +1,19 @@
package com.xiang.xservice.application.generator.util;
import joptsimple.internal.Strings;
import java.util.Map;
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)) {
try {
return map.get(key).toString();
} catch (Exception e) {
e.printStackTrace();
return "";
return Strings.EMPTY;
}
} 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;
}
}

View File

@@ -2,7 +2,7 @@ package com.xiang.xservice.application.generator.util;
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> mysqlSwaggerTypeMap = new HashMap<String, String>();

View File

@@ -1,13 +1,15 @@
package com.xiang.xservice.application.generator.util;
import org.apache.commons.lang3.StringUtils;
public class StringUtilsPlus {
/**
* 首字母大写
*
* @param str
* @param str 字符串
*
* @return
* @return 首字母大写
*/
public static String upperCaseFirst(String str) {
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) {
//2019-2-10 解决StringUtils.lowerCaseFirst潜在的NPE异常@liutf
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) {
StringBuilder result = new StringBuilder();
if (underscoreName != null && underscoreName.trim().length() > 0) {
if (StringUtils.isNotBlank(underscoreName)) {
boolean flag = false;
for (int i = 0; i < underscoreName.length(); i++) {
char ch = underscoreName.charAt(i);
@@ -58,8 +59,6 @@ public class StringUtilsPlus {
/**
* 转 user_name 风格
* <p>
* 不管原始是什么风格
*/
public static String toUnderline(String str, boolean upperCase) {
if (str == null || str.trim().isEmpty()) {
@@ -91,7 +90,8 @@ public class StringUtilsPlus {
}
/**
* any str ==> lowerCamel
* 任何字符串 转驼峰
* @param str 任何字符串
*/
public static String toLowerCamel(String str) {
if (str == null || str.trim().isEmpty()) {
@@ -122,9 +122,4 @@ public class StringUtilsPlus {
return lowerCaseFirst(result.toString());
}
public static boolean isNotNull(String str) {
return org.apache.commons.lang3.StringUtils.isNotEmpty(str);
}
}

View File

@@ -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/");
}
}