Przeglądaj źródła

减少子类注入,修复单元测试.

聂秋秋 6 lat temu
rodzic
commit
3ecf0edfae

+ 0 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractSqlInjector.java

@@ -95,10 +95,6 @@ public abstract class AbstractSqlInjector implements ISqlInjector {
                 break;
             }
         }
-        Class<?>[] interfaces = mapperClass.getInterfaces();
-        if (target == null && interfaces != null && interfaces.length > 0) {
-            return extractModelClass(interfaces[0]);
-        }
         return target == null ? null : (Class<?>) target.getActualTypeArguments()[0];
     }
 }

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserMapperTest.java

@@ -154,8 +154,8 @@ class H2UserMapperTest extends BaseTest {
         // 测试自定义注入方法
         h2User.setDesc("");
         h2User.setTestDate(new Date());
-        Assertions.assertTrue(userMapper.updateAllColumnById(h2User) > 0);
-        Assertions.assertTrue("".equals(userMapper.selectById(h2User.getTestId()).getDesc()));
+        Assertions.assertTrue(userMapper.alwaysUpdateSomeColumnById(h2User) > 0);
+        Assertions.assertEquals("", userMapper.selectById(h2User.getTestId()).getDesc());
     }
 
     @Test

+ 108 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/MybatisMapperRegistryTest.java

@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2011-2019, 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>
+ * https://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.test.h2;
+
+import com.baomidou.mybatisplus.core.MybatisMapperRegistry;
+import com.baomidou.mybatisplus.core.override.MybatisMapperMethod;
+import com.baomidou.mybatisplus.core.override.MybatisMapperProxyFactory;
+import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
+import com.baomidou.mybatisplus.test.h2.config.DBConfig;
+import com.baomidou.mybatisplus.test.h2.config.MybatisPlusConfig;
+import com.baomidou.mybatisplus.test.h2.mapper.H2StudentMapper;
+import com.baomidou.mybatisplus.test.h2.mapper.H2UserMapper;
+import org.apache.ibatis.session.Configuration;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import javax.sql.DataSource;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+/**
+ * @author nieqiurong 2019/4/12.
+ */
+@ExtendWith(SpringExtension.class)
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+@MapperScan(value = "com.baomidou.mybatisplus.test.h2")
+@ContextConfiguration(classes = {MybatisMapperRegistryTest.class, DBConfig.class})
+class MybatisMapperRegistryTest extends BaseTest {
+    
+    private interface H2StudentChildrenMapper extends H2StudentMapper {
+    
+    }
+    
+    @Bean
+    DBConfig dbConfig() {
+        return new DBConfig();
+    }
+    
+    @Bean
+    MybatisPlusConfig mybatisPlusConfig() {
+        return new MybatisPlusConfig();
+    }
+    
+    @Bean
+    SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
+        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
+        sqlSessionFactory.setDataSource(dataSource);
+        return sqlSessionFactory.getObject();
+    }
+    
+    @Autowired
+    private SqlSessionFactory sqlSessionFactory;
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    void test() throws ReflectiveOperationException {
+        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
+            Configuration configuration = sqlSessionFactory.getConfiguration();
+            MybatisMapperRegistry mapperRegistry = (MybatisMapperRegistry) sqlSessionFactory.getConfiguration().getMapperRegistry();
+            Assertions.assertTrue(mapperRegistry.hasMapper(H2UserMapper.class));
+            Assertions.assertTrue(mapperRegistry.hasMapper(H2StudentChildrenMapper.class));
+            H2StudentMapper studentMapper = mapperRegistry.getMapper(H2StudentMapper.class, sqlSession);
+            
+            Assertions.assertTrue(configuration.hasStatement(H2StudentMapper.class.getName() + ".selectById"));
+            studentMapper.selectById(1);
+            
+            Field field = mapperRegistry.getClass().getDeclaredField("knownMappers");
+            field.setAccessible(true);
+            Map<Class<?>, MybatisMapperProxyFactory<?>> knownMappers = (Map<Class<?>, MybatisMapperProxyFactory<?>>) field.get(mapperRegistry);
+            MybatisMapperProxyFactory<?> mybatisMapperProxyFactory = knownMappers.get(H2StudentChildrenMapper.class);
+            
+            
+            H2StudentChildrenMapper h2StudentChildrenMapper = mapperRegistry.getMapper(H2StudentChildrenMapper.class, sqlSession);
+            Assertions.assertFalse(configuration.hasStatement(H2StudentChildrenMapper.class.getName() + ".selectById"));
+            Map<Method, MybatisMapperMethod> methodCache = mybatisMapperProxyFactory.getMethodCache();
+            Assertions.assertTrue(methodCache.isEmpty());
+            
+            h2StudentChildrenMapper.selectById(2);
+            methodCache = mybatisMapperProxyFactory.getMethodCache();
+            Assertions.assertFalse(methodCache.isEmpty());
+        }
+    }
+}

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/mapper/SuperMapper.java

@@ -31,7 +31,7 @@ public interface SuperMapper<T> extends com.baomidou.mybatisplus.core.mapper.Bas
      * 这里注入自定义的公共方法
      */
 
-    int updateAllColumnById(@Param("et") T entity);
+    int alwaysUpdateSomeColumnById(@Param("et") T entity);
 
     int deleteByIdWithFill(T entity);