Ver Fonte

Merge branch '3.0' of https://gitee.com/baomidou/mybatis-plus into 3.0

willenfoo há 7 anos atrás
pai
commit
f54dce9281

+ 2 - 2
mybatis-plus-adapter/src/main/java/com/baomidou/mybatisplus/mapper/Wrapper.java

@@ -27,10 +27,10 @@ import com.baomidou.mybatisplus.core.enums.SqlLike;
 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.baomidou.mybatisplus.core.toolkit.MapUtils;
 import com.baomidou.mybatisplus.core.toolkit.SerializationUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
+import com.baomidou.mybatisplus.extension.toolkit.ObjectUtils;
 
 
 /**
@@ -341,7 +341,7 @@ public class Wrapper<T> extends com.baomidou.mybatisplus.core.conditions.Wrapper
      */
     @SuppressWarnings({"rawtypes", "unchecked"})
     public Wrapper<T> allEq(boolean condition, Map<String, Object> params) {
-        if (condition && MapUtils.isNotEmpty(params)) {
+        if (condition && ObjectUtils.isNotEmpty(params)) {
             Iterator iterator = params.entrySet().iterator();
             while (iterator.hasNext()) {
                 Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();

+ 3 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java

@@ -49,7 +49,9 @@ public class MybatisConfiguration extends Configuration {
      */
     public MybatisConfiguration() {
         this.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
-        logger.debug("Mybatis-plus init success.");
+        System.out.println("  _ _     /_ _ _/_. ____  /    _");
+        System.out.println(" / / //_//_//_|/ /_\\  /_///_/_\\ ");
+        System.out.println("      _/             /             init success.");
     }
 
     /**

+ 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字段

+ 47 - 17
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

@@ -25,6 +25,53 @@ import org.apache.ibatis.annotations.Param;
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 
+/*
+
+               :`
+                    .:,
+                     :::,,.
+             ::      `::::::
+             ::`    `,:,` .:`
+             `:: `::::::::.:`      `:';,`
+              ::::,     .:::`   `@++++++++:
+               ``        :::`  @+++++++++++#
+                         :::, #++++++++++++++`
+                 ,:      `::::::;'##++++++++++
+                 .@#@;`   ::::::::::::::::::::;
+                  #@####@, :::::::::::::::+#;::.
+                  @@######+@:::::::::::::.  #@:;
+           ,      @@########':::::::::::: .#''':`
+           ;##@@@+:##########@::::::::::: @#;.,:.
+            #@@@######++++#####'::::::::: .##+,:#`
+            @@@@@#####+++++'#####+::::::::` ,`::@#:`
+            `@@@@#####++++++'#####+#':::::::::::@.
+             @@@@######+++++''#######+##';::::;':,`
+              @@@@#####+++++'''#######++++++++++`
+               #@@#####++++++''########++++++++'
+               `#@######+++++''+########+++++++;
+                `@@#####+++++''##########++++++,
+                 @@######+++++'##########+++++#`
+                @@@@#####+++++############++++;
+              ;#@@@@@####++++##############+++,
+             @@@@@@@@@@@###@###############++'
+           @#@@@@@@@@@@@@###################+:
+        `@#@@@@@@@@@@@@@@###################'`
+      :@#@@@@@@@@@@@@@@@@@##################,
+      ,@@@@@@@@@@@@@@@@@@@@################;
+       ,#@@@@@@@@@@@@@@@@@@@##############+`
+        .#@@@@@@@@@@@@@@@@@@#############@,
+          @@@@@@@@@@@@@@@@@@@###########@,
+           :#@@@@@@@@@@@@@@@@##########@,
+            `##@@@@@@@@@@@@@@@########+,
+              `+@@@@@@@@@@@@@@@#####@:`
+                `:@@@@@@@@@@@@@@##@;.
+                   `,'@@@@##@@@+;,`
+                        ``...``
+
+ _ _     /_ _ _/_. ____  /    _
+/ / //_//_//_|/ /_\  /_///_/_\      Talk is cheap. Show me the code.
+     _/             /
+ */
 /**
  * <p>
  * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
@@ -44,7 +91,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param entity 实体对象
-     * @return int
      */
     Integer insert(T entity);
 
@@ -54,7 +100,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param id 主键ID
-     * @return int
      */
     Integer deleteById(Serializable id);
 
@@ -64,7 +109,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param columnMap 表字段 map 对象
-     * @return int
      */
     Integer deleteByMap(@Param("cm") Map<String, Object> columnMap);
 
@@ -74,7 +118,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param queryWrapper 实体对象封装操作类(可以为 null)
-     * @return int
      */
     Integer delete(@Param("ew") Wrapper<T> queryWrapper);
 
@@ -84,7 +127,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param idList 主键ID列表
-     * @return int
      */
     Integer deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
 
@@ -94,7 +136,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param entity 实体对象
-     * @return int
      */
     Integer updateById(@Param("et") T entity);
 
@@ -105,7 +146,6 @@ public interface BaseMapper<T> {
      *
      * @param entity        实体对象 (set 条件值)
      * @param updateWrapper 实体对象封装操作类(可以为 null)
-     * @return int
      */
     Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
 
@@ -115,7 +155,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param id 主键ID
-     * @return T
      */
     T selectById(Serializable id);
 
@@ -125,7 +164,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param idList 主键ID列表
-     * @return List<T>
      */
     List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
 
@@ -135,7 +173,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param columnMap 表字段 map 对象
-     * @return List<T>
      */
     List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
 
@@ -145,7 +182,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param entity 实体对象
-     * @return T
      */
     T selectOne(@Param("ew") T entity);
 
@@ -155,7 +191,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param queryWrapper 实体对象
-     * @return int
      */
     Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
 
@@ -165,7 +200,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param queryWrapper 实体对象封装操作类(可以为 null)
-     * @return List<T>
      */
     List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
 
@@ -175,7 +209,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param queryWrapper 实体对象封装操作类(可以为 null)
-     * @return List<T>
      */
     List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
 
@@ -186,7 +219,6 @@ public interface BaseMapper<T> {
      * </p>
      *
      * @param queryWrapper 实体对象封装操作类(可以为 null)
-     * @return List<Object>
      */
     List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
 
@@ -197,7 +229,6 @@ public interface BaseMapper<T> {
      *
      * @param page         分页查询条件(可以为 RowBounds.DEFAULT)
      * @param queryWrapper 实体对象封装操作类(可以为 null)
-     * @return List<T>
      */
     IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
 
@@ -208,7 +239,6 @@ public interface BaseMapper<T> {
      *
      * @param page         分页查询条件
      * @param queryWrapper 实体对象封装操作类
-     * @return
      */
     List<Map<String, Object>> selectMapsPage(IPage page, @Param("ew") Wrapper<T> queryWrapper);
 

+ 1 - 3
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/api/ApiAssert.java

@@ -18,9 +18,7 @@ package com.baomidou.mybatisplus.extension.api;
 import java.util.Collection;
 import java.util.Map;
 
-
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.baomidou.mybatisplus.core.toolkit.MapUtils;
 import com.baomidou.mybatisplus.extension.exceptions.ApiException;
 import com.baomidou.mybatisplus.extension.toolkit.ObjectUtils;
 
@@ -158,7 +156,7 @@ public class ApiAssert {
     }
 
     public static void notEmpty(Map<?, ?> map, IErrorCode errorCode) {
-        if (MapUtils.isEmpty(map)) {
+        if (ObjectUtils.isEmpty(map)) {
             fail(errorCode);
         }
     }

+ 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();
+    }
+
+}

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

@@ -32,12 +32,12 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.baomidou.mybatisplus.core.toolkit.MapUtils;
 import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlHelper;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.baomidou.mybatisplus.extension.toolkit.ObjectUtils;
 
 /**
  * <p>
@@ -215,7 +215,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean deleteByMap(Map<String, Object> columnMap) {
-        if (MapUtils.isEmpty(columnMap)) {
+        if (ObjectUtils.isEmpty(columnMap)) {
             throw new MybatisPlusException("deleteByMap columnMap is empty.");
         }
         return SqlHelper.delBool(baseMapper.deleteByMap(columnMap));

+ 7 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/ObjectUtils.java

@@ -38,6 +38,13 @@ public class ObjectUtils {
     /**
      * 对象非空判断
      */
+    public static boolean isNotEmpty(Object obj) {
+        return !isEmpty(obj);
+    }
+
+    /**
+     * 对象空判断
+     */
     public static boolean isEmpty(Object obj) {
         if (obj == null) {
             return true;