Entity增加camelCase属性
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
```sql
|
```sql
|
||||||
DELETE FROM user WHERE user_name = $${bean.userName}
|
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模板示例:
|
  Service调用原生SQL模板示例:
|
||||||
```java
|
```java
|
||||||
|
|||||||
@@ -13,11 +13,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.persistence;
|
package org.redkale.persistence;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
import static java.lang.annotation.ElementType.TYPE;
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
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.
|
* 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 {
|
public @interface Entity {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (Optional) The entity name. Defaults to the unqualified name of the entity class. This name is used to refer to
|
* (Optional) 表名和字段名是否将驼峰式改成下划线式
|
||||||
* the entity in queries. The name must not be a reserved literal in the Java Persistence query language.
|
|
||||||
*
|
*
|
||||||
* @return String
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
String name() default "";
|
boolean camelCase() default false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (Optional) The comment of the entity.
|
* (Optional) The comment of the entity.
|
||||||
|
|||||||
@@ -294,7 +294,7 @@ public class EntityBuilder<T> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 带下划线的字段名替换成驼峰式
|
// 下划线式字段名替换成驼峰式
|
||||||
protected String snakeCaseColumn(String sqlCol) {
|
protected String snakeCaseColumn(String sqlCol) {
|
||||||
return snakeMap.computeIfAbsent(sqlCol, col -> {
|
return snakeMap.computeIfAbsent(sqlCol, col -> {
|
||||||
char ch;
|
char ch;
|
||||||
@@ -310,22 +310,9 @@ public class EntityBuilder<T> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 驼峰式字段名替换成带下划线的
|
// 驼峰式字段名替换成下划线式
|
||||||
protected String camelCaseColumn(String column) {
|
protected String camelCaseColumn(String column) {
|
||||||
return camelMap.computeIfAbsent(column, col -> {
|
return camelMap.computeIfAbsent(column, EntityColumn::camelCase);
|
||||||
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();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected T getObjectValue(List<String> sqlColumns, final DataResultSetRow row) {
|
protected T getObjectValue(List<String> sqlColumns, final DataResultSetRow row) {
|
||||||
|
|||||||
@@ -34,10 +34,11 @@ public class EntityColumn {
|
|||||||
|
|
||||||
private final int scale;
|
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.primary = primary;
|
||||||
this.field = name;
|
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.type = type;
|
||||||
this.comment = (col == null || col.comment().isEmpty())
|
this.comment = (col == null || col.comment().isEmpty())
|
||||||
&& comment != null
|
&& comment != null
|
||||||
@@ -51,6 +52,26 @@ public class EntityColumn {
|
|||||||
this.scale = col == null ? 0 : col.scale();
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return JsonConvert.root().convertTo(this);
|
return JsonConvert.root().convertTo(this);
|
||||||
|
|||||||
@@ -323,11 +323,17 @@ public final class EntityInfo<T> {
|
|||||||
logmap.forEach((l, set) -> excludeLogLevels.put(l, set.toArray(new String[set.size()])));
|
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);
|
org.redkale.persistence.Table t1 = type.getAnnotation(org.redkale.persistence.Table.class);
|
||||||
javax.persistence.Table t2 = type.getAnnotation(javax.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 tableName0 = t1 != null ? t1.name() : (t2 != null ? t2.name() : null);
|
||||||
final String tableCcatalog0 = t1 != null ? t1.catalog() : (t2 != null ? t2.catalog() : 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)) {
|
if (Utility.isNotEmpty(tableCcatalog0)) {
|
||||||
table0 = tableCcatalog0 + '.' + table0;
|
table0 = tableCcatalog0 + '.' + table0;
|
||||||
}
|
}
|
||||||
@@ -471,7 +477,8 @@ public final class EntityInfo<T> {
|
|||||||
notNullColumns.add(fieldName);
|
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);
|
queryCols.add(sqlField);
|
||||||
queryAttrs.add(attr);
|
queryAttrs.add(attr);
|
||||||
fields.add(fieldName);
|
fields.add(fieldName);
|
||||||
@@ -526,13 +533,15 @@ public final class EntityInfo<T> {
|
|||||||
this.insertColumns = new EntityColumn[this.insertAttributes.length];
|
this.insertColumns = new EntityColumn[this.insertAttributes.length];
|
||||||
for (int i = 0; i < this.insertAttributes.length; i++) {
|
for (int i = 0; i < this.insertAttributes.length; i++) {
|
||||||
String field = this.insertAttributes[i].field();
|
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.updateAttributes = updateAttrs.toArray(new Attribute[updateAttrs.size()]);
|
||||||
this.updateColumns = new EntityColumn[this.updateAttributes.length];
|
this.updateColumns = new EntityColumn[this.updateAttributes.length];
|
||||||
for (int i = 0; i < this.updateAttributes.length; i++) {
|
for (int i = 0; i < this.updateAttributes.length; i++) {
|
||||||
String field = this.updateAttributes[i].field();
|
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.updateEntityAttributes = Utility.append(this.updateAttributes, this.primary);
|
||||||
this.updateEntityColumns = Utility.append(this.updateColumns, this.primaryColumn);
|
this.updateEntityColumns = Utility.append(this.updateColumns, this.primaryColumn);
|
||||||
@@ -545,8 +554,8 @@ public final class EntityInfo<T> {
|
|||||||
} else {
|
} else {
|
||||||
constructorAttributes = new Attribute[constructorParameters.length];
|
constructorAttributes = new Attribute[constructorParameters.length];
|
||||||
List<Attribute<T, Serializable>> unconstructorAttrs = new ArrayList<>();
|
List<Attribute<T, Serializable>> unconstructorAttrs = new ArrayList<>();
|
||||||
List<String> newquerycols1 = new ArrayList<>();
|
List<String> newQueryCols1 = new ArrayList<>();
|
||||||
List<String> newquerycols2 = new ArrayList<>();
|
List<String> newQueryCols2 = new ArrayList<>();
|
||||||
for (Attribute<T, Serializable> attr : new ArrayList<>(queryAttrs)) {
|
for (Attribute<T, Serializable> attr : new ArrayList<>(queryAttrs)) {
|
||||||
int pos = -1;
|
int pos = -1;
|
||||||
for (int i = 0; i < constructorParameters.length; i++) {
|
for (int i = 0; i < constructorParameters.length; i++) {
|
||||||
@@ -557,15 +566,15 @@ public final class EntityInfo<T> {
|
|||||||
}
|
}
|
||||||
if (pos >= 0) {
|
if (pos >= 0) {
|
||||||
constructorAttributes[pos] = attr;
|
constructorAttributes[pos] = attr;
|
||||||
newquerycols1.add(queryCols.get(queryAttrs.indexOf(attr)));
|
newQueryCols1.add(queryCols.get(queryAttrs.indexOf(attr)));
|
||||||
} else {
|
} else {
|
||||||
unconstructorAttrs.add(attr);
|
unconstructorAttrs.add(attr);
|
||||||
newquerycols2.add(queryCols.get(queryAttrs.indexOf(attr)));
|
newQueryCols2.add(queryCols.get(queryAttrs.indexOf(attr)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unconstructorAttributes = unconstructorAttrs.toArray(new Attribute[unconstructorAttrs.size()]);
|
unconstructorAttributes = unconstructorAttrs.toArray(new Attribute[unconstructorAttrs.size()]);
|
||||||
newquerycols1.addAll(newquerycols2);
|
newQueryCols1.addAll(newQueryCols2);
|
||||||
queryCols = newquerycols1;
|
queryCols = newQueryCols1;
|
||||||
List<Attribute<T, Serializable>> newqueryattrs = new ArrayList<>();
|
List<Attribute<T, Serializable>> newqueryattrs = new ArrayList<>();
|
||||||
newqueryattrs.addAll(List.of(constructorAttributes));
|
newqueryattrs.addAll(List.of(constructorAttributes));
|
||||||
newqueryattrs.addAll(unconstructorAttrs);
|
newqueryattrs.addAll(unconstructorAttrs);
|
||||||
@@ -578,7 +587,8 @@ public final class EntityInfo<T> {
|
|||||||
this.queryColumns = new EntityColumn[this.queryAttributes.length];
|
this.queryColumns = new EntityColumn[this.queryAttributes.length];
|
||||||
for (int i = 0; i < this.queryAttributes.length; i++) {
|
for (int i = 0; i < this.queryAttributes.length; i++) {
|
||||||
String field = this.queryAttributes[i].field();
|
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<>(
|
this.builder = new EntityBuilder<>(
|
||||||
type,
|
type,
|
||||||
@@ -735,7 +745,6 @@ public final class EntityInfo<T> {
|
|||||||
boolean cacheable = false;
|
boolean cacheable = false;
|
||||||
int interval = 0;
|
int interval = 0;
|
||||||
boolean direct = false;
|
boolean direct = false;
|
||||||
org.redkale.persistence.Entity en = type.getAnnotation(org.redkale.persistence.Entity.class);
|
|
||||||
if (en != null) {
|
if (en != null) {
|
||||||
cacheable = en.cacheable();
|
cacheable = en.cacheable();
|
||||||
interval = en.cacheInterval();
|
interval = en.cacheInterval();
|
||||||
@@ -1774,5 +1783,4 @@ public final class EntityInfo<T> {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return getClass().getSimpleName() + "(" + type.getName() + ")@" + Objects.hashCode(this);
|
return getClass().getSimpleName() + "(" + type.getName() + ")@" + Objects.hashCode(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user