Compare commits

..

2 Commits

Author SHA1 Message Date
1508bf7c7f fix SQLProvider example 2025-12-25 18:09:31 +08:00
e0398b3a22 rename likeExample to likesExample 2025-12-25 14:57:49 +08:00
2 changed files with 32 additions and 24 deletions

View File

@ -17,7 +17,7 @@ public class Page<T> extends BasePage {
protected T equalsExample;
protected T likeExample;
protected T likesExample;
protected LinkedHashMap<String, BaseMapper.OrderType> orderMap;
@ -44,12 +44,12 @@ public class Page<T> extends BasePage {
this.equalsExample = equalsExample;
}
public T getLikeExample() {
return likeExample;
public T getLikesExample() {
return likesExample;
}
public void setLikeExample(T likeExample) {
this.likeExample = likeExample;
public void setLikesExample(T likesExample) {
this.likesExample = likesExample;
}
public LinkedHashMap<String, BaseMapper.OrderType> getOrderMap() {

View File

@ -64,21 +64,25 @@ 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 "));
if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND ").append(conditionClause);
}
if (TimiJava.isNotEmpty(page.getLikeExample())) {
}
if (TimiJava.isNotEmpty(page.getLikesExample())) {
// 模糊查询
Object obj = page.getLikeExample();
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 "));
if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND (").append(conditionClause).append(')');
}
}
// 排序
if (TimiJava.isNotEmpty(page.getOrderMap())) {
sql.append(" ORDER BY ");
@ -122,21 +126,25 @@ 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 "));
if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND ").append(conditionClause);
}
if (TimiJava.isNotEmpty(page.getLikeExample())) {
}
if (TimiJava.isNotEmpty(page.getLikesExample())) {
// 模糊查询
Object obj = page.getLikeExample();
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 "));
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) {