欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot整合mybatis访问时报错Invalid bound statement (not found)

程序员文章站 2023-04-03 19:50:19
最近在学springboot相关知识,在整合mybatis时遇到了一个问题(我这种新手易犯的低级错误),项目启动不报错页面也能正常访问,但是一点击UserController的addUser方法时,后台报错,swaggerUI页面也显示报错,具体错误信息如下:o.s.web.servlet.HandlerExecutionChain : HandlerInterceptor.afterCompletion threw exceptionjava.lang.NullPointerExcepti...

最近在学springboot相关知识,在整合mybatis时遇到了一个问题(我这种新手易犯的低级错误),项目启动不报错页面也能正常访问,但是一点击UserController的addUser方法时,后台报错,swaggerUI页面也显示报错,具体错误信息如下:

o.s.web.servlet.HandlerExecutionChain    : HandlerInterceptor.afterCompletion threw exception

java.lang.NullPointerException: null
	at com.kongoking.springboot.interceptor.TimeInterceptor.afterCompletion(TimeInterceptor.java:36) ~[classes/:na]
	at
 .....

2020-07-14 09:00:58.464  INFO 8372 --- [nio-8080-exec-1] c.k.s.interceptor.LogInterceptor         : afterCompletion
2020-07-14 09:00:58.466 ERROR 8372 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.kongoking.springboot.mybatis.dao.UserMapper.insert] with root cause

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.kongoking.springboot.mybatis.dao.UserMapper.insert
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225) ~[mybatis-3.4.4.jar:3.4.4]
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48) ~[mybatis-3.4.4.jar:3.4.4]
	at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) ~[mybatis-3.4.4.jar:3.4.4]
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) ~[mybatis-3.4.4.jar:3.4.4]
	at com.sun.proxy.$Proxy116.insert(Unknown Source) ~[na:na]
	at com.kongoking.springboot.mybatis.service.UserService.addUser(UserService.java:17) ~[classes/:na]
	at com.kongoking.springboot.mybatis.controller.UserController.addUser(UserController.java:22) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_241]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_241]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_241]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_241]
....
2020-07-14 09:00:58.471  INFO 8372 --- [nio-8080-exec-1] c.k.s.interceptor.LogInterceptor         : preHandle
2020-07-14 09:00:58.471  INFO 8372 --- [nio-8080-exec-1] c.k.s.interceptor.TimeInterceptor        : 开始时间:1594688458471
2020-07-14 09:00:58.505  INFO 8372 --- [nio-8080-exec-1] c.k.s.interceptor.TimeInterceptor        : 结束时间:1594688458505
2020-07-14 09:00:58.505  INFO 8372 --- [nio-8080-exec-1] c.k.s.interceptor.LogInterceptor         : postHandle
2020-07-14 09:00:58.506  INFO 8372 --- [nio-8080-exec-1] c.k.s.interceptor.TimeInterceptor        : 接口执行时间:34毫秒
2020-07-14 09:00:58.506  INFO 8372 --- [nio-8080-exec-1] c.k.s.interceptor.LogInterceptor         : afterCompletion

通过报错部分知道是在调用UserMapper接口中的insert方法时报的错,由于在IDEA中看到了标红提示 userMapper "Could not autowire. No beans of 'UserMapper' type found."一开始便一直以为是未在主入口处添加@MapperScan("mapper文件所在包名路径")导致@Autowired不生效的问题,但是加上对应注解后还是不行。还有一种说法是不在入口主类中添加@MapperScan注解,在每个单独的Mapper类中加上@Mapper注解,经过尝试发现此方法还是不行。后台通过debugger 看到Service中的mapper的确生成了一个代理对象,截图如下:

SpringBoot整合mybatis访问时报错Invalid bound statement (not found)

 

这就让我很纳闷,对象有生成有注入,那就说明mybatis本身运行是没问题的,生成的对象绑定失败,我想了想只可能是配置文件哪里出问题了,可是仔细对照路径后发现也没啥问题啊。(以下是错误配置

#数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  devtools:
    restart:
      enabled: true
      additional-paths: src/main/java
  #注意此处的mybatis层级错误
  mybatis:
    type-aliases-package: com.kongoking.springboot.mybatis.models
    mapper-locations: classpath:mappers/*.xml


经过一番查找后,发现了问题所在,在yaml语言中是以制表符,空格来表示层级关系的,在上面的配置中mybatis标签与datasource对齐说明这个属性是spring标签的子标签,实际读取时相当于properties中的 spring.mybatis.xxx  ,而通过查资料发现

在springboot中使用propertie 方式配置mybatis时应该是这样的:

mybatis.config-location=classpath:mybatis/mybatis-config.xml

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

这就是说在yml格式的配置文件中mybatis的标签应该在顶层,前面应该不带任何空格表示属于顶层配置。修正后的正确配置文件如下:

#数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  devtools:
    restart:
      enabled: true
      additional-paths: src/main/java
#正确的mybatis配置应该是*,与spring对齐
mybatis:
    type-aliases-package: com.kongoking.springboot.mybatis.models
    mapper-locations: classpath:mappers/*.xml

参考链接:IDEA中springboot整合mybatis报错invalid statement bind

注意:yml格式与properties 格式文件方式的不同

本文地址:https://blog.csdn.net/LUONIANXIN08/article/details/107334172