Kaynağa Gözat

自定义mybatis中的 mapperScanner接口

zhang 1 yıl önce
ebeveyn
işleme
5c57d41413

+ 3 - 1
mybatis_train/src/main/java/com/zhouyuTest/Test.java

@@ -10,7 +10,9 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
10 10
 
11 11
 public class Test {
12 12
     public static void main(String[] args) {
13
-        AnnotationConfigApplicationContext an = new AnnotationConfigApplicationContext(MyConfig.class);
13
+        AnnotationConfigApplicationContext an = new AnnotationConfigApplicationContext();
14
+        an.register(MyConfig.class);
15
+        an.refresh();
14 16
         UserService bean = an.getBean(UserService.class);
15 17
         bean.test();
16 18
     }

+ 2 - 1
mybatis_train/src/main/java/com/zhouyuTest/config/MyConfig.java

@@ -1,6 +1,7 @@
1 1
 package com.zhouyuTest.config;
2 2
 
3 3
 import com.zhouyuTest.spring_mybatis.MyImportBeanDefinitionRegistrar;
4
+import com.zhouyuTest.spring_mybatis.MyMapperScan;
4 5
 import org.apache.ibatis.io.Resources;
5 6
 import org.apache.ibatis.session.SqlSessionFactory;
6 7
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
@@ -14,7 +15,7 @@ import java.io.InputStream;
14 15
 
15 16
 @Configuration
16 17
 @ComponentScan({"com.zhouyuTest"})
17
-@Import(MyImportBeanDefinitionRegistrar.class)
18
+@MyMapperScan("com.zhouyuTest.mapper")
18 19
 public class MyConfig {
19 20
     @Bean
20 21
     public SqlSessionFactory getFactory() throws IOException {

+ 8 - 0
mybatis_train/src/main/java/com/zhouyuTest/mapper/ManagerMapper.java

@@ -0,0 +1,8 @@
1
+package com.zhouyuTest.mapper;
2
+
3
+import org.apache.ibatis.annotations.Select;
4
+
5
+public interface ManagerMapper {
6
+    @Select("select 'managerOrder'")
7
+    public String getManager();
8
+}

+ 30 - 0
mybatis_train/src/main/java/com/zhouyuTest/spring_mybatis/MapperScanner.java

@@ -0,0 +1,30 @@
1
+package com.zhouyuTest.spring_mybatis;
2
+
3
+import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
4
+import org.springframework.beans.factory.config.BeanDefinition;
5
+import org.springframework.beans.factory.config.BeanDefinitionHolder;
6
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
7
+import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
8
+
9
+import java.util.Set;
10
+
11
+public class MapperScanner extends ClassPathBeanDefinitionScanner {
12
+    public MapperScanner(BeanDefinitionRegistry registry) {
13
+        super(registry);
14
+    }
15
+
16
+    @Override
17
+    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
18
+        return beanDefinition.getMetadata().isInterface();
19
+    }
20
+    @Override
21
+    protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
22
+        Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(basePackages);
23
+        for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
24
+            BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
25
+            beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition.getBeanClassName());
26
+            beanDefinition.setBeanClassName(MybatisFactoryBean.class.getName());
27
+        }
28
+        return beanDefinitionHolders;
29
+    }
30
+}

+ 24 - 8
mybatis_train/src/main/java/com/zhouyuTest/spring_mybatis/MyImportBeanDefinitionRegistrar.java

@@ -8,17 +8,33 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
8 8
 import org.springframework.beans.factory.support.BeanNameGenerator;
9 9
 import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
10 10
 import org.springframework.core.type.AnnotationMetadata;
11
+import org.springframework.core.type.classreading.MetadataReader;
12
+import org.springframework.core.type.classreading.MetadataReaderFactory;
13
+import org.springframework.core.type.filter.TypeFilter;
14
+
15
+import java.io.IOException;
16
+import java.util.Map;
11 17
 
12 18
 public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
13 19
     @Override
14 20
     public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
15
-        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
16
-        beanDefinition.setBeanClass(MybatisFactoryBean.class);
17
-        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserMapper.class);
18
-        registry.registerBeanDefinition("userMapper",beanDefinition);
19
-        AbstractBeanDefinition beanDefinition2 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
20
-        beanDefinition2.setBeanClass(MybatisFactoryBean.class);
21
-        beanDefinition2.getConstructorArgumentValues().addGenericArgumentValue(OrderMapper.class);
22
-        registry.registerBeanDefinition("orderMapper",beanDefinition2);
21
+        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(MyMapperScan.class.getName());
22
+        String path = ((String) annotationAttributes.get("value"));
23
+        MapperScanner mapperScanner = new MapperScanner(registry);
24
+        mapperScanner.addIncludeFilter(new TypeFilter() {
25
+            @Override
26
+            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
27
+                return true;
28
+            }
29
+        });
30
+        mapperScanner.scan(path);
31
+//        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
32
+//        beanDefinition.setBeanClass(MybatisFactoryBean.class);
33
+//        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserMapper.class);
34
+//        registry.registerBeanDefinition("userMapper",beanDefinition);
35
+//        AbstractBeanDefinition beanDefinition2 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
36
+//        beanDefinition2.setBeanClass(MybatisFactoryBean.class);
37
+//        beanDefinition2.getConstructorArgumentValues().addGenericArgumentValue(OrderMapper.class);
38
+//        registry.registerBeanDefinition("orderMapper",beanDefinition2);
23 39
     }
24 40
 }

+ 15 - 0
mybatis_train/src/main/java/com/zhouyuTest/spring_mybatis/MyMapperScan.java

@@ -0,0 +1,15 @@
1
+package com.zhouyuTest.spring_mybatis;
2
+
3
+import org.springframework.context.annotation.Import;
4
+
5
+import java.lang.annotation.ElementType;
6
+import java.lang.annotation.Retention;
7
+import java.lang.annotation.RetentionPolicy;
8
+import java.lang.annotation.Target;
9
+
10
+@Target(ElementType.TYPE)
11
+@Retention(value = RetentionPolicy.RUNTIME)
12
+@Import(MyImportBeanDefinitionRegistrar.class)
13
+public @interface MyMapperScan {
14
+    String value() default "";
15
+}