Sfoglia il codice sorgente

精简注入方法

hubin 7 anni fa
parent
commit
a12b6be20c

+ 0 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java

@@ -28,7 +28,6 @@ public enum SqlMethod {
      * 插入
      */
     INSERT_ONE("insert", "插入一条数据(选择字段插入)", "<script>INSERT INTO %s %s VALUES %s</script>"),
-    INSERT_ONE_ALL_COLUMN("insertAllColumn", "插入一条数据(全部字段插入)", "<script>INSERT INTO %s %s VALUES %s</script>"),
 
     /**
      * 删除
@@ -50,7 +49,6 @@ public enum SqlMethod {
      * 修改
      */
     UPDATE_BY_ID("updateById", "根据ID 选择修改数据", "<script>UPDATE %s %s WHERE %s=#{%s} %s</script>"),
-    UPDATE_ALL_COLUMN_BY_ID("updateAllColumnById", "根据ID 修改全部数据", "<script>UPDATE %s %s WHERE %s=#{%s} %s</script>"),
     UPDATE("update", "根据 whereEntity 条件,更新记录", "<script>UPDATE %s %s %s</script>"),
 
     /**

+ 0 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/DefaultSqlInjector.java

@@ -38,14 +38,12 @@ public class DefaultSqlInjector extends AbstractSqlInjector {
     public List<AbstractMethod> getMethodList() {
         return Stream.of(
             new Insert(),
-            new InsertAllColumn(),
             new Delete(),
             new DeleteByMap(),
             new DeleteById(),
             new DeleteBatchByIds(),
             new Update(),
             new UpdateById(),
-            new UpdateAllColumnById(),
             new SelectById(),
             new SelectBatchByIds(),
             new SelectByMap(),

+ 0 - 92
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/InsertAllColumn.java

@@ -1,92 +0,0 @@
-/*
- * 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.methods;
-
-import java.util.List;
-
-import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
-import org.apache.ibatis.executor.keygen.KeyGenerator;
-import org.apache.ibatis.executor.keygen.NoKeyGenerator;
-import org.apache.ibatis.mapping.MappedStatement;
-import org.apache.ibatis.mapping.SqlSource;
-
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.core.enums.SqlMethod;
-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;
-import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
-
-/**
- * <p>
- * 根据 ID 删除
- * </p>
- *
- * @author hubin
- * @since 2018-04-06
- */
-public class InsertAllColumn extends AbstractMethod {
-
-    @Override
-    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
-        KeyGenerator keyGenerator = new NoKeyGenerator();
-        StringBuilder fieldBuilder = new StringBuilder();
-        StringBuilder placeholderBuilder = new StringBuilder();
-        SqlMethod sqlMethod = SqlMethod.INSERT_ONE_ALL_COLUMN;
-
-        fieldBuilder.append("\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
-        placeholderBuilder.append("\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
-        String keyProperty = null;
-        String keyColumn = null;
-
-        // 表包含主键处理逻辑,如果不包含主键当普通字段处理
-        if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
-            if (tableInfo.getIdType() == IdType.AUTO) {
-                /** 自增主键 */
-                keyGenerator = new Jdbc3KeyGenerator();
-                keyProperty = tableInfo.getKeyProperty();
-                keyColumn = tableInfo.getKeyColumn();
-            } else {
-                if (null != tableInfo.getKeySequence()) {
-                    keyGenerator = TableInfoHelper.genKeyGenerator(tableInfo, builderAssistant, sqlMethod.getMethod(), languageDriver);
-                    keyProperty = tableInfo.getKeyProperty();
-                    keyColumn = tableInfo.getKeyColumn();
-                    fieldBuilder.append(tableInfo.getKeyColumn()).append(",");
-                    placeholderBuilder.append("#{").append(tableInfo.getKeyProperty()).append("},");
-                } else {
-                    /** 用户输入自定义ID */
-                    fieldBuilder.append(tableInfo.getKeyColumn()).append(",");
-                    // 正常自定义主键策略
-                    placeholderBuilder.append("#{").append(tableInfo.getKeyProperty()).append("},");
-                }
-            }
-        }
-
-        // 是否 IF 标签判断
-        boolean ifTag;
-        List<TableFieldInfo> fieldList = tableInfo.getFieldList();
-        for (TableFieldInfo fieldInfo : fieldList) {
-            fieldBuilder.append(fieldInfo.getColumn()).append(",");
-            placeholderBuilder.append("#{").append(fieldInfo.getEl()).append("},");
-        }
-        fieldBuilder.append("\n</trim>");
-        placeholderBuilder.append("\n</trim>");
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), fieldBuilder.toString(), placeholderBuilder.toString());
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator, keyProperty, keyColumn);
-    }
-}

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

@@ -35,7 +35,8 @@ public class Update extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.UPDATE;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, true, tableInfo, "et."),
+        // TODO 这里有个  前缀判断的问题
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, true, tableInfo, "ew.entity"),
             this.sqlWhereEntityWrapper(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);

+ 0 - 51
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/UpdateAllColumnById.java

@@ -1,51 +0,0 @@
-/*
- * 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.methods;
-
-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;
-
-/**
- * <p>
- * 根据 ID 更新所有字段<br/>
- * 该方法 3.1 删除,建议迁移到 update 使用 UpdateWrapper 处理空字段问题
- * </p>
- *
- * @author hubin
- * @since 2018-04-06
- */
-@Deprecated
-public class UpdateAllColumnById extends AbstractMethod {
-
-    @Override
-    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
-        SqlMethod sqlMethod = SqlMethod.UPDATE_ALL_COLUMN_BY_ID;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(false, false, tableInfo, "et."),
-            tableInfo.getKeyColumn(), "et." + tableInfo.getKeyProperty(),
-            "<if test=\"et instanceof java.util.Map\">"
-            + "<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">"
-            + "  AND ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}"
-            + "</if>"
-            + "</if>"
-        );
-        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
-    }
-}

+ 2 - 24
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

@@ -48,16 +48,6 @@ public interface BaseMapper<T> {
      */
     Integer insert(T entity);
 
-    /**
-     * <p>
-     * 插入一条记录
-     * </p>
-     *
-     * @param entity 实体对象
-     * @return int
-     */
-    Integer insertAllColumn(T entity);
-
     /**
      * <p>
      * 根据 ID 删除
@@ -110,25 +100,13 @@ public interface BaseMapper<T> {
 
     /**
      * <p>
-     * 根据 ID 修改
-     * </p>
-     *
-     * @param entity 实体对象
-     * @return int
-     */
-    @Deprecated
-    Integer updateAllColumnById(@Param("et") T entity);
-
-    /**
-     * <p>
-     * 根据 whereEntity 条件,更新记录
+     * 根据 updateWrapper 条件,更新记录
      * </p>
      *
-     * @param entity        实体对象 (set 条件值)
      * @param updateWrapper 实体对象封装操作类(可以为 null)
      * @return int
      */
-    Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
+    Integer update(@Param("ew") Wrapper<T> updateWrapper);
 
     /**
      * <p>

+ 4 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/LogicSqlInjector.java → mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/LogicSqlInjector.java

@@ -13,7 +13,7 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-package com.baomidou.mybatisplus.core.injector;
+package com.baomidou.mybatisplus.extension.injector;
 
 import java.util.List;
 import java.util.stream.Collectors;
@@ -21,8 +21,10 @@ import java.util.stream.Stream;
 
 import org.apache.ibatis.session.Configuration;
 
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
+import com.baomidou.mybatisplus.core.injector.SqlRunnerInjector;
 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;
@@ -38,7 +40,6 @@ 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;
 
 
@@ -57,14 +58,12 @@ public class LogicSqlInjector extends AbstractSqlInjector {
     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(),

+ 9 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/package-info.java

@@ -0,0 +1,9 @@
+/**
+ * <p>
+ * 扩展注入核心代码
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-13
+ */
+package com.baomidou.mybatisplus.extension.injector;

+ 2 - 71
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/IService.java

@@ -43,16 +43,6 @@ public interface IService<T> {
      */
     boolean insert(T entity);
 
-    /**
-     * <p>
-     * 插入一条记录(全部字段)
-     * </p>
-     *
-     * @param entity 实体对象
-     * @return boolean
-     */
-    boolean insertAllColumn(T entity);
-
     /**
      * <p>
      * 插入(批量),该方法不适合 Oracle
@@ -95,25 +85,6 @@ public interface IService<T> {
      */
     boolean insertOrUpdateBatch(List<T> entityList, int batchSize);
 
-    /**
-     * <p>
-     * 批量修改或插入全部字段
-     * </p>
-     *
-     * @param entityList 实体对象列表
-     * @return boolean
-     */
-    boolean insertOrUpdateAllColumnBatch(List<T> entityList);
-
-    /**
-     * 批量修改或插入全部字段
-     *
-     * @param entityList 实体对象列表
-     * @param batchSize
-     * @return boolean
-     */
-    boolean insertOrUpdateAllColumnBatch(List<T> entityList, int batchSize);
-
     /**
      * <p>
      * 根据 ID 删除
@@ -164,26 +135,15 @@ public interface IService<T> {
      */
     boolean updateById(T entity);
 
-    /**
-     * <p>
-     * 根据 ID 修改全部字段
-     * </p>
-     *
-     * @param entity 实体对象
-     * @return boolean
-     */
-    boolean updateAllColumnById(T entity);
-
     /**
      * <p>
      * 根据 whereEntity 条件,更新记录
      * </p>
      *
-     * @param entity  实体对象
      * @param wrapper 实体包装类 {@link Wrapper}
      * @return boolean
      */
-    boolean update(T entity, Wrapper<T> wrapper);
+    boolean update(Wrapper<T> wrapper);
 
     /**
      * <p>
@@ -206,27 +166,6 @@ public interface IService<T> {
      */
     boolean updateBatchById(List<T> entityList, int batchSize);
 
-    /**
-     * <p>
-     * 根据ID 批量更新全部字段
-     * </p>
-     *
-     * @param entityList 实体对象列表
-     * @return boolean
-     */
-    boolean updateAllColumnBatchById(List<T> entityList);
-
-    /**
-     * <p>
-     * 根据ID 批量更新全部字段
-     * </p>
-     *
-     * @param entityList 实体对象列表
-     * @param batchSize  更新批次数量
-     * @return boolean
-     */
-    boolean updateAllColumnBatchById(List<T> entityList, int batchSize);
-
     /**
      * <p>
      * TableId 注解存在更新记录,否插入一条记录
@@ -237,14 +176,6 @@ public interface IService<T> {
      */
     boolean insertOrUpdate(T entity);
 
-    /**
-     * 插入或修改一条记录的全部字段
-     *
-     * @param entity 实体对象
-     * @return boolean
-     */
-    boolean insertOrUpdateAllColumn(T entity);
-
     /**
      * <p>
      * 根据 ID 查询
@@ -291,7 +222,7 @@ public interface IService<T> {
      * </p>
      *
      * @param wrapper {@link Wrapper}
-     * @return Map<String   ,   Object>
+     * @return Map<String       ,       Object>
      */
     Map<String, Object> selectMap(Wrapper<T> wrapper);
 

+ 4 - 76
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -97,12 +97,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return retBool(baseMapper.insert(entity));
     }
 
-    @Transactional(rollbackFor = Exception.class)
-    @Override
-    public boolean insertAllColumn(T entity) {
-        return retBool(baseMapper.insertAllColumn(entity));
-    }
-
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean insertBatch(List<T> entityList) {
@@ -169,29 +163,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return false;
     }
 
-    @Transactional(rollbackFor = Exception.class)
-    @Override
-    public boolean insertOrUpdateAllColumn(T entity) {
-        if (null != entity) {
-            Class<?> cls = entity.getClass();
-            TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
-            if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
-                Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
-                if (StringUtils.checkValNull(idVal)) {
-                    return insertAllColumn(entity);
-                } else {
-                    /*
-                     * 更新成功直接返回,失败执行插入逻辑
-                     */
-                    return updateAllColumnById(entity) || insertAllColumn(entity);
-                }
-            } else {
-                throw new MybatisPlusException("Error:  Can not execute. Could not find @TableId.");
-            }
-        }
-        return false;
-    }
-
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean insertOrUpdateBatch(List<T> entityList) {
@@ -204,18 +175,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return insertOrUpdateBatch(entityList, batchSize, true);
     }
 
-    @Transactional(rollbackFor = Exception.class)
-    @Override
-    public boolean insertOrUpdateAllColumnBatch(List<T> entityList) {
-        return insertOrUpdateBatch(entityList, 30, false);
-    }
-
-    @Transactional(rollbackFor = Exception.class)
-    @Override
-    public boolean insertOrUpdateAllColumnBatch(List<T> entityList, int batchSize) {
-        return insertOrUpdateBatch(entityList, batchSize, false);
-    }
-
     /**
      * 批量插入修改
      *
@@ -234,7 +193,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                 if (selective) {
                     insertOrUpdate(entityList.get(i));
                 } else {
-                    insertOrUpdateAllColumn(entityList.get(i));
+                    //insertOrUpdateAllColumn(entityList.get(i));
                 }
                 if (i >= 1 && i % batchSize == 0) {
                     batchSqlSession.flushStatements();
@@ -282,14 +241,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public boolean updateAllColumnById(T entity) {
-        return retBool(baseMapper.updateAllColumnById(entity));
-    }
-
-    @Transactional(rollbackFor = Exception.class)
-    @Override
-    public boolean update(T entity, Wrapper<T> wrapper) {
-        return retBool(baseMapper.update(entity, wrapper));
+    public boolean update(Wrapper<T> wrapper) {
+        return retBool(baseMapper.update(wrapper));
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -301,37 +254,12 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean updateBatchById(List<T> entityList, int batchSize) {
-        return updateBatchById(entityList, batchSize, true);
-    }
-
-    @Transactional(rollbackFor = Exception.class)
-    @Override
-    public boolean updateAllColumnBatchById(List<T> entityList) {
-        return updateAllColumnBatchById(entityList, 30);
-    }
-
-    @Transactional(rollbackFor = Exception.class)
-    @Override
-    public boolean updateAllColumnBatchById(List<T> entityList, int batchSize) {
-        return updateBatchById(entityList, batchSize, false);
-    }
-
-    /**
-     * 根据主键ID进行批量修改
-     *
-     * @param entityList 实体对象列表
-     * @param batchSize  批量刷新个数
-     * @param selective  是否滤掉空字段
-     * @return boolean
-     */
-    private boolean updateBatchById(List<T> entityList, int batchSize, boolean selective) {
         if (CollectionUtils.isEmpty(entityList)) {
             throw new IllegalArgumentException("Error: entityList must not be empty");
         }
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
             int size = entityList.size();
-            SqlMethod sqlMethod = selective ? SqlMethod.UPDATE_BY_ID : SqlMethod.UPDATE_ALL_COLUMN_BY_ID;
-            String sqlStatement = sqlStatement(sqlMethod);
+            String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);
             for (int i = 0; i < size; i++) {
                 MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
                 param.put("et", entityList.get(i));