Skip to content

Commit

Permalink
Merge pull request #894 from getError/feature/for_issue_893
Browse files Browse the repository at this point in the history
为@LogicDelete注解提供两个字段用于处理希望null作为逻辑删除的标记的能力。表示以null作为删除/未删除记录的标识
  • Loading branch information
abel533 authored Apr 6, 2024
2 parents 9913d99 + 55dd584 commit c8016f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

int isDeletedValue() default 1;

// 优先级比isDeletedValue更高 表示以null作为删除记录的标识
boolean isNullForDeletedValue() default false;

int notDeletedValue() default 0;

// 优先级比notDeletedValue更高 表示以null作为未删除记录的标识
boolean isNullForNotDeletedValue() default false;

}
13 changes: 9 additions & 4 deletions core/src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,12 @@ public static String logicDeleteColumnEqualsValue(Class<?> entityClass, boolean
public static String logicDeleteColumnEqualsValue(EntityColumn column, boolean isDeleted) {
String result = "";
if (column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
result = column.getColumn() + " = " + getLogicDeletedValue(column, isDeleted);
Integer logicDeletedValue = getLogicDeletedValue(column, isDeleted);
if(logicDeletedValue==null){
result = column.getColumn() + " is null ";
}else {
result = column.getColumn() + " = " + logicDeletedValue;
}
}
return result;
}
Expand All @@ -788,15 +793,15 @@ public static String logicDeleteColumnEqualsValue(EntityColumn column, boolean i
* @param isDeleted true:逻辑删除的值,false:未逻辑删除的值
* @return
*/
public static int getLogicDeletedValue(EntityColumn column, boolean isDeleted) {
public static Integer getLogicDeletedValue(EntityColumn column, boolean isDeleted) {
if (!column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
throw new LogicDeleteException(column.getColumn() + " 没有 @LogicDelete 注解!");
}
LogicDelete logicDelete = column.getEntityField().getAnnotation(LogicDelete.class);
if (isDeleted) {
return logicDelete.isDeletedValue();
return logicDelete.isNullForDeletedValue()?null: logicDelete.isDeletedValue();
}
return logicDelete.notDeletedValue();
return logicDelete.isNullForNotDeletedValue()?null: logicDelete.notDeletedValue();
}

/**
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/tk/mybatis/mapper/util/OGNL.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ public static String andNotLogicDelete(Object parameter) {
EntityColumn column = entry.getValue();
if (column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
// 未逻辑删除的条件
result = column.getColumn() + " = " + SqlHelper.getLogicDeletedValue(column, false);
Integer logicDeletedValue = SqlHelper.getLogicDeletedValue(column, false);
if(logicDeletedValue==null){
result = column.getColumn() + " is null ";
}else {
result = column.getColumn() + " = " + logicDeletedValue;
}

// 如果Example中有条件,则拼接" and ",
// 如果是空的oredCriteria,则where中只有逻辑删除注解的未删除条件
Expand Down

0 comments on commit c8016f2

Please sign in to comment.