时间:2022-11-18 10:10:31 | 栏目:JAVA代码 | 点击:次
@MapperScan注解配置的一般是dao或者mapper的扫描包,一般用于数据库操作,里面类的一般都是接口,如果在dao层有其他接口,比如说@Service等就会报错
把service包移走,方法可行
不使用@MapperScan,在每个dao或者mapper里面加上注解@Mapper,方法可行
使用自定义注解,在mybatis的注解比较完善的情况下,就不用自己搞多少
创建注解@MyMapperScan
里面的属性全部抄袭@MapperScan
MapperScannerRegistrar换成自己的
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Import(MyMapperScannerRegistrar.class) @Repeatable(MapperScans.class) public @interface MapperScan { String[] value() default {}; String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; Class<? extends Annotation> annotationClass() default Annotation.class; Class<?> markerInterface() default Class.class; String sqlSessionTemplateRef() default ""; String sqlSessionFactoryRef() default ""; Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class; String lazyInitialization() default ""; }
MyMapperScannerRegistrar扫描注册器
这个类和mybatis的一模一样,唯一的不同就是MyClassPathMapperScanner是自己的扫描
public class MyMapperScannerRegistrar extends MapperScannerRegistrar { private ResourceLoader resourceLoader; /** * {@inheritDoc} */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName())); //这个是自己的 ClassPathMapperScanner scanner = new MyClassPathMapperScanner(registry); // this check is needed in Spring 3.1 if (resourceLoader != null) { scanner.setResourceLoader(resourceLoader); } Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass"); if (!Annotation.class.equals(annotationClass)) { scanner.setAnnotationClass(annotationClass); } Class<?> markerInterface = annoAttrs.getClass("markerInterface"); if (!Class.class.equals(markerInterface)) { scanner.setMarkerInterface(markerInterface); } Class<? extends BeanNameGenerator> generatorClass = annoAttrs.getClass("nameGenerator"); if (!BeanNameGenerator.class.equals(generatorClass)) { scanner.setBeanNameGenerator(BeanUtils.instantiateClass(generatorClass)); } Class<? extends MapperFactoryBean> mapperFactoryBeanClass = annoAttrs.getClass("factoryBean"); if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) { scanner.setMapperFactoryBean(BeanUtils.instantiateClass(mapperFactoryBeanClass)); } scanner.setSqlSessionTemplateBeanName(annoAttrs.getString("sqlSessionTemplateRef")); scanner.setSqlSessionFactoryBeanName(annoAttrs.getString("sqlSessionFactoryRef")); List<String> basePackages = new ArrayList<String>(); for (String pkg : annoAttrs.getStringArray("value")) { if (StringUtils.hasText(pkg)) { basePackages.add(pkg); } } for (String pkg : annoAttrs.getStringArray("basePackages")) { if (StringUtils.hasText(pkg)) { basePackages.add(pkg); } } for (Class<?> clazz : annoAttrs.getClassArray("basePackageClasses")) { basePackages.add(ClassUtils.getPackageName(clazz)); } scanner.registerFilters(); scanner.doScan(StringUtils.toStringArray(basePackages)); } /** * {@inheritDoc} */ @Override public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } }
MyClassPathMapperScanner
自己的扫描类基本也是mybatis的,就是在判断上面改动了一点点
public class MyClassPathMapperScanner extends ClassPathMapperScanner { public MyClassPathMapperScanner(BeanDefinitionRegistry registry) { super(registry); } @Override protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { boolean flag = super.isCandidateComponent(beanDefinition); //包名带有Mapper的才会被mybatis代理 boolean mapper = beanDefinition.getBeanClassName().contains("Mapper"); return flag && mapper; } }
现在只需要用自己的扫描注解即可,用法和mybatis的一模一样
这个是针对第三点的,作者使用第三点的时候mybatis版本为3.4.6
mybatis-spring版本为1.3.2,spring版本为5.x
当mybatis版本为3.5.2的mybatis-spring版本为2.0.2的时候
MapperScannerRegistrar类扫描的方式发生了一点点变化,
还需要改写MapperScannerConfigurer类,其他的不变
完毕!
@MapperScan注解使用markerInterface或者annotationClass参数限制扫描的接口
由于不太注意顺手就导了spring的包:
import org.mybatis.spring.annotation.MapperScan;
结果就异常:
tk.mybatis.mapper.provider.base.BaseSelectProvider:xxxx
应该导mybatis的MapperScan而不是spring中的包,正确的包名:
import tk.mybatis.spring.annotation.MapperScan;