fix SQLProvider example

This commit is contained in:
Timi
2025-12-25 18:09:31 +08:00
parent e0398b3a22
commit 1508bf7c7f

View File

@ -64,20 +64,24 @@ public class SQLProvider {
Object obj = page.getEqualsExample();
EntityMeta metaExample = getEntityMeta(obj.getClass());
String conditionClause = metaExample.fieldColumnList.stream()
.filter(fc -> fc.isNotNull(obj))
.filter(fc -> fc.isNotEmpty(obj))
.map(fc -> "`%s` = '%s'".formatted(fc.columnName, fc.getAsString(obj)))
.collect(Collectors.joining(" AND "));
sql.append(" AND ").append(conditionClause);
if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND ").append(conditionClause);
}
}
if (TimiJava.isNotEmpty(page.getLikesExample())) {
// 模糊查询
Object obj = page.getLikesExample();
EntityMeta metaExample = getEntityMeta(obj.getClass());
String conditionClause = metaExample.fieldColumnList.stream()
.filter(fc -> fc.isNotNull(obj))
.filter(fc -> fc.isNotEmpty(obj))
.map(fc -> "`%s` LIKE CONCAT('%%', '%s', '%%')".formatted(fc.columnName, fc.getAsString(obj)))
.collect(Collectors.joining(" OR "));
sql.append(" AND (").append(conditionClause).append(')');
if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND (").append(conditionClause).append(')');
}
}
// 排序
if (TimiJava.isNotEmpty(page.getOrderMap())) {
@ -122,20 +126,24 @@ public class SQLProvider {
Object obj = page.getEqualsExample();
EntityMeta metaExample = getEntityMeta(obj.getClass());
String conditionClause = metaExample.fieldColumnList.stream()
.filter(fc -> fc.isNotNull(obj))
.filter(fc -> fc.isNotEmpty(obj))
.map(fc -> "`%s` = '%s'".formatted(fc.columnName, fc.getAsString(obj)))
.collect(Collectors.joining(" AND "));
sql.append(" AND ").append(conditionClause);
if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND ").append(conditionClause);
}
}
if (TimiJava.isNotEmpty(page.getLikesExample())) {
// 模糊查询
Object obj = page.getLikesExample();
EntityMeta metaExample = getEntityMeta(obj.getClass());
String conditionClause = metaExample.fieldColumnList.stream()
.filter(fc -> fc.isNotNull(obj))
.filter(fc -> fc.isNotEmpty(obj))
.map(fc -> "`%s` LIKE CONCAT('%%', '%s', '%%')".formatted(fc.columnName, fc.getAsString(obj)))
.collect(Collectors.joining(" OR "));
sql.append(" AND (").append(conditionClause).append(')');
if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND (").append(conditionClause).append(')');
}
}
return sql.toString();
}
@ -218,7 +226,7 @@ public class SQLProvider {
public String selectAllByExample(Object entity) {
EntityMeta meta = getEntityMeta(entity.getClass());
String conditionClause = meta.fieldColumnList.stream()
.filter(fc -> fc.isNotNull(entity))
.filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
.collect(Collectors.joining(" AND "));
@ -270,7 +278,7 @@ public class SQLProvider {
}
String setClause = meta.fieldColumnList.stream()
.filter(FieldColumn::isNotId)
.filter(fc -> fc.isNotNull(entity))
.filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
.collect(Collectors.joining(", "));
return "UPDATE `%s` SET %s WHERE `%s` = #{%s}".formatted(meta.table, setClause, meta.idFieldColumn.columnName, meta.idFieldColumn.fieldName);
@ -302,7 +310,7 @@ public class SQLProvider {
String delClause = meta.fieldColumnList.stream()
.filter(FieldColumn::isNotId)
.filter(fc -> fc.isNotNull(entity))
.filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
.collect(Collectors.joining(" AND "));
StringBuilder sql = new StringBuilder("UPDATE `%s` SET `%s` = ".formatted(meta.table, deleteColumn.getColumnName()));
@ -543,16 +551,16 @@ public class SQLProvider {
}
}
public boolean isNull(Object entity) {
public boolean isEmpty(Object entity) {
try {
return Ref.getFieldValue(entity, field, Object.class) == null;
return TimiJava.isEmpty(Ref.getFieldValue(entity, field, Object.class));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public boolean isNotNull(Object entity) {
return !isNull(entity);
public boolean isNotEmpty(Object entity) {
return !isEmpty(entity);
}
public String getAsString(Object obj) {