add BaseMapper.deleteAllByExample
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
package com.imyeyu.spring.annotation.table;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-12-01 10:56
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface DeleteColumn {
|
||||
|
||||
Type value() default Type.UNIX;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-12-01 10:57
|
||||
*/
|
||||
enum Type {
|
||||
|
||||
UNIX,
|
||||
|
||||
DATE,
|
||||
|
||||
DATE_TIME
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package com.imyeyu.spring.entity;
|
||||
|
||||
import com.imyeyu.spring.annotation.table.DeleteColumn;
|
||||
import com.imyeyu.utils.Time;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -19,6 +20,7 @@ public class BaseEntity implements Serializable, Creatable, Updatable, Deletable
|
||||
protected Long updatedAt;
|
||||
|
||||
/** 删除时间 */
|
||||
@DeleteColumn
|
||||
protected Long deletedAt;
|
||||
|
||||
/**
|
||||
|
||||
@ -31,13 +31,13 @@ public interface BaseMapper<T, P> {
|
||||
DESC
|
||||
}
|
||||
|
||||
static final String NOT_DELETE = " AND `deleted_at` IS NULL ";
|
||||
String NOT_DELETE = " AND `deleted_at` IS NULL ";
|
||||
|
||||
static final String LIMIT_1 = " LIMIT 1";
|
||||
String LIMIT_1 = " LIMIT 1";
|
||||
|
||||
static final String UNIX_TIME = " FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000) ";
|
||||
String UNIX_TIME = " FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000) ";
|
||||
|
||||
static final String PAGE = NOT_DELETE + " LIMIT #{offset}, #{limit}";
|
||||
String PAGE = NOT_DELETE + " LIMIT #{offset}, #{limit}";
|
||||
|
||||
/**
|
||||
* 统计数据量
|
||||
@ -107,6 +107,9 @@ public interface BaseMapper<T, P> {
|
||||
@UpdateProvider(type = SQLProvider.class, method = "delete")
|
||||
void delete(P id);
|
||||
|
||||
@UpdateProvider(type = SQLProvider.class, method = "deleteAllByExample")
|
||||
void deleteAllByExample(T t);
|
||||
|
||||
/**
|
||||
* 销毁(物理删除)
|
||||
*
|
||||
|
||||
@ -6,6 +6,7 @@ import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
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.Table;
|
||||
import com.imyeyu.spring.annotation.table.Transient;
|
||||
@ -23,6 +24,7 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@ -232,6 +234,29 @@ public class SQLProvider {
|
||||
return "UPDATE `%s` SET `deleted_at` = %s WHERE `%s` = #{id}".formatted(meta.table, Time.now(), meta.idFieldColumn.columnName);
|
||||
}
|
||||
|
||||
public String deleteAllByExample(Object entity) {
|
||||
EntityMeta meta = getEntityMeta(entity.getClass());
|
||||
TimiException.required(meta.canDelete, "not allow delete for %s".formatted(meta.entityClass));
|
||||
|
||||
FieldColumn deleteColumn = meta.getFieldColumnList().stream().filter(fc -> fc.isDeleteColumn).findFirst().orElse(null);
|
||||
TimiException.required(deleteColumn, "unknown delete column, use com.imyeyu.spring.annotation.table.DeleteColumn annotation on field");
|
||||
assert deleteColumn != null;
|
||||
assert deleteColumn.deleteColumnType != null;
|
||||
|
||||
String delClause = meta.fieldColumnList.stream()
|
||||
.filter(FieldColumn::isNotId)
|
||||
.filter(fc -> fc.isNotNull(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()));
|
||||
sql.append("'").append(switch (deleteColumn.deleteColumnType) {
|
||||
case UNIX -> Time.now();
|
||||
case DATE, DATE_TIME -> new Date();
|
||||
}).append("'");
|
||||
sql.append(" WHERE ").append(delClause);
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 硬删除,需要实体实现 {@link Destroyable}
|
||||
*
|
||||
@ -419,6 +444,10 @@ public class SQLProvider {
|
||||
|
||||
final boolean isAutoUpperUUID;
|
||||
|
||||
final boolean isDeleteColumn;
|
||||
|
||||
final DeleteColumn.Type deleteColumnType;
|
||||
|
||||
public FieldColumn(Field field) {
|
||||
this.field = field;
|
||||
|
||||
@ -437,6 +466,12 @@ public class SQLProvider {
|
||||
} else {
|
||||
isAutoUpperUUID = false;
|
||||
}
|
||||
isDeleteColumn = field.isAnnotationPresent(DeleteColumn.class);
|
||||
if (isDeleteColumn) {
|
||||
deleteColumnType = field.getAnnotation(DeleteColumn.class).value();
|
||||
} else {
|
||||
deleteColumnType = null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isNull(Object entity) {
|
||||
|
||||
Reference in New Issue
Block a user