浏览代码

隔离 spring 框架强依赖

hubin 3 年之前
父节点
当前提交
6c3bf64518

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

@@ -20,6 +20,8 @@ import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils;
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.security.AccessController;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
@@ -95,7 +97,7 @@ public final class ReflectionKit {
      */
     public static Class<?> getSuperClassGenericType(final Class<?> clazz, final Class<?> genericIfc, final int index) {
         //update by noear @2021-09-03
-        Class<?>[] typeArguments = GenericTypeUtils.getGenericTypeResolver().resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc);
+        Class<?>[] typeArguments = GenericTypeUtils.resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc);
         return null == typeArguments ? null : typeArguments[index];
     }
 

+ 6 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/reflect/GenericTypeUtils.java

@@ -18,7 +18,8 @@ package com.baomidou.mybatisplus.core.toolkit.reflect;
 /**
  * 泛型类工具(用于隔离Spring的代码)
  *
- * @author noear hubin
+ * @author noear
+ * @author hubin
  * @since 2021-09-03
  */
 public class GenericTypeUtils {
@@ -27,11 +28,12 @@ public class GenericTypeUtils {
     /**
      * 获取泛型工具助手
      */
-    public static IGenericTypeResolver getGenericTypeResolver() {
+    public static Class<?>[] resolveTypeArguments(final Class<?> clazz, final Class<?> genericIfc) {
         if (null == GENERIC_TYPE_RESOLVER) {
-            GENERIC_TYPE_RESOLVER = new SpringGenericTypeResolver();
+            // 直接使用 spring 静态方法,减少对象创建
+            return SpringReflectionHelper.resolveTypeArguments(clazz, genericIfc);
         }
-        return GENERIC_TYPE_RESOLVER;
+        return GENERIC_TYPE_RESOLVER.resolveTypeArguments(clazz, genericIfc);
     }
 
     /**

+ 2 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/reflect/IGenericTypeResolver.java

@@ -18,7 +18,8 @@ package com.baomidou.mybatisplus.core.toolkit.reflect;
 /**
  * 泛型类助手(用于隔离Spring的代码)
  *
- * @author noear hubin
+ * @author noear
+ * @author hubin
  * @since 2021-09-03
  */
 public interface IGenericTypeResolver {

+ 5 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/reflect/SpringGenericTypeResolver.java → mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/reflect/SpringReflectionHelper.java

@@ -18,15 +18,15 @@ package com.baomidou.mybatisplus.core.toolkit.reflect;
 import org.springframework.core.GenericTypeResolver;
 
 /**
- * 泛型类助手默认 spring 实现
+ * Spring 反射辅助类
  *
- * @author noear hubin
+ * @author noear
+ * @author hubin
  * @since 2021-09-03
  */
-public class SpringGenericTypeResolver implements IGenericTypeResolver {
+public class SpringReflectionHelper {
 
-    @Override
-    public Class<?>[] resolveTypeArguments(Class<?> clazz, Class<?> genericIfc) {
+    public static Class<?>[] resolveTypeArguments(Class<?> clazz, Class<?> genericIfc) {
         return GenericTypeResolver.resolveTypeArguments(clazz, genericIfc);
     }
 }

+ 15 - 4
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/conditions/LambdaQueryWrapperTest.java

@@ -3,8 +3,14 @@ package com.baomidou.mybatisplus.core.conditions;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import lombok.Data;
+import org.apache.ibatis.builder.MapperBuilderAssistant;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -15,13 +21,19 @@ class LambdaQueryWrapperTest extends BaseWrapperTest {
 
     @Test
     void testLambdaOrderBySqlSegment() {
-        Wrappers.<Table>lambdaQuery()
-            .orderByDesc(Table::getId);
+        TableInfo tableInfo = TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), Table.class);
+        Assertions.assertEquals("xxx", tableInfo.getTableName());
+        LambdaQueryWrapper lqw = Wrappers.<Table>lambdaQuery().orderByDesc(Table::getId);
+        Assertions.assertEquals(lqw.getSqlSegment(), " ORDER BY `id` DESC");
+        lqw.clear();
+        Assertions.assertEquals(lqw.getSqlSegment(), "");
+        lqw = Wrappers.<Table>lambdaQuery().eq(Table::getId, 1).nested(false, x -> x.eq(Table::getName, "李白"));
+        Assertions.assertEquals(lqw.getSqlSegment(), "(`id` = #{ew.paramNameValuePairs.MPGENVAL1})");
     }
 
 
     @Data
-    @TableName( "xxx")
+    @TableName("xxx")
     private static class Table {
 
         @TableId("`id`")
@@ -30,5 +42,4 @@ class LambdaQueryWrapperTest extends BaseWrapperTest {
         @TableField("`name`")
         private Long name;
     }
-
 }