From 58c25cda001fb8268e65e49da61609e98f398fce Mon Sep 17 00:00:00 2001 From: wangyuhao12 Date: Fri, 3 Nov 2023 11:37:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=B8=BA@LogicDelete=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E4=B8=A4=E4=B8=AA=E5=AD=97=E6=AE=B5=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=A4=84=E7=90=86=E5=B8=8C=E6=9C=9Bnull=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E9=80=BB=E8=BE=91=E5=88=A0=E9=99=A4=E7=9A=84=E6=A0=87?= =?UTF-8?q?=E8=AE=B0=E7=9A=84=E8=83=BD=E5=8A=9B=E3=80=82=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E4=BB=A5null=E4=BD=9C=E4=B8=BA=E5=88=A0=E9=99=A4/=E6=9C=AA?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=AE=B0=E5=BD=95=E7=9A=84=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tk/mybatis/mapper/annotation/LogicDelete.java | 8 ++++++++ .../tk/mybatis/mapper/mapperhelper/SqlHelper.java | 13 +++++++++---- core/src/main/java/tk/mybatis/mapper/util/OGNL.java | 7 ++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java b/core/src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java index fc29872f5..8ea911ef9 100644 --- a/core/src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java +++ b/core/src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java @@ -1,5 +1,7 @@ package tk.mybatis.mapper.annotation; +import scala.Int; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -14,6 +16,12 @@ int isDeletedValue() default 1; + // 优先级比isDeletedValue更高 表示以null作为删除记录的标识 + boolean isNullForDeletedValue() default false; + int notDeletedValue() default 0; + // 优先级比notDeletedValue更高 表示以null作为未删除记录的标识 + boolean isNullForNotDeletedValue() default false; + } diff --git a/core/src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java b/core/src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java index 949a44042..83c7391b5 100644 --- a/core/src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java +++ b/core/src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java @@ -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; } @@ -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(); } /** diff --git a/core/src/main/java/tk/mybatis/mapper/util/OGNL.java b/core/src/main/java/tk/mybatis/mapper/util/OGNL.java index a5e4a8959..34bbe0513 100644 --- a/core/src/main/java/tk/mybatis/mapper/util/OGNL.java +++ b/core/src/main/java/tk/mybatis/mapper/util/OGNL.java @@ -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中只有逻辑删除注解的未删除条件 From 55dd584efc2c9c324b52f21c0b6bd6b66e769bcb Mon Sep 17 00:00:00 2001 From: wangyuhao12 Date: Fri, 3 Nov 2023 12:08:42 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=9C=AA=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java b/core/src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java index 8ea911ef9..ecd482ecb 100644 --- a/core/src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java +++ b/core/src/main/java/tk/mybatis/mapper/annotation/LogicDelete.java @@ -1,7 +1,5 @@ package tk.mybatis.mapper.annotation; -import scala.Int; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy;