@[TOC](springboot,mybatis报错 Servlet.service() for servlet [dispatcherServlet] in context with path 解决方案)
问题背景
我在使用mybatis-generator生成的xml文件,在进行插入mysql数据库的时候,出现以下问题,因为以下问题并看不出来是什么问题,有很多情况都会出现一样的报错,这里介绍我出现这个问题的原因,如果不是你需要的解决方案,可以看看其他别人的解决方案
2022-02-09 10:06:26.326 ERROR [http-nio-7788-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet].log(DirectJDKLog.java:175): Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.dazhen.data.api.controller.TransformController.executeIdmapping(TransformController.java:133)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:124)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
解决方案
在使用insert插入的时候,我的项目需要用到当条记录的主键,但生成的xml没有返回主键,可以使用两种方式增加返回主键
方式一
打开xml文件,原本为
<insert id="insert" parameterType="data.api.entity.mysql.TransformRecord">
添加返回主键:useGeneratedKeys=“true” keyProperty=“recordId” ,recordId替换为你自己的主键,更改为
<insert id="insert" parameterType="data.api.entity.mysql.TransformRecord"
useGeneratedKeys="true" keyProperty="recordId">
方式二
原本我的generatorConfig配置文件是这样
<table tableName="transform_record">
<property name="useActualColumnNames" value="false"/>
</table>
更改为
<table tableName="transform_record"
enableInsert="true">
<generatedKey column="recordId" sqlStatement="MySql" identity="true"/>
</table>
心得
- 这个问题不好找 ,我完全不知道哪里出错了,报错信息与本身错误无关
作为程序员第 62 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …