| 2025.09.14 | 优化JSqlParser Engine(DDL Create SQL和Select SQL),适配更高级复杂的SQL

This commit is contained in:
Moshow郑锴
2025-09-14 15:07:09 +08:00
parent f11a656a74
commit b687f3666b
6 changed files with 52 additions and 40 deletions

View File

@@ -87,9 +87,14 @@ public class GeneratorServiceImpl implements GeneratorService {
@Override
public ClassInfo generateSelectSqlBySQLPraser(ParamInfo paramInfo) throws Exception {
ClassInfo classInfo = new ClassInfo();
Statement statement = CCJSqlParserUtil.parse(paramInfo.getTableSql());
String processedSql = paramInfo.getTableSql().trim()
.replaceAll("'", "`") // 将单引号替换为反引号
.replaceAll("\"", "`") // 将双引号替换为反引号
.replaceAll("", ","); // 将中文逗号替换为英文逗号
Statement statement = null;
CCJSqlParserManager parserManager = new CCJSqlParserManager();
statement = parserManager.parse(new StringReader(paramInfo.getTableSql()));
statement = parserManager.parse(new StringReader(processedSql));
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); // 创建表名发现者对象
List<String> tableNameList = tablesNamesFinder.getTableList(statement); // 获取到表名列表
//一般这里应该只解析到一个表名,除非多个表名,取第一个
@@ -97,7 +102,7 @@ public class GeneratorServiceImpl implements GeneratorService {
String tableName = tableNameList.get(0).trim();
classInfo.setTableName(tableName);
classInfo.setOriginTableName(tableName);
String className = StringUtilsPlus.upperCaseFirst(StringUtilsPlus.underlineToCamelCase(tableName));
String className = StringUtilsPlus.upperCaseFirst(StringUtilsPlus.underlineToCamelCase(tableName)).replaceAll("`", "");
if (className.contains("_")) {
className = className.replaceAll("_", "");
}
@@ -114,7 +119,7 @@ public class GeneratorServiceImpl implements GeneratorService {
List<FieldInfo> fieldList = new ArrayList<FieldInfo>();
selectItems.forEach(t->{
FieldInfo fieldInfo = new FieldInfo();
String fieldName = ((Column)t.getExpression()).getColumnName();
String fieldName = ((Column)t.getExpression()).getColumnName().replaceAll("`", "");
String aliasName = t.getAlias() != null ? t.getAlias().getName() : ((Column)t.getExpression()).getColumnName();
//存储原始字段名
fieldInfo.setFieldComment(aliasName);fieldInfo.setColumnName(aliasName);
@@ -153,8 +158,14 @@ public class GeneratorServiceImpl implements GeneratorService {
public ClassInfo generateCreateSqlBySQLPraser(ParamInfo paramInfo) throws Exception {
ClassInfo classInfo = new ClassInfo();
Statement statement = null;
// 对SQL进行预处理以提高解析成功率
String processedSql = paramInfo.getTableSql().trim()
.replaceAll("'", "`") // 将单引号替换为反引号
.replaceAll("\"", "`") // 将双引号替换为反引号
.replaceAll("", ","); // 将中文逗号替换为英文逗号
try {
statement = CCJSqlParserUtil.parse(paramInfo.getTableSql().trim());
statement = CCJSqlParserUtil.parse(processedSql);
}catch (Exception e) {
e.printStackTrace();
throw new SqlException("SQL语法错误:"+e.getMessage());
@@ -166,7 +177,7 @@ public class GeneratorServiceImpl implements GeneratorService {
}
// 提取表名
String tableName = createTable.getTable().getName();
String tableName = createTable.getTable().getName().replaceAll("`", "");
classInfo.setTableName(tableName);
String className = StringUtilsPlus.upperCaseFirst(StringUtilsPlus.underlineToCamelCase(tableName));
if (className.contains("_")) {
@@ -183,7 +194,7 @@ public class GeneratorServiceImpl implements GeneratorService {
if (columnDefinitions != null) {
for (ColumnDefinition columnDefinition : columnDefinitions) {
FieldInfo fieldInfo = new FieldInfo();
String columnName = columnDefinition.getColumnName();
String columnName = columnDefinition.getColumnName().replaceAll("`", "");
fieldInfo.setColumnName(columnName);
fieldInfo.setFieldComment(columnDefinition.toString());

View File

@@ -53,11 +53,11 @@ OEM:
header: SQL转Java JPA、MYBATIS实现类代码生成平台
keywords: sql转实体类,sql转DAO,SQL转service,SQL转JPA实现,SQL转MYBATIS实现
title: JAVA在线代码生成
slogan: Free your hands from tedious and repetitive CRUD work
description: <p>SpringBootCodeGenerator又名`大狼狗代码生成器`、`SQL转JAVA`、`SQL转JPA`、`SQL转Mybatis`、`Mybatis在线生成器`、`SQL转Java JPA、MYBATIS实现类代码生成平台`。</p><p>——从繁琐重复的`CRUD工作`中释放你的双手可通过DDL SQL语句或Select SQL语句或简单Json -> 生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL/CommonMapper等相关模板代码。</p>
slogan: 👐 Free your hands from boring CRUD—let the code write itself! ⚡
description: <p>💻 SpringBootCodeGenerator又名 `大狼狗代码生成器`🐺🐶,支持 SQL 一键转 JAVA/JPA/Mybatis/MybatisPlus 等多种模板,轻松搞定 CRUD彻底解放双手 ✋!只需提供 DDL、SELECT SQL 或简单 JSON 👉 即可生成 生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL/CommonMapper 等代码模板。</p><p>🔥🔥🔥 全新 JSqlParser 引擎上线,强力支持 DDL CREATE SQL与 SELECT SQL 解析!欢迎体验 & 反馈 💬!</p><p>👨‍💻 面向开发者的高效利器,已服务数万工程师,欢迎 Star ⭐、Fork 🍴、提 Issue 💬,一起打造更强大的代码生成平台!</p>
author: BEJSON.com
packageName: www.bejson.com
copyright: Powered by <a href="https://zhengkai.blog.csdn.net" target="_blank">Moshow</a> + <a href="https://www.bejson.com/">BeJSON</a> , Might the holy light be with you !
copyright: Powered by <a href="https://zhengkai.blog.csdn.net" target="_blank">Moshow郑锴</a><a href="https://www.bejson.com/">BeJSON</a> — may the holy light guide your code, your coffee, and your commits! ⚡🧙‍♂️💻
returnUtilSuccess: ResponseUtil.success
returnUtilFailure: ResponseUtil.error
outputStr: www.bejson.com

View File

@@ -50,12 +50,12 @@ OEM:
version: 2025 September
header: SQL转Java JPA、MYBATIS实现类代码生成平台
keywords: sql转实体类,sql转DAO,SQL转service,SQL转JPA实现,SQL转MYBATIS实现
title: JAVA代码生成平台
slogan: Free your hands from tedious and repetitive CRUD work
description: <p>SpringBootCodeGenerator又名`大狼狗代码生成器`、`SQL转JAVA`、`SQL转JPA`、`SQL转Mybatis`、`Mybatis在线生成器`、`SQL转Java JPA、MYBATIS实现类代码生成平台`。</p><p>——从繁琐重复的`CRUD工作`中释放你的双手可通过DDL SQL语句或Select SQL语句或简单Json -> 生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL/CommonMapper等相关模板代码。</p>
title: 大狼狗代码生成
slogan: 👐 Free your hands from boring CRUD—let the code write itself! ⚡
description: <p>💻 SpringBootCodeGenerator又名 `大狼狗代码生成器`🐺🐶,支持 SQL 一键转 JAVA/JPA/Mybatis/MybatisPlus 等多种模板,轻松搞定 CRUD彻底解放双手 ✋!只需提供 DDL、SELECT SQL 或简单 JSON 👉 即可生成 生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL/CommonMapper 等代码模板。</p><p>🔥🔥🔥 全新 JSqlParser 引擎上线,强力支持 DDL CREATE SQL与 SELECT SQL 解析!欢迎体验 & 反馈 💬!</p><p>👨‍💻 面向开发者的高效利器,已服务数万工程师,欢迎 Star ⭐、Fork 🍴、提 Issue 💬,一起打造更强大的代码生成平台!</p>
author: zhengkai.blog.csdn.net
packageName: com.software.system
copyright: Powered by <a href="https://zhengkai.blog.csdn.net" target="_blank">Moshow郑锴</a> , Might the holy light be with you !
copyright: Powered by <a href="https://zhengkai.blog.csdn.net" target="_blank">Moshow郑锴</a> — may the holy light guide your code, your coffee, and your commits! ⚡🧙‍♂️💻
returnUtilSuccess: ResponseUtil.success
returnUtilFailure: ResponseUtil.error
outputStr: http://zhengkai.blog.csdn.net

View File

@@ -53,11 +53,11 @@ OEM:
header: SQL转Java JPA、MYBATIS实现类代码生成平台
keywords: sql转实体类,sql转DAO,SQL转service,SQL转JPA实现,SQL转MYBATIS实现
title: JAVA在线代码生成
slogan: Free your hands from tedious and repetitive CRUD work
description: <p>SpringBootCodeGenerator又名`大狼狗代码生成器`、`SQL转JAVA`、`SQL转JPA`、`SQL转Mybatis`、`Mybatis在线生成器`、`SQL转Java JPA、MYBATIS实现类代码生成平台`。</p><p>——从繁琐重复的`CRUD工作`中释放你的双手可通过DDL SQL语句或Select SQL语句或简单Json -> 生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL/CommonMapper等相关模板代码。</p>
slogan: 👐 Free your hands from boring CRUD—let the code write itself! ⚡
description: <p>💻 SpringBootCodeGenerator又名 `大狼狗代码生成器`🐺🐶,支持 SQL 一键转 JAVA/JPA/Mybatis/MybatisPlus 等多种模板,轻松搞定 CRUD彻底解放双手 ✋!只需提供 DDL、SELECT SQL 或简单 JSON 👉 即可生成 生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL/CommonMapper 等代码模板。</p><p>🔥🔥🔥 全新 JSqlParser 引擎上线,强力支持 DDL CREATE SQL与 SELECT SQL 解析!欢迎体验 & 反馈 💬!</p><p>👨‍💻 面向开发者的高效利器,已服务数万工程师,欢迎 Star ⭐、Fork 🍴、提 Issue 💬,一起打造更强大的代码生成平台!</p>
author: https://www.json.cn/
packageName: www.json.cn
copyright: Powered by <a href="https://zhengkai.blog.csdn.net" target="_blank">Moshow</a> + <a href="https://www.json.cn/">JSON</a> , Might the holy light be with you !
copyright: Powered by <a href="https://zhengkai.blog.csdn.net" target="_blank">Moshow郑锴</a><a href="https://www.json.cn/">JSON</a> — may the holy light guide your code, your coffee, and your commits! ⚡🧙‍♂️💻
returnUtilSuccess: ResponseUtil.success
returnUtilFailure: ResponseUtil.error
outputStr: www.json.cn

View File

@@ -62,7 +62,7 @@
</div>
<small>${(value.slogan)!!}</small>
<div class="links">
View in <a href="https://github.com/moshowgame/SpringBootCodeGenerator/" target="_blank">GitHub</a> <a href="https://gitee.com/moshowgame/SpringBootCodeGenerator/" target="_blank">Gitee</a>
<a href="https://github.com/moshowgame/SpringBootCodeGenerator/" target="_blank">GitHub</a> <a href="https://zhengkai.blog.csdn.net/" target="_blank">CSDN</a>
</div>
</div>
<div class="container">