最近学习springBoot,遇到了一些问题。特别是今天在学习springBoot整合mybatis时,遇到一些问题,网上搜索了好久也才解决,特此记录。
说明:使用idea编译器,jdk版本1.8,spring-boot的版本是2.2.1.RELEASE,视图层使用默认的thymeleaf。
异常1:
2020-02-07 21:25:16.926 ERROR 5604 --- [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.jw.mapper.UserMapper.addUser] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.jw.mapper.UserMapper.addUser
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:214) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) ~[mybatis-3.4.0.jar:3.4.0]
at com.sun.proxy.$Proxy59.addUser(Unknown Source) ~[na:na]
首先排查了mapper接口文件和mapper.xml文件,路径一致,名称也相同。通过百度知道,idea要求mapper.xml文件需要放到resources文件夹下,这才恍然大悟。
异常1处理:①.修改mapper.xml文件的位置,如下图:
②修改application.properties配置文件:
#配置的是mapper.xml文件的路径
mybatis.mapper-locations = mapper/*.xml
异常2:
异常2处理:
修改配置文件:
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
异常3:
异常3处理:
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&useSSL=false