hubin 7 лет назад
Родитель
Сommit
d3b7fa8d8e

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

@@ -163,6 +163,34 @@ 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字段

+ 87 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/LogicSqlInjector.java

@@ -0,0 +1,87 @@
+/*
+ * 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.core.injector;
+
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.ibatis.session.Configuration;
+
+import com.baomidou.mybatisplus.core.injector.methods.Insert;
+import com.baomidou.mybatisplus.core.injector.methods.InsertAllColumn;
+import com.baomidou.mybatisplus.core.injector.methods.LogicDelete;
+import com.baomidou.mybatisplus.core.injector.methods.LogicDeleteBatchByIds;
+import com.baomidou.mybatisplus.core.injector.methods.LogicDeleteById;
+import com.baomidou.mybatisplus.core.injector.methods.LogicDeleteByMap;
+import com.baomidou.mybatisplus.core.injector.methods.SelectBatchByIds;
+import com.baomidou.mybatisplus.core.injector.methods.SelectById;
+import com.baomidou.mybatisplus.core.injector.methods.SelectByMap;
+import com.baomidou.mybatisplus.core.injector.methods.SelectCount;
+import com.baomidou.mybatisplus.core.injector.methods.SelectList;
+import com.baomidou.mybatisplus.core.injector.methods.SelectMaps;
+import com.baomidou.mybatisplus.core.injector.methods.SelectMapsPage;
+import com.baomidou.mybatisplus.core.injector.methods.SelectObjs;
+import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
+import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
+import com.baomidou.mybatisplus.core.injector.methods.Update;
+import com.baomidou.mybatisplus.core.injector.methods.UpdateAllColumnById;
+import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
+
+
+/**
+ * <p>
+ * SQL 逻辑删除注入器
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-12
+ */
+public class LogicSqlInjector extends AbstractSqlInjector {
+
+
+    @Override
+    public List<AbstractMethod> getMethodList() {
+        return Stream.of(
+            new Insert(),
+            new InsertAllColumn(),
+            new LogicDelete(),
+            new LogicDeleteByMap(),
+            new LogicDeleteById(),
+            new LogicDeleteBatchByIds(),
+            new Update(),
+            new UpdateById(),
+            new UpdateAllColumnById(),
+            new SelectById(),
+            new SelectBatchByIds(),
+            new SelectByMap(),
+            new SelectOne(),
+            new SelectCount(),
+            new SelectMaps(),
+            new SelectMapsPage(),
+            new SelectObjs(),
+            new SelectList(),
+            new SelectList(),
+            new SelectPage()
+        ).collect(Collectors.toList());
+    }
+
+
+    @Override
+    public void injectSqlRunner(Configuration configuration) {
+        new SqlRunnerInjector().inject(configuration);
+    }
+}

+ 2 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteBatchByIds.java

@@ -35,11 +35,8 @@ public class DeleteBatchByIds extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.DELETE_BATCH_BY_IDS;
-        StringBuilder ids = new StringBuilder();
-        ids.append("\n<foreach item=\"item\" collection=\"coll\" separator=\",\">");
-        ids.append("#{item}");
-        ids.append("\n</foreach>");
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), ids.toString());
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
+            "<foreach item=\"item\" collection=\"coll\" separator=\",\">#{item}</foreach>");
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
     }

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

@@ -34,9 +34,10 @@ public class LogicDelete extends AbstractMethod {
 
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
-        SqlMethod sqlMethod = SqlMethod.DELETE_BY_ID;
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(),
-            tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty()), modelClass);
-        return this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
+        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo),
+            this.sqlWhereEntityWrapper(tableInfo));
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
     }
 }

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

@@ -34,13 +34,10 @@ public class LogicDeleteBatchByIds extends AbstractMethod {
 
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
-        SqlMethod sqlMethod = SqlMethod.DELETE_BATCH_BY_IDS;
-        StringBuilder ids = new StringBuilder();
-        ids.append("\n<foreach item=\"item\" index=\"index\" collection=\"coll\" separator=\",\">");
-        ids.append("#{item}");
-        ids.append("\n</foreach>");
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), ids.toString());
+        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BATCH_BY_IDS;
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo), tableInfo.getKeyColumn(),
+            "<foreach item=\"item\" collection=\"coll\" separator=\",\">#{item}</foreach>");
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
+        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
     }
 }

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

@@ -34,9 +34,10 @@ public class LogicDeleteById extends AbstractMethod {
 
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
-        SqlMethod sqlMethod = SqlMethod.DELETE_BY_ID;
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(),
-            tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty()), modelClass);
-        return this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
+        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_ID;
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo),
+            tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
     }
 }

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

@@ -15,6 +15,8 @@
  */
 package com.baomidou.mybatisplus.core.injector.methods;
 
+import java.util.Map;
+
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
@@ -34,9 +36,10 @@ public class LogicDeleteByMap extends AbstractMethod {
 
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
-        SqlMethod sqlMethod = SqlMethod.DELETE_BY_ID;
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(),
-            tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty()), modelClass);
-        return this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
+        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_MAP;
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo),
+            sqlWhereByMap(tableInfo));
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
+        return this.addUpdateMappedStatement(mapperClass, Map.class, sqlMethod.getMethod(), sqlSource);
     }
 }

+ 30 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -38,11 +38,12 @@ import java.util.concurrent.ConcurrentHashMap;
 public final class LambdaUtils {
 
     private static final Map<String, Map<String, String>> LAMBDA_CACHE = new ConcurrentHashMap<>();
-    // 做个小小的缓存
-    private static final Map<Class, WeakReference<SerializedLambda>> CACHE = new WeakHashMap<>();
+    private static final Map<Class, WeakReference<SerializedLambda>> FUNC_CACHE = new WeakHashMap<>();
 
     /**
+     * <p>
      * 解析 lambda 表达式
+     * </p>
      *
      * @param func 需要解析的 lambda 对象
      * @param <T>  类型,被调用的 Function 对象的目标类型
@@ -50,21 +51,37 @@ public final class LambdaUtils {
      */
     public static <T> SerializedLambda resolve(Property<T, ?> func) {
         Class clazz = func.getClass();
-        return Optional.ofNullable(CACHE.get(clazz))
+        return Optional.ofNullable(FUNC_CACHE.get(clazz))
             .map(WeakReference::get)
             .orElseGet(() -> {
                 SerializedLambda lambda = SerializedLambda.convert(func);
-                CACHE.put(clazz, new WeakReference<>(lambda));
+                FUNC_CACHE.put(clazz, new WeakReference<>(lambda));
                 return lambda;
             });
     }
 
+    /**
+     * <p>
+     * 缓存实体类名与表字段映射关系
+     * </p>
+     *
+     * @param className 实体类名
+     * @param tableInfo 表信息
+     */
     public static void createCache(String className, TableInfo tableInfo) {
         LAMBDA_CACHE.put(className, createLambdaMap(tableInfo));
     }
 
+    /**
+     * <p>
+     * 缓存实体字段 MAP 信息
+     * </p>
+     *
+     * @param tableInfo 表信息
+     * @return
+     */
     private static Map<String, String> createLambdaMap(TableInfo tableInfo) {
-        Map<String, String> map = new HashMap<>();
+        Map<String, String> map = new HashMap<>(16);
         if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
             map.put(tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
         }
@@ -72,6 +89,14 @@ public final class LambdaUtils {
         return map;
     }
 
+    /**
+     * <p>
+     * 获取实体对应字段 MAP
+     * </p>
+     *
+     * @param entityClassName 实体类名
+     * @return
+     */
     public static Map<String, String> getColumnMap(String entityClassName) {
         return LAMBDA_CACHE.get(entityClassName);
     }

+ 5 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/TableInfoHelper.java

@@ -60,11 +60,6 @@ public class TableInfoHelper {
      * 缓存反射类表信息
      */
     private static final Map<String, TableInfo> TABLE_INFO_CACHE = new ConcurrentHashMap<>();
-
-    /**
-     * lambda 字段缓存
-     */
-    private static final Map<String, String> LAMBDA_COLUMN_CACHE = new ConcurrentHashMap<>();
     /**
      * 默认表主键
      */
@@ -218,10 +213,15 @@ public class TableInfoHelper {
         if (StringUtils.isEmpty(tableInfo.getKeyColumn())) {
             logger.warn(String.format("Warn: Could not find @TableId in Class: %s.", clazz.getName()));
         }
+
         /*
          * 注入
          */
         TABLE_INFO_CACHE.put(clazz.getName(), tableInfo);
+
+        /*
+         * 缓存 Lambda 映射关系
+         */
         LambdaUtils.createCache(clazz.getName(), tableInfo);
         return tableInfo;
     }