项目是SpringBoot + Mybatis构建的。关键组件版本如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.2</version>
</dependency>
下面给出关键代码:
@SpringBootApplication
@ServletComponentScan
@EnableCaching
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) throws IOException {
SpringApplication.run(Application.class, args);
}
}
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.xxx.retailOrder.mapper");// mapper package namespace
return mapperScannerConfigurer;
}
}
@RequestMapping(value = "/xxxOrderSignatureReceived", produces = APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseDTO<?> orderSignatureReceived(@Valid @RequestBody RetailOrderReceived2DTO dto){
myRetailOrderService.orderReceipt(dto);
return onSuccess(null);
}
控制台此时会报出这样的错误:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxx.retailorder.service.MyRetailOrderService.orderReceipt
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:196) ~[mybatis-3.3.1.jar:3.3.1]
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:44) ~[mybatis-3.3.1.jar:3.3.1]
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59) ~[mybatis-3.3.1.jar:3.3.1]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) ~[mybatis-3.3.1.jar:3.3.1]
注:根据经验,先要排除Mybatis配置等相关错误原因
根据上面日志,不难想像:在控制层注入MyRetailOrderService对象时,使用注解@Autowired注入时,从Mybatis相关类中拿到了CGLIB方式生成的对象,而不是我容器本身业务对象。也就是说,我的业务层对象被代理生成了Mybatis用的Mapper类代理对象。这不是正常操作,这个事情springboot什么时候做的呢?
本人对SpringBoot不太了解。查阅相关资料,说是根据依赖包会进行相关自动配置。有了这个理论前提,我用方法一:禁止SpringBoot对Mybatis自动配置。
@SpringBootApplication(exclude = MybatisAutoConfiguration.class)
@ServletComponentScan
@EnableCaching
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) throws IOException {
SpringApplication.run(Application.class, args);
}
}
方法二:强行用MapperScan指定扫描范围
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
@MapperScan(basePackages = "com.xxx.retailorder.mapper")
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.xxx.retailorder.mapper");// mapper package namespace,此处指定不起作用
return mapperScannerConfigurer;
}
}
对于方法二不明白的地方是,为什么mapperScannerConfiguer设置扫描范围不起作用,非要用MapperScan注解才起作用。有路过的大神指教一下吗?