fix SQLProvider example
This commit is contained in:
@ -64,21 +64,25 @@ public class SQLProvider {
|
|||||||
Object obj = page.getEqualsExample();
|
Object obj = page.getEqualsExample();
|
||||||
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
||||||
String conditionClause = metaExample.fieldColumnList.stream()
|
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)))
|
.map(fc -> "`%s` = '%s'".formatted(fc.columnName, fc.getAsString(obj)))
|
||||||
.collect(Collectors.joining(" AND "));
|
.collect(Collectors.joining(" AND "));
|
||||||
|
if (TimiJava.isNotEmpty(conditionClause)) {
|
||||||
sql.append(" AND ").append(conditionClause);
|
sql.append(" AND ").append(conditionClause);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (TimiJava.isNotEmpty(page.getLikesExample())) {
|
if (TimiJava.isNotEmpty(page.getLikesExample())) {
|
||||||
// 模糊查询
|
// 模糊查询
|
||||||
Object obj = page.getLikesExample();
|
Object obj = page.getLikesExample();
|
||||||
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
||||||
String conditionClause = metaExample.fieldColumnList.stream()
|
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)))
|
.map(fc -> "`%s` LIKE CONCAT('%%', '%s', '%%')".formatted(fc.columnName, fc.getAsString(obj)))
|
||||||
.collect(Collectors.joining(" OR "));
|
.collect(Collectors.joining(" OR "));
|
||||||
|
if (TimiJava.isNotEmpty(conditionClause)) {
|
||||||
sql.append(" AND (").append(conditionClause).append(')');
|
sql.append(" AND (").append(conditionClause).append(')');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 排序
|
// 排序
|
||||||
if (TimiJava.isNotEmpty(page.getOrderMap())) {
|
if (TimiJava.isNotEmpty(page.getOrderMap())) {
|
||||||
sql.append(" ORDER BY ");
|
sql.append(" ORDER BY ");
|
||||||
@ -122,21 +126,25 @@ public class SQLProvider {
|
|||||||
Object obj = page.getEqualsExample();
|
Object obj = page.getEqualsExample();
|
||||||
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
||||||
String conditionClause = metaExample.fieldColumnList.stream()
|
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)))
|
.map(fc -> "`%s` = '%s'".formatted(fc.columnName, fc.getAsString(obj)))
|
||||||
.collect(Collectors.joining(" AND "));
|
.collect(Collectors.joining(" AND "));
|
||||||
|
if (TimiJava.isNotEmpty(conditionClause)) {
|
||||||
sql.append(" AND ").append(conditionClause);
|
sql.append(" AND ").append(conditionClause);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (TimiJava.isNotEmpty(page.getLikesExample())) {
|
if (TimiJava.isNotEmpty(page.getLikesExample())) {
|
||||||
// 模糊查询
|
// 模糊查询
|
||||||
Object obj = page.getLikesExample();
|
Object obj = page.getLikesExample();
|
||||||
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
||||||
String conditionClause = metaExample.fieldColumnList.stream()
|
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)))
|
.map(fc -> "`%s` LIKE CONCAT('%%', '%s', '%%')".formatted(fc.columnName, fc.getAsString(obj)))
|
||||||
.collect(Collectors.joining(" OR "));
|
.collect(Collectors.joining(" OR "));
|
||||||
|
if (TimiJava.isNotEmpty(conditionClause)) {
|
||||||
sql.append(" AND (").append(conditionClause).append(')');
|
sql.append(" AND (").append(conditionClause).append(')');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,7 +226,7 @@ public class SQLProvider {
|
|||||||
public String selectAllByExample(Object entity) {
|
public String selectAllByExample(Object entity) {
|
||||||
EntityMeta meta = getEntityMeta(entity.getClass());
|
EntityMeta meta = getEntityMeta(entity.getClass());
|
||||||
String conditionClause = meta.fieldColumnList.stream()
|
String conditionClause = meta.fieldColumnList.stream()
|
||||||
.filter(fc -> fc.isNotNull(entity))
|
.filter(fc -> fc.isNotEmpty(entity))
|
||||||
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
|
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
|
||||||
.collect(Collectors.joining(" AND "));
|
.collect(Collectors.joining(" AND "));
|
||||||
|
|
||||||
@ -270,7 +278,7 @@ public class SQLProvider {
|
|||||||
}
|
}
|
||||||
String setClause = meta.fieldColumnList.stream()
|
String setClause = meta.fieldColumnList.stream()
|
||||||
.filter(FieldColumn::isNotId)
|
.filter(FieldColumn::isNotId)
|
||||||
.filter(fc -> fc.isNotNull(entity))
|
.filter(fc -> fc.isNotEmpty(entity))
|
||||||
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
|
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
|
||||||
.collect(Collectors.joining(", "));
|
.collect(Collectors.joining(", "));
|
||||||
return "UPDATE `%s` SET %s WHERE `%s` = #{%s}".formatted(meta.table, setClause, meta.idFieldColumn.columnName, meta.idFieldColumn.fieldName);
|
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()
|
String delClause = meta.fieldColumnList.stream()
|
||||||
.filter(FieldColumn::isNotId)
|
.filter(FieldColumn::isNotId)
|
||||||
.filter(fc -> fc.isNotNull(entity))
|
.filter(fc -> fc.isNotEmpty(entity))
|
||||||
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
|
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
|
||||||
.collect(Collectors.joining(" AND "));
|
.collect(Collectors.joining(" AND "));
|
||||||
StringBuilder sql = new StringBuilder("UPDATE `%s` SET `%s` = ".formatted(meta.table, deleteColumn.getColumnName()));
|
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 {
|
try {
|
||||||
return Ref.getFieldValue(entity, field, Object.class) == null;
|
return TimiJava.isEmpty(Ref.getFieldValue(entity, field, Object.class));
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isNotNull(Object entity) {
|
public boolean isNotEmpty(Object entity) {
|
||||||
return !isNull(entity);
|
return !isEmpty(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAsString(Object obj) {
|
public String getAsString(Object obj) {
|
||||||
|
|||||||
Reference in New Issue
Block a user