From 5bc8c1d41ed1bb77fa29b56971a6fde84fc62437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=81=E5=9F=B9=E5=9F=B9?= Date: Tue, 13 Aug 2024 20:10:55 +0800 Subject: [PATCH] set Generated's table default value to empty string #4 --- .../ningpp/mmegp/annotations/Generated.java | 2 +- .../ningpp/mmegp/demo/entity/SysAutoUser.java | 2 +- .../DefaultIntrospectedTableBuilder.java | 2 +- .../java/me/ningpp/mmegp/JavaParserUtil.java | 36 ++++++++++++------- .../me/ningpp/mmegp/entity/BigEntity.java | 2 +- .../ningpp/mmegp/entity/BigRecordEntity.java | 2 +- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/mmegp-annotations/src/main/java/me/ningpp/mmegp/annotations/Generated.java b/mmegp-annotations/src/main/java/me/ningpp/mmegp/annotations/Generated.java index bb54861..68ae33e 100644 --- a/mmegp-annotations/src/main/java/me/ningpp/mmegp/annotations/Generated.java +++ b/mmegp-annotations/src/main/java/me/ningpp/mmegp/annotations/Generated.java @@ -31,7 +31,7 @@ /** * table name */ - String table(); + String table() default ""; /** * seperate multi columns by seperator=, diff --git a/mmegp-maven-plugin-demo/mmegp-maven-plugin-demo-dynamic-sql/src/main/java/me/ningpp/mmegp/demo/entity/SysAutoUser.java b/mmegp-maven-plugin-demo/mmegp-maven-plugin-demo-dynamic-sql/src/main/java/me/ningpp/mmegp/demo/entity/SysAutoUser.java index aa6008a..b44bc18 100644 --- a/mmegp-maven-plugin-demo/mmegp-maven-plugin-demo-dynamic-sql/src/main/java/me/ningpp/mmegp/demo/entity/SysAutoUser.java +++ b/mmegp-maven-plugin-demo/mmegp-maven-plugin-demo-dynamic-sql/src/main/java/me/ningpp/mmegp/demo/entity/SysAutoUser.java @@ -4,7 +4,7 @@ import me.ningpp.mmegp.annotations.GeneratedColumn; import me.ningpp.mmegp.annotations.SoftDelete; -@Generated(table = "sys_auto_user") +@Generated @SoftDelete(column = "deleted") public class SysAutoUser { @GeneratedColumn(id = true, generatedValue = true) diff --git a/mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/DefaultIntrospectedTableBuilder.java b/mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/DefaultIntrospectedTableBuilder.java index bc34aa5..2a1c112 100644 --- a/mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/DefaultIntrospectedTableBuilder.java +++ b/mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/DefaultIntrospectedTableBuilder.java @@ -95,7 +95,7 @@ private static IntrospectedTable buildIntrospectedTable(Context context, TypeDeclaration modelDeclaration, NodeList importDeclarations, MetaInfoHandler metaInfoHandler) { - GeneratedTableInfo tableInfo = JavaParserUtil.getTableValue(modelDeclaration); + GeneratedTableInfo tableInfo = JavaParserUtil.getTableValue(modelDeclaration, context); if (tableInfo == null || StringUtils.isEmpty(tableInfo.getName())) { return null; } diff --git a/mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/JavaParserUtil.java b/mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/JavaParserUtil.java index 3181e52..897bfb2 100644 --- a/mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/JavaParserUtil.java +++ b/mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/JavaParserUtil.java @@ -123,20 +123,29 @@ public static JavaParser newParser() { return new JavaParser(jpc); } - public static GeneratedTableInfo getTableValue(TypeDeclaration annotationNode) { - if (annotationNode == null || annotationNode.getFullyQualifiedName().isEmpty()) { - return null; - } - var annotationMembers = getNormalAnnotationMembers(annotationNode, Generated.class); - if (annotationMembers.isEmpty()) { + public static GeneratedTableInfo getTableValue(TypeDeclaration typeDeclar, Context context) { + if (typeDeclar == null || typeDeclar.getFullyQualifiedName().isEmpty()) { return null; } + var annotationMembers = getNormalAnnotationMembers(typeDeclar, Generated.class); - String tableName = parseString(annotationMembers, "table", null); + String tableName = parseTableName(annotationMembers, typeDeclar.getNameAsString(), context); List countGroupByColumns = parseArrayString(annotationMembers, COUNT_GROUP_BY_COLUMNS_NAME); return new GeneratedTableInfo(tableName, countGroupByColumns); } + private static String parseTableName(Map> annotationMembers, + String entityName, Context context) { + return parseNamingByStrategy(annotationMembers, "table", entityName, + "tableNamingStrategyInstance", "tableNamingStrategy", context); + } + + private static String parseColumnName(Map> annotationMembers, + String javaProperty, Context context) { + return parseNamingByStrategy(annotationMembers, "name", javaProperty, + "columnNamingStrategyInstance", "columnNamingStrategy", context); + } + public static Pair buildColumn(TypeDeclaration modelDeclaration, Map declarMappings, Parameter param, @@ -223,19 +232,20 @@ private static Pair> annotationMembers, - String javaProperty, Context context) { - String name = parseString(annotationMembers, "name", ""); + private static String parseNamingByStrategy(Map> annotationMembers, + String annotationMethod, String srcName, + String instanceKey, String propertyKey, + Context context) { + String name = parseString(annotationMembers, annotationMethod, ""); if (StringUtils.isEmpty(name)) { - String instanceKey = "columnNamingStrategyInstance"; NamingStrategy strategy = (NamingStrategy) context.getProperties().get(instanceKey); if (strategy == null) { String strategyClassName = (String) context.getProperties() - .getOrDefault("columnNamingStrategy", SnakeCaseStrategy.class.getName()); + .getOrDefault(propertyKey, SnakeCaseStrategy.class.getName()); strategy = (NamingStrategy) ObjectFactory.createExternalObject(strategyClassName); context.getProperties().put(instanceKey, strategy); } - name = strategy.translate(javaProperty); + name = strategy.translate(srcName); } return name; } diff --git a/mmegp-mbg-ext/src/test/java/me/ningpp/mmegp/entity/BigEntity.java b/mmegp-mbg-ext/src/test/java/me/ningpp/mmegp/entity/BigEntity.java index 951cfde..78c0daf 100644 --- a/mmegp-mbg-ext/src/test/java/me/ningpp/mmegp/entity/BigEntity.java +++ b/mmegp-mbg-ext/src/test/java/me/ningpp/mmegp/entity/BigEntity.java @@ -33,7 +33,7 @@ import static org.apache.ibatis.type.JdbcType.VARCHAR; -@Generated(table = "big_entity") +@Generated public class BigEntity { @GeneratedColumn(id = true) private String id; diff --git a/mmegp-mbg-ext/src/test/java/me/ningpp/mmegp/entity/BigRecordEntity.java b/mmegp-mbg-ext/src/test/java/me/ningpp/mmegp/entity/BigRecordEntity.java index 7353392..217e4e9 100644 --- a/mmegp-mbg-ext/src/test/java/me/ningpp/mmegp/entity/BigRecordEntity.java +++ b/mmegp-mbg-ext/src/test/java/me/ningpp/mmegp/entity/BigRecordEntity.java @@ -21,7 +21,7 @@ import static org.apache.ibatis.type.JdbcType.VARCHAR; -@Generated(table = "big_record_entity") +@Generated public record BigRecordEntity( @GeneratedColumn(name = "id", jdbcType = VARCHAR, id = true) String id,