Logical Deletion
Logical deletion is designed as a
String
type for compatibility with various data types. You can modify the data type via the configuration file or by using thedateType
attribute in the@CollectionLogic
annotation. Recommended strategies for filling logical deletion fields: database default field > manual setting > built-in interceptor > custom interceptor.
Configuration File
- You can configure additional options in the configuration file (optional: by default, logical deletion is disabled):
yaml
mongo-plus:
configuration:
logic:
open: true # Enable logical deletion
auto-fill: true # Enable interceptor to automatically fill logical deletion fields (effective only if logical deletion is enabled; recommended to use database defaults and entity defaults to reduce interceptor modifications)
logic-delete-field: logicDel # Global logical deletion field (default type: string)
logic-delete-value: 1 # Value indicating logical deletion
logic-not-delete-value: 0 # Value indicating not deleted
logic-data-type: integer # Override data type to Integer
Ignoring Logical Deletion
- When configuring global logical deletion, you can customize the logical deletion field and marker values.
- You can also ignore logical deletion for specific documents.
Annotation Attributes
java
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface CollectionLogic {
/**
* Disable global configuration and ignore this document (can be applied to any field; recommended to place on the first field, usually the @ID field)
*/
boolean close() default false;
/**
* Default value indicating not deleted (can be omitted; automatically obtained from global configuration)
*/
String value() default "";
/**
* Default value indicating deleted (can be omitted; automatically obtained from global configuration)
*/
String delval() default "";
/**
* Data type of the logical deletion field
*/
LogicDataType delType() default LogicDataType.DEFAULT;
}
Custom Logical Deletion Field Example
java
@CollectionName(value = "mongoTest")
public class MongoTest {
@ID(type = IdTypeEnum.AUTO)
private String id;
private String userName;
/**
* Mark this document to not use the global logicDel configuration.
* Use 'logic' as the logical deletion field: true = deleted, false = not deleted
*/
@CollectionLogic(value = "true", delval = "false")
private String logic;
}
Ignore Logical Deletion for a Document
java
@CollectionName(value = "mongoTest")
// Mark this document to ignore logical deletion
@IgnoreLogic
public class MongoTest {
@ID(type = IdTypeEnum.AUTO)
private String id;
private String userName;
private String logic;
}
Ignore Logical Deletion for a Method
java
// This method ignores logical deletion
@IgnoreLogic
public void ignore() {
List<Object> result = mapper.list();
}
Programmatic Fine-Grained Ignore of Logical Deletion
java
public void ignore() {
List<Object> result1 = mapper.list();
// Fine-grained ignore of logical deletion
LogicManager.withoutLogic(() -> {
List<Object> result2 = mapper.list();
});
}