关于Service方法调用时出现org.apache.ibatis.binding.BindingException错误的可能原因

项目是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注解才起作用。有路过的大神指教一下吗?



  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Mybatisplus的org.apache.ibatis.binding.BindingException是一种常见的异常,表示绑定语句无效(未找到)。这个异常通常发生在使用mybatis或mybatis-plus作为持久化框架,通过dao层接口调用xml中配置好的sql出现这个异常的原因可能有几种: 1. SQL语句没有正确配置或命名错误,导致无法找到对应的绑定语句。 2. Dao层接口没有正确绑定或命名错误,导致无法找到对应的绑定语句。 解决这个异常的方法有以下几种: 1. 检查SQL语句的配置和命名是否正确,并确保与Dao层接口的绑定一致。 2. 检查Dao层接口的绑定和命名是否正确,并确保与SQL语句的配置一致。 3. 确保XML配置文件中正确引入了Mapper接口,并且Mapper接口中的方法与XML文件中的配置一致。 4. 如果使用了mybatis-plus,可以尝试使用其提供的CRUD代码生成器来生成Dao层接口和XML文件,可以减少手动配置错误可能性。 5. 检查依赖的版本是否兼容,有不同的版本之间可能存在一些兼容性问题。 通过以上方法,可以解决Mybatisplus的org.apache.ibatis.binding.BindingException异常。请根据具体情况选择合适的方法进行调试和修复。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement](https://blog.csdn.net/w1014074794/article/details/125725011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [springboot 集成 mybatis-plus报错解决:org.apache.ibatis.binding.BindingException: Invalid bound ...](https://blog.csdn.net/big_bigwolf/article/details/120752264)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值