按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解。
但是加入此注解之后,启动时会出现如下警告:
Skipping MapperFactoryBean with name 'xxxMapper' and 'tk.mybatis.xxx.mapper.xxxMapper' mapperInterface. Bean already defined with the same name! No MyBatis mapper was found in '[tk.mybatis]' package. Please check your configuration.
虽然不影响运行,但是对于追求完美的童鞋而言,却是小有遗憾。
两条信息各自对应了一个问题,逐条解决即可。
第一个问题是由 Mapper 注解引起的,将其去掉。但是这样一来,第二个问题所指出的找不到 mapper 包的问题,就会引起 Mapper bean 找不到的问题。
嗯,在配置中添加 Mapper 扫描的基础包即可,如果用 xml,配置如下:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="tk.mybatis.**.mapper"/>
</bean>
如果用 Java 配置,配置如下:
@Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("tk.mybatis.**.mapper"); return mapperScannerConfigurer; }
世界清静了!