diff --git a/docs/sqlsource.md b/docs/sqlsource.md index e9563e538..8c1eeb103 100644 --- a/docs/sqlsource.md +++ b/docs/sqlsource.md @@ -22,7 +22,7 @@ ```sql DELETE FROM user WHERE user_name = $${bean.userName} ``` -  当bean.userName=null时,执行sql会报错 ```Missing parameter bean.userName``` +  当bean=null或者bean.userName=null时,执行sql会报错 ```Missing parameter bean.userName```   Service调用原生SQL模板示例: ```java diff --git a/src/main/java/org/redkale/persistence/Entity.java b/src/main/java/org/redkale/persistence/Entity.java index 265a754c8..f568f5370 100644 --- a/src/main/java/org/redkale/persistence/Entity.java +++ b/src/main/java/org/redkale/persistence/Entity.java @@ -13,11 +13,10 @@ */ package org.redkale.persistence; +import java.lang.annotation.*; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; -import java.lang.annotation.*; - /** * Specifies that the class is an entity. This annotation is applied to the entity class. * @@ -30,12 +29,11 @@ import java.lang.annotation.*; public @interface Entity { /** - * (Optional) The entity name. Defaults to the unqualified name of the entity class. This name is used to refer to - * the entity in queries. The name must not be a reserved literal in the Java Persistence query language. + * (Optional) 表名和字段名是否将驼峰式改成下划线式 * - * @return String + * @return boolean */ - String name() default ""; + boolean camelCase() default false; /** * (Optional) The comment of the entity. diff --git a/src/main/java/org/redkale/source/EntityBuilder.java b/src/main/java/org/redkale/source/EntityBuilder.java index 2c75e5446..ed1534e07 100644 --- a/src/main/java/org/redkale/source/EntityBuilder.java +++ b/src/main/java/org/redkale/source/EntityBuilder.java @@ -294,7 +294,7 @@ public class EntityBuilder { }); } - // 带下划线的字段名替换成驼峰式 + // 下划线式字段名替换成驼峰式 protected String snakeCaseColumn(String sqlCol) { return snakeMap.computeIfAbsent(sqlCol, col -> { char ch; @@ -310,22 +310,9 @@ public class EntityBuilder { }); } - // 驼峰式字段名替换成带下划线的 + // 驼峰式字段名替换成下划线式 protected String camelCaseColumn(String column) { - return camelMap.computeIfAbsent(column, col -> { - char ch; - char[] chs = col.toCharArray(); - StringBuilder sb = new StringBuilder(chs.length + 3); - for (int i = 0; i < chs.length; i++) { - ch = chs[i]; - if (Character.isUpperCase(ch)) { - sb.append('_').append(Character.toLowerCase(ch)); - } else { - sb.append(ch); - } - } - return sb.toString(); - }); + return camelMap.computeIfAbsent(column, EntityColumn::camelCase); } protected T getObjectValue(List sqlColumns, final DataResultSetRow row) { diff --git a/src/main/java/org/redkale/source/EntityColumn.java b/src/main/java/org/redkale/source/EntityColumn.java index 7abaf01b6..3e8a51cfe 100644 --- a/src/main/java/org/redkale/source/EntityColumn.java +++ b/src/main/java/org/redkale/source/EntityColumn.java @@ -34,10 +34,11 @@ public class EntityColumn { private final int scale; - public EntityColumn(boolean primary, Column col, String name, Class type, Comment comment) { + public EntityColumn(boolean primary, boolean camelCase, Column col, String name, Class type, Comment comment) { this.primary = primary; this.field = name; - this.column = col == null || col.name().isEmpty() ? name : col.name(); + this.column = + col == null || col.name().isEmpty() ? (camelCase ? EntityColumn.camelCase(name) : name) : col.name(); this.type = type; this.comment = (col == null || col.comment().isEmpty()) && comment != null @@ -51,6 +52,26 @@ public class EntityColumn { this.scale = col == null ? 0 : col.scale(); } + /** + * 驼峰式字段名替换成下划线式 + * @param col 驼峰式字段名 + * @return 下划线式字段名 + */ + public static String camelCase(String col) { + char ch; + char[] chs = col.toCharArray(); + StringBuilder sb = new StringBuilder(chs.length + 3); + for (int i = 0; i < chs.length; i++) { + ch = chs[i]; + if (Character.isUpperCase(ch)) { + sb.append('_').append(Character.toLowerCase(ch)); + } else { + sb.append(ch); + } + } + return sb.toString(); + } + @Override public String toString() { return JsonConvert.root().convertTo(this); diff --git a/src/main/java/org/redkale/source/EntityInfo.java b/src/main/java/org/redkale/source/EntityInfo.java index b196f98ec..a7fc1072c 100644 --- a/src/main/java/org/redkale/source/EntityInfo.java +++ b/src/main/java/org/redkale/source/EntityInfo.java @@ -323,11 +323,17 @@ public final class EntityInfo { logmap.forEach((l, set) -> excludeLogLevels.put(l, set.toArray(new String[set.size()]))); } // --------------------------------------------- + org.redkale.persistence.Entity en = type.getAnnotation(org.redkale.persistence.Entity.class); + boolean camelCase = en != null && en.camelCase(); org.redkale.persistence.Table t1 = type.getAnnotation(org.redkale.persistence.Table.class); javax.persistence.Table t2 = type.getAnnotation(javax.persistence.Table.class); final String tableName0 = t1 != null ? t1.name() : (t2 != null ? t2.name() : null); final String tableCcatalog0 = t1 != null ? t1.catalog() : (t2 != null ? t2.catalog() : null); - String table0 = Utility.isEmpty(tableName0) ? type.getSimpleName().toLowerCase() : tableName0; + String table0 = Utility.isEmpty(tableName0) + ? (camelCase + ? EntityColumn.camelCase(type.getSimpleName()) + : type.getSimpleName().toLowerCase()) + : tableName0; if (Utility.isNotEmpty(tableCcatalog0)) { table0 = tableCcatalog0 + '.' + table0; } @@ -471,7 +477,8 @@ public final class EntityInfo { notNullColumns.add(fieldName); } } - ddl.add(new EntityColumn(idFlag, col, attr.field(), attr.type(), field.getAnnotation(Comment.class))); + ddl.add(new EntityColumn( + idFlag, camelCase, col, attr.field(), attr.type(), field.getAnnotation(Comment.class))); queryCols.add(sqlField); queryAttrs.add(attr); fields.add(fieldName); @@ -526,13 +533,15 @@ public final class EntityInfo { this.insertColumns = new EntityColumn[this.insertAttributes.length]; for (int i = 0; i < this.insertAttributes.length; i++) { String field = this.insertAttributes[i].field(); - this.insertColumns[i] = Utility.find(this.ddlColumns, c -> c.getField().equals(field)); + this.insertColumns[i] = + Utility.find(this.ddlColumns, c -> c.getField().equals(field)); } this.updateAttributes = updateAttrs.toArray(new Attribute[updateAttrs.size()]); this.updateColumns = new EntityColumn[this.updateAttributes.length]; for (int i = 0; i < this.updateAttributes.length; i++) { String field = this.updateAttributes[i].field(); - this.updateColumns[i] = Utility.find(this.ddlColumns, c -> c.getField().equals(field)); + this.updateColumns[i] = + Utility.find(this.ddlColumns, c -> c.getField().equals(field)); } this.updateEntityAttributes = Utility.append(this.updateAttributes, this.primary); this.updateEntityColumns = Utility.append(this.updateColumns, this.primaryColumn); @@ -545,8 +554,8 @@ public final class EntityInfo { } else { constructorAttributes = new Attribute[constructorParameters.length]; List> unconstructorAttrs = new ArrayList<>(); - List newquerycols1 = new ArrayList<>(); - List newquerycols2 = new ArrayList<>(); + List newQueryCols1 = new ArrayList<>(); + List newQueryCols2 = new ArrayList<>(); for (Attribute attr : new ArrayList<>(queryAttrs)) { int pos = -1; for (int i = 0; i < constructorParameters.length; i++) { @@ -557,15 +566,15 @@ public final class EntityInfo { } if (pos >= 0) { constructorAttributes[pos] = attr; - newquerycols1.add(queryCols.get(queryAttrs.indexOf(attr))); + newQueryCols1.add(queryCols.get(queryAttrs.indexOf(attr))); } else { unconstructorAttrs.add(attr); - newquerycols2.add(queryCols.get(queryAttrs.indexOf(attr))); + newQueryCols2.add(queryCols.get(queryAttrs.indexOf(attr))); } } unconstructorAttributes = unconstructorAttrs.toArray(new Attribute[unconstructorAttrs.size()]); - newquerycols1.addAll(newquerycols2); - queryCols = newquerycols1; + newQueryCols1.addAll(newQueryCols2); + queryCols = newQueryCols1; List> newqueryattrs = new ArrayList<>(); newqueryattrs.addAll(List.of(constructorAttributes)); newqueryattrs.addAll(unconstructorAttrs); @@ -578,7 +587,8 @@ public final class EntityInfo { this.queryColumns = new EntityColumn[this.queryAttributes.length]; for (int i = 0; i < this.queryAttributes.length; i++) { String field = this.queryAttributes[i].field(); - this.queryColumns[i] = Utility.find(this.ddlColumns, c -> c.getField().equals(field)); + this.queryColumns[i] = + Utility.find(this.ddlColumns, c -> c.getField().equals(field)); } this.builder = new EntityBuilder<>( type, @@ -735,7 +745,6 @@ public final class EntityInfo { boolean cacheable = false; int interval = 0; boolean direct = false; - org.redkale.persistence.Entity en = type.getAnnotation(org.redkale.persistence.Entity.class); if (en != null) { cacheable = en.cacheable(); interval = en.cacheInterval(); @@ -1774,5 +1783,4 @@ public final class EntityInfo { public String toString() { return getClass().getSimpleName() + "(" + type.getName() + ")@" + Objects.hashCode(this); } - }