Browse Source

分离逻辑删除方法

hubin 7 years ago
parent
commit
492ce08d47

+ 0 - 28
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -163,34 +163,6 @@ public abstract class AbstractMethod {
         return set.toString();
     }
 
-    /**
-     * <p>
-     * SQL 更新 set 语句
-     * </p>
-     *
-     * @param table 表信息
-     * @return sql set 片段
-     */
-    protected String sqlLogicSet(TableInfo table) {
-        List<TableFieldInfo> fieldList = table.getFieldList();
-        StringBuilder set = new StringBuilder("SET ");
-        int i = 0;
-        for (TableFieldInfo fieldInfo : fieldList) {
-            if (fieldInfo.isLogicDelete()) {
-                if (++i > 1) {
-                    set.append(",");
-                }
-                set.append(fieldInfo.getColumn()).append("=");
-                if (StringUtils.isCharSequence(fieldInfo.getPropertyType())) {
-                    set.append("'").append(fieldInfo.getLogicDeleteValue()).append("'");
-                } else {
-                    set.append(fieldInfo.getLogicDeleteValue());
-                }
-            }
-        }
-        return set.toString();
-    }
-
     /**
      * <p>
      * 获取需要转义的SQL字段

+ 4 - 5
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDelete.java

@@ -19,7 +19,6 @@ import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
-import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 
 /**
@@ -30,14 +29,14 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
  * @author hubin
  * @since 2018-04-06
  */
-public class LogicDelete extends AbstractMethod {
+public class LogicDelete extends LogicMethod {
 
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo),
-            this.sqlWhereEntityWrapper(tableInfo));
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
+            sqlWhereEntityWrapper(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
+        return addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
     }
 }

+ 3 - 4
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteBatchByIds.java

@@ -19,7 +19,6 @@ import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
-import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 
 /**
@@ -30,14 +29,14 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
  * @author hubin
  * @since 2018-04-06
  */
-public class LogicDeleteBatchByIds extends AbstractMethod {
+public class LogicDeleteBatchByIds extends LogicMethod {
 
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BATCH_BY_IDS;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo), tableInfo.getKeyColumn(),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo), tableInfo.getKeyColumn(),
             "<foreach item=\"item\" collection=\"coll\" separator=\",\">#{item}</foreach>");
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
+        return addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
     }
 }

+ 3 - 4
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteById.java

@@ -19,7 +19,6 @@ import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
-import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 
 /**
@@ -30,14 +29,14 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
  * @author hubin
  * @since 2018-04-06
  */
-public class LogicDeleteById extends AbstractMethod {
+public class LogicDeleteById extends LogicMethod {
 
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_ID;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
             tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
+        return addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
     }
 }

+ 3 - 4
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteByMap.java

@@ -21,7 +21,6 @@ import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
-import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 
 /**
@@ -32,14 +31,14 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
  * @author hubin
  * @since 2018-04-06
  */
-public class LogicDeleteByMap extends AbstractMethod {
+public class LogicDeleteByMap extends LogicMethod {
 
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_MAP;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
             sqlWhereByMap(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
-        return this.addUpdateMappedStatement(mapperClass, Map.class, sqlMethod.getMethod(), sqlSource);
+        return addUpdateMappedStatement(mapperClass, Map.class, sqlMethod.getMethod(), sqlSource);
     }
 }

+ 64 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicMethod.java

@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.injector.methods;
+
+import java.util.List;
+
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+
+/**
+ * <p>
+ * 抽象的注入方法类
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-04-06
+ */
+public abstract class LogicMethod extends AbstractMethod {
+
+
+    /**
+     * <p>
+     * SQL 更新 set 语句
+     * </p>
+     *
+     * @param table 表信息
+     * @return sql set 片段
+     */
+    protected String sqlLogicSet(TableInfo table) {
+        List<TableFieldInfo> fieldList = table.getFieldList();
+        StringBuilder set = new StringBuilder("SET ");
+        int i = 0;
+        for (TableFieldInfo fieldInfo : fieldList) {
+            if (fieldInfo.isLogicDelete()) {
+                if (++i > 1) {
+                    set.append(",");
+                }
+                set.append(fieldInfo.getColumn()).append("=");
+                if (StringUtils.isCharSequence(fieldInfo.getPropertyType())) {
+                    set.append("'").append(fieldInfo.getLogicDeleteValue()).append("'");
+                } else {
+                    set.append(fieldInfo.getLogicDeleteValue());
+                }
+            }
+        }
+        return set.toString();
+    }
+
+}