Explorar el Código

简化 api 层命名及初始值规则

hubin hace 6 años
padre
commit
7bc25212e4

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

@@ -15,164 +15,17 @@
  */
 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.ObjectUtils;
-import com.baomidou.mybatisplus.extension.exceptions.ApiException;
-
 /**
  * <p>
  * REST API 业务断言<br>
  * 参考:org.junit.Assert
+ * 简化命名为 com.baomidou.mybatisplus.extension.api.Assert
  * </p>
  *
  * @author hubin
  * @since 2018-06-05
  */
-public class ApiAssert {
-
-    protected ApiAssert() {
-        // to do noting
-    }
-
-    /**
-     * 大于O
-     */
-    public static void gtZero(Integer num, IErrorCode errorCode) {
-        if (num == null || num <= 0) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    /**
-     * 大于等于O
-     */
-    public static void geZero(Integer num, IErrorCode errorCode) {
-        if (num == null || num < 0) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    /**
-     * num1大于num2
-     */
-    public static void gt(Integer num1, Integer num2, IErrorCode errorCode) {
-        if (num1 <= num2) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    /**
-     * num1大于等于num2
-     */
-    public static void ge(Integer num1, Integer num2, IErrorCode errorCode) {
-        if (num1 < num2) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    /**
-     * obj1 eq obj2
-     */
-    public static void eq(Object obj1, Object obj2, IErrorCode errorCode) {
-        if (!obj1.equals(obj2)) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void isTrue(boolean condition, IErrorCode errorCode) {
-        if (!condition) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void isFalse(boolean condition, IErrorCode errorCode) {
-        if (condition) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void isNull(IErrorCode errorCode, Object... conditions) {
-        if (ObjectUtils.isNotNull(conditions)) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void notNull(IErrorCode errorCode, Object... conditions) {
-        if (ObjectUtils.isNull(conditions)) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    /**
-     * <p>
-     * 失败结果
-     * </p>
-     *
-     * @param errorCode 异常错误码
-     */
-    public static void fail(IErrorCode errorCode) {
-        throw new ApiException(errorCode);
-    }
-
-    public static void fail(boolean condition, IErrorCode errorCode) {
-        if (condition) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void fail(String message) {
-        throw new ApiException(message);
-    }
-
-    public static void fail(boolean condition, String message) {
-        if (condition) {
-            ApiAssert.fail(message);
-        }
-    }
-
-    public static void notEmpty(Object[] array, IErrorCode errorCode) {
-        if (ObjectUtils.isEmpty(array)) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void noNullElements(Object[] array, IErrorCode errorCode) {
-        if (array != null) {
-            for (Object element : array) {
-                if (element == null) {
-                    ApiAssert.fail(errorCode);
-                }
-            }
-        }
-    }
-
-    public static void notEmpty(Collection<?> collection, IErrorCode errorCode) {
-        if (CollectionUtils.isNotEmpty(collection)) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void notEmpty(Map<?, ?> map, IErrorCode errorCode) {
-        if (ObjectUtils.isEmpty(map)) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void isInstanceOf(Class<?> type, Object obj, IErrorCode errorCode) {
-        ApiAssert.notNull(errorCode, type);
-        if (!type.isInstance(obj)) {
-            ApiAssert.fail(errorCode);
-        }
-    }
-
-    public static void isAssignable(Class<?> superType, Class<?> subType, IErrorCode errorCode) {
-        ApiAssert.notNull(errorCode, superType);
-        if (subType == null || !superType.isAssignableFrom(subType)) {
-            ApiAssert.fail(errorCode);
-        }
-    }
+@Deprecated
+public class ApiAssert extends Assert {
 
 }

+ 6 - 6
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/api/ApiController.java

@@ -40,8 +40,8 @@ public class ApiController {
      * @param <T>  对象泛型
      * @return
      */
-    protected <T> ApiResult<T> success(T data) {
-        return ApiResult.ok(data);
+    protected <T> R<T> success(T data) {
+        return R.ok(data);
     }
 
     /**
@@ -52,8 +52,8 @@ public class ApiController {
      * @param msg 提示内容
      * @return
      */
-    protected ApiResult<Object> failed(String msg) {
-        return ApiResult.failed(msg);
+    protected R<Object> failed(String msg) {
+        return R.failed(msg);
     }
 
     /**
@@ -64,8 +64,8 @@ public class ApiController {
      * @param errorCode 请求错误码
      * @return
      */
-    protected ApiResult<Object> failed(IErrorCode errorCode) {
-        return ApiResult.failed(errorCode);
+    protected R<Object> failed(IErrorCode errorCode) {
+        return R.failed(errorCode);
     }
 
 }

+ 3 - 70
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/api/ApiResult.java

@@ -15,82 +15,15 @@
  */
 package com.baomidou.mybatisplus.extension.api;
 
-import java.util.Optional;
-
-import com.baomidou.mybatisplus.extension.enums.ApiErrorCode;
-import com.baomidou.mybatisplus.extension.exceptions.ApiException;
-
-import lombok.Data;
-
 /**
  * <p>
- * REST API 返回结果
+ * REST API 返回结果 , 简化为类 com.baomidou.mybatisplus.extension.api.R
  * </p>
  *
  * @author hubin
  * @since 2018-06-05
  */
-@Data
-public class ApiResult<T> {
-
-    /**
-     * 业务错误码
-     */
-    private String code;
-    /**
-     * 结果集
-     */
-    private T data;
-    /**
-     * 描述
-     */
-    private String msg;
-
-    public ApiResult() {
-        // to do nothing
-    }
-
-    public ApiResult(IErrorCode errorCode) {
-        errorCode = Optional.ofNullable(errorCode).orElse(ApiErrorCode.FAILED);
-        this.code = errorCode.getCode();
-        this.msg = errorCode.getMsg();
-    }
-
-    public static <T> ApiResult<T> ok(T data) {
-        return restResult(data, ApiErrorCode.SUCCESS);
-    }
-
-    public static <T> ApiResult<T> failed(String msg) {
-        return restResult(null, ApiErrorCode.FAILED.getCode(), msg);
-    }
-
-    public static <T> ApiResult<T> failed(IErrorCode errorCode) {
-        return restResult(null, errorCode);
-    }
-
-    public static <T> ApiResult<T> restResult(T data, IErrorCode errorCode) {
-        return restResult(data, errorCode.getCode(), errorCode.getMsg());
-    }
-
-    private static <T> ApiResult<T> restResult(T data, String code, String msg) {
-        ApiResult<T> apiResult = new ApiResult<>();
-        apiResult.setCode(code);
-        apiResult.setData(data);
-        apiResult.setMsg(msg);
-        return apiResult;
-    }
-
-    public boolean ok() {
-        return ApiErrorCode.SUCCESS.getCode().equals(this.code);
-    }
+@Deprecated
+public class ApiResult<T> extends R<T> {
 
-    /**
-     * 服务间调用非业务正常,异常直接释放
-     */
-    public T serviceData() {
-        if (!ok()) {
-            throw new ApiException(this.msg);
-        }
-        return data;
-    }
 }

+ 178 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/api/Assert.java

@@ -0,0 +1,178 @@
+/*
+ * 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.api;
+
+import java.util.Collection;
+import java.util.Map;
+
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.baomidou.mybatisplus.extension.exceptions.ApiException;
+
+/**
+ * <p>
+ * REST API 业务断言<br>
+ * 参考:org.junit.Assert
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-05
+ */
+public class Assert {
+
+    protected Assert() {
+        // to do noting
+    }
+
+    /**
+     * 大于O
+     */
+    public static void gtZero(Integer num, IErrorCode errorCode) {
+        if (num == null || num <= 0) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    /**
+     * 大于等于O
+     */
+    public static void geZero(Integer num, IErrorCode errorCode) {
+        if (num == null || num < 0) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    /**
+     * num1大于num2
+     */
+    public static void gt(Integer num1, Integer num2, IErrorCode errorCode) {
+        if (num1 <= num2) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    /**
+     * num1大于等于num2
+     */
+    public static void ge(Integer num1, Integer num2, IErrorCode errorCode) {
+        if (num1 < num2) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    /**
+     * obj1 eq obj2
+     */
+    public static void eq(Object obj1, Object obj2, IErrorCode errorCode) {
+        if (!obj1.equals(obj2)) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void isTrue(boolean condition, IErrorCode errorCode) {
+        if (!condition) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void isFalse(boolean condition, IErrorCode errorCode) {
+        if (condition) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void isNull(IErrorCode errorCode, Object... conditions) {
+        if (ObjectUtils.isNotNull(conditions)) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void notNull(IErrorCode errorCode, Object... conditions) {
+        if (ObjectUtils.isNull(conditions)) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    /**
+     * <p>
+     * 失败结果
+     * </p>
+     *
+     * @param errorCode 异常错误码
+     */
+    public static void fail(IErrorCode errorCode) {
+        throw new ApiException(errorCode);
+    }
+
+    public static void fail(boolean condition, IErrorCode errorCode) {
+        if (condition) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void fail(String message) {
+        throw new ApiException(message);
+    }
+
+    public static void fail(boolean condition, String message) {
+        if (condition) {
+            Assert.fail(message);
+        }
+    }
+
+    public static void notEmpty(Object[] array, IErrorCode errorCode) {
+        if (ObjectUtils.isEmpty(array)) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void noNullElements(Object[] array, IErrorCode errorCode) {
+        if (array != null) {
+            for (Object element : array) {
+                if (element == null) {
+                    Assert.fail(errorCode);
+                }
+            }
+        }
+    }
+
+    public static void notEmpty(Collection<?> collection, IErrorCode errorCode) {
+        if (CollectionUtils.isNotEmpty(collection)) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void notEmpty(Map<?, ?> map, IErrorCode errorCode) {
+        if (ObjectUtils.isEmpty(map)) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void isInstanceOf(Class<?> type, Object obj, IErrorCode errorCode) {
+        Assert.notNull(errorCode, type);
+        if (!type.isInstance(obj)) {
+            Assert.fail(errorCode);
+        }
+    }
+
+    public static void isAssignable(Class<?> superType, Class<?> subType, IErrorCode errorCode) {
+        Assert.notNull(errorCode, superType);
+        if (subType == null || !superType.isAssignableFrom(subType)) {
+            Assert.fail(errorCode);
+        }
+    }
+
+}

+ 96 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/api/R.java

@@ -0,0 +1,96 @@
+/*
+ * 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.api;
+
+import java.util.Optional;
+
+import com.baomidou.mybatisplus.extension.enums.ApiErrorCode;
+import com.baomidou.mybatisplus.extension.exceptions.ApiException;
+
+import lombok.Data;
+
+/**
+ * <p>
+ * REST API 返回结果
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-05
+ */
+@Data
+public class R<T> {
+
+    /**
+     * 业务错误码
+     */
+    private String code;
+    /**
+     * 结果集
+     */
+    private T data;
+    /**
+     * 描述
+     */
+    private String msg;
+
+    public R() {
+        // to do nothing
+    }
+
+    public R(IErrorCode errorCode) {
+        errorCode = Optional.ofNullable(errorCode).orElse(ApiErrorCode.FAILED);
+        this.code = errorCode.getCode();
+        this.msg = errorCode.getMsg();
+    }
+
+    public static <T> R<T> ok(T data) {
+        return restResult(data, ApiErrorCode.SUCCESS);
+    }
+
+    public static <T> R<T> failed(String msg) {
+        return restResult(null, ApiErrorCode.FAILED.getCode(), msg);
+    }
+
+    public static <T> R<T> failed(IErrorCode errorCode) {
+        return restResult(null, errorCode);
+    }
+
+    public static <T> R<T> restResult(T data, IErrorCode errorCode) {
+        return restResult(data, errorCode.getCode(), errorCode.getMsg());
+    }
+
+    private static <T> R<T> restResult(T data, String code, String msg) {
+        R<T> apiResult = new R<>();
+        apiResult.setCode(code);
+        apiResult.setData(data);
+        apiResult.setMsg(msg);
+        return apiResult;
+    }
+
+    public boolean ok() {
+        return ApiErrorCode.SUCCESS.getCode().equals(this.code);
+    }
+
+    /**
+     * 服务间调用非业务正常,异常直接释放
+     */
+    public T serviceData() {
+        if (!ok()) {
+            throw new ApiException(this.msg);
+        }
+        return data;
+    }
+}

+ 2 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/enums/ApiErrorCode.java

@@ -29,11 +29,11 @@ public enum ApiErrorCode implements IErrorCode {
     /**
      * 失败
      */
-    FAILED("0", "失败"),
+    FAILED("-1", "失败"),
     /**
      * 成功
      */
-    SUCCESS("1", "成功");
+    SUCCESS("0", "成功");
 
     private final String code;
     private final String msg;