Skip to content

ClassPathMapperScanner scan mappers interface do not check if it contains @Mapper annotation #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maxjiang153 opened this issue Oct 26, 2017 · 3 comments

Comments

@maxjiang153
Copy link

maxjiang153 commented Oct 26, 2017

when scan interfaces it will proxy all interface even it does not contains @Mapper interface.

https://github.com/mybatis/spring/blob/master/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java#L216

  protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
    return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent();
  }


  should be check has @Mapper annotation like this?
  protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
    return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent() 
                    &&  beanDefinition.getMetadata().hasAnnotation(Mapper.class.getName()); 
  }

example :

interface Example {
....
}

will create MapperFactoryBean proxy without @Mapper

@kazuki43zoo
Copy link
Member

Probably you can scan only interfaces that annotated the @Mapper invoking ClassPathMapperScanner.setAnnotationClass(Mapper.class) .

The @Mapper has been added as the optional annotation for narrowing down target interfaces on DI container like CDI or Spring's ApplicationContext.
For details see mybatis/mybatis-3#629.

Note:
This annotation is used the MybatisAutoConfiguration provided by mybatis-spring-boot-starter.

@kazuki43zoo
Copy link
Member

For example:

<mybatis:scan base-package="com.example" annotation="org.apache.ibatis.annotations.Mapper" />

or

@MapperScan(basePackages = "com.example", annotationClass = Mapper.class)
public class MyBatisConfig {
  // ...
}

@maxjiang153
Copy link
Author

@kazuki43zoo thanks : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants