add destroyAllByExample

This commit is contained in:
Timi
2026-01-15 11:20:53 +08:00
parent 7ac4cdae56
commit fd7bb73f5c
6 changed files with 60 additions and 5 deletions

View File

@ -162,4 +162,12 @@ public interface BaseMapper<T, P> {
*/ */
@DeleteProvider(type = SQLProvider.class, method = "destroy") @DeleteProvider(type = SQLProvider.class, method = "destroy")
void destroy(P id); void destroy(P id);
/**
* 根据示例批量销毁(物理删除)
*
* @param t 示例对象
*/
@UpdateProvider(type = SQLProvider.class, method = "destroyAllByExample")
void destroyAllByExample(T t);
} }

View File

@ -150,4 +150,13 @@ public interface DynamicTableMapper<T, P> {
*/ */
@DeleteProvider(type = DynamicTableSQLProvider.class, method = "destroy") @DeleteProvider(type = DynamicTableSQLProvider.class, method = "destroy")
void destroy(@Param("tableName") String tableName, @Param("id") P id); void destroy(@Param("tableName") String tableName, @Param("id") P id);
/**
* 根据示例批量销毁(物理删除)
*
* @param tableName 表名
* @param entity 示例对象
*/
@UpdateProvider(type = DynamicTableSQLProvider.class, method = "destroyAllByExample")
void destroyAllByExample(@Param("tableName") String tableName, @Param("entity") T entity);
} }

View File

@ -414,6 +414,25 @@ public abstract class BaseSQLProvider {
return "DELETE FROM `%s` WHERE `%s` = #{%s}".formatted(tableName, meta.idFieldColumn.columnName, idParam); return "DELETE FROM `%s` WHERE `%s` = #{%s}".formatted(tableName, meta.idFieldColumn.columnName, idParam);
} }
/**
* 构建批量逻辑删除 SQL
*
* @param meta 实体元数据
* @param tableName 表名
* @param entity 示例实体
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
* @return SQL
*/
protected String buildDestroyAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix) {
TimiException.required(meta.canDestroy, "not allow destroy for %s".formatted(meta.entityClass));
String destroyClause = meta.fieldColumnList.stream()
.filter(FieldColumn::isNotId)
.filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName))
.collect(Collectors.joining(" AND "));
return "DELETE FROM `%s` WHERE %s".formatted(tableName, destroyClause);
}
/** /**
* 实体元数据 * 实体元数据
* *

View File

@ -172,4 +172,17 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
EntityMeta meta = getEntityMeta(context); EntityMeta meta = getEntityMeta(context);
return buildDestroySQL(meta, tableName, "id"); return buildDestroySQL(meta, tableName, "id");
} }
/**
* 根据示例批量逻辑删除
*
* @param context 代理器上下文
* @param tableName 表名
* @param entity 实体
* @return SQL
*/
public String destroyAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
EntityMeta meta = getEntityMeta(context);
return buildDestroyAllByExampleSQL(meta, tableName, entity, "entity.");
}
} }

View File

@ -41,9 +41,4 @@ public class RawSQLProvider extends SQLProvider {
.collect(Collectors.joining(" AND ")); .collect(Collectors.joining(" AND "));
return "SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, meta.table, conditionClause); return "SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, meta.table, conditionClause);
} }
@Override
public String deleteAllByExample(Object entity) {
return super.deleteAllByExample(entity);
}
} }

View File

@ -162,4 +162,15 @@ public class SQLProvider extends BaseSQLProvider {
EntityMeta meta = getEntityMeta(context); EntityMeta meta = getEntityMeta(context);
return buildDestroySQL(meta, meta.table, "id"); return buildDestroySQL(meta, meta.table, "id");
} }
/**
* 根据示例批量销毁(物理删除)
*
* @param entity 实体
* @return SQL
*/
public String destroyAllByExample(Object entity) {
EntityMeta meta = getEntityMeta(entity.getClass());
return buildDestroyAllByExampleSQL(meta, meta.table, entity, "");
}
} }