add PageIgnore
This commit is contained in:
@ -0,0 +1,21 @@
|
||||
package com.imyeyu.spring.annotation.table;
|
||||
|
||||
import com.imyeyu.spring.bean.Page;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 在 {@link com.imyeyu.spring.mapper.BaseMapper#page(Page)} 方法忽略查询该属性
|
||||
* <br />
|
||||
* {@link com.imyeyu.spring.service.AbstractEntityService#page(Page)} 同上
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-12-12 14:54
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface PageIgnore {
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.CallbackArgReturn;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
@ -8,6 +9,7 @@ import com.imyeyu.spring.annotation.table.AutoUUID;
|
||||
import com.imyeyu.spring.annotation.table.Column;
|
||||
import com.imyeyu.spring.annotation.table.DeleteColumn;
|
||||
import com.imyeyu.spring.annotation.table.Id;
|
||||
import com.imyeyu.spring.annotation.table.PageIgnore;
|
||||
import com.imyeyu.spring.annotation.table.Table;
|
||||
import com.imyeyu.spring.annotation.table.Transient;
|
||||
import com.imyeyu.spring.bean.Page;
|
||||
@ -53,7 +55,7 @@ public class SQLProvider {
|
||||
public String listByPage(ProviderContext context, @Param("page") Page<?> page) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("SELECT %s FROM `%s` WHERE 1 = 1".formatted(meta.selectAllClause, meta.table));
|
||||
sql.append("SELECT %s FROM `%s` WHERE 1 = 1".formatted(meta.selectPageClause, meta.table));
|
||||
if (meta.canDelete) {
|
||||
sql.append(" AND (`deleted_at` IS NULL OR %s < `deleted_at`)".formatted(Time.now()));
|
||||
}
|
||||
@ -367,6 +369,9 @@ public class SQLProvider {
|
||||
/** 查询字段映射 */
|
||||
final String selectAllClause;
|
||||
|
||||
/** 页面查询字段映射 */
|
||||
final String selectPageClause;
|
||||
|
||||
/** ID 字段 */
|
||||
final FieldColumn idFieldColumn;
|
||||
|
||||
@ -400,8 +405,6 @@ public class SQLProvider {
|
||||
TimiException.required(this.table, String.format("empty table annotation value for %s entity", entityClass.getName()));
|
||||
}
|
||||
List<Field> allFieldList = Ref.listAllFields(entityClass);
|
||||
|
||||
StringBuilder selectAllClause = new StringBuilder();
|
||||
FieldColumn idFieldColumn = null;
|
||||
List<FieldColumn> fieldColumnList = new ArrayList<>();
|
||||
for (int i = 0; i < allFieldList.size(); i++) {
|
||||
@ -414,21 +417,10 @@ public class SQLProvider {
|
||||
TimiException.requiredNull(idFieldColumn, String.format("multi id field for %s entity", entityClass.getName()));
|
||||
idFieldColumn = fieldColumn;
|
||||
}
|
||||
{
|
||||
Column column = field.getAnnotation(Column.class);
|
||||
if (column == null) {
|
||||
selectAllClause.append('`').append(fieldColumn.columnName).append('`');
|
||||
selectAllClause.append(',');
|
||||
} else {
|
||||
// 处理自定义映射列名
|
||||
selectAllClause.append('`').append(column.value()).append('`');
|
||||
selectAllClause.append(" AS `").append(fieldColumn.fieldName).append('`');
|
||||
selectAllClause.append(',');
|
||||
}
|
||||
}
|
||||
fieldColumnList.add(fieldColumn);
|
||||
}
|
||||
this.selectAllClause = selectAllClause.substring(0, selectAllClause.length() - 1);
|
||||
this.selectAllClause = buildSelectClause(fieldColumnList, null);
|
||||
this.selectPageClause = buildSelectClause(fieldColumnList, fc -> !fc.getField().isAnnotationPresent(PageIgnore.class));
|
||||
this.idFieldColumn = idFieldColumn;
|
||||
this.fieldColumnList = List.of(fieldColumnList.toArray(new FieldColumn[0])); // 转为只读
|
||||
canCreate = Creatable.class.isAssignableFrom(entityClass);
|
||||
@ -437,6 +429,28 @@ public class SQLProvider {
|
||||
canDestroy = Destroyable.class.isAssignableFrom(entityClass);
|
||||
}
|
||||
|
||||
private String buildSelectClause(List<FieldColumn> fieldColumnList, CallbackArgReturn<FieldColumn, Boolean> callback) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < fieldColumnList.size(); i++) {
|
||||
FieldColumn fieldColumn = fieldColumnList.get(i);
|
||||
Field field = fieldColumn.getField();
|
||||
if (callback != null && !callback.handler(fieldColumn)) {
|
||||
continue;
|
||||
}
|
||||
Column column = field.getAnnotation(Column.class);
|
||||
if (column == null) {
|
||||
sb.append('`').append(fieldColumn.columnName).append('`');
|
||||
sb.append(',');
|
||||
} else {
|
||||
// 自定义映射列名
|
||||
sb.append('`').append(column.value()).append('`');
|
||||
sb.append(" AS `").append(fieldColumn.fieldName).append('`');
|
||||
sb.append(',');
|
||||
}
|
||||
}
|
||||
return sb.substring(0, sb.length() - 1);
|
||||
}
|
||||
|
||||
public Class<?> getEntityClass() {
|
||||
return entityClass;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user