Entity增加camelCase属性

This commit is contained in:
redkale
2024-06-27 21:24:37 +08:00
parent da17758614
commit 68af3c7495
5 changed files with 52 additions and 38 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -294,7 +294,7 @@ public class EntityBuilder<T> {
});
}
// 下划线字段名替换成驼峰式
// 下划线字段名替换成驼峰式
protected String snakeCaseColumn(String sqlCol) {
return snakeMap.computeIfAbsent(sqlCol, col -> {
char ch;
@@ -310,22 +310,9 @@ public class EntityBuilder<T> {
});
}
// 驼峰式字段名替换成下划线
// 驼峰式字段名替换成下划线
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<String> sqlColumns, final DataResultSetRow row) {

View File

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

View File

@@ -323,11 +323,17 @@ public final class EntityInfo<T> {
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<T> {
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<T> {
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<T> {
} else {
constructorAttributes = new Attribute[constructorParameters.length];
List<Attribute<T, Serializable>> unconstructorAttrs = new ArrayList<>();
List<String> newquerycols1 = new ArrayList<>();
List<String> newquerycols2 = new ArrayList<>();
List<String> newQueryCols1 = new ArrayList<>();
List<String> newQueryCols2 = new ArrayList<>();
for (Attribute<T, Serializable> attr : new ArrayList<>(queryAttrs)) {
int pos = -1;
for (int i = 0; i < constructorParameters.length; i++) {
@@ -557,15 +566,15 @@ public final class EntityInfo<T> {
}
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<Attribute<T, Serializable>> newqueryattrs = new ArrayList<>();
newqueryattrs.addAll(List.of(constructorAttributes));
newqueryattrs.addAll(unconstructorAttrs);
@@ -578,7 +587,8 @@ public final class EntityInfo<T> {
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<T> {
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<T> {
public String toString() {
return getClass().getSimpleName() + "(" + type.getName() + ")@" + Objects.hashCode(this);
}
}