求助,使用Mybatis插入id为null时报错,请问如何解决?
求助
使用Mybatis插入id为null时报错
请求的数据如下
{ "DicTypeCode": "1",
"DicCode": "2",
"DicName": "3",
"Remark": "2",
"RecordStartTime": "2023-03-15 15:00:00",
"RecordStopFlag": "1",
"RecordStopTime": "2023-03-15:00:00"
}
报错图
接口代码如下
@RequestMapping(value = "/v1/tmDdDicSet", method={ RequestMethod.POST }) public ResponseResult insertTmDdDicSetV1 (
HttpServletRequest request,
@RequestBody Map<String, Object> tmDdDicSetMap
) {
ResponseResult responseResult = new ResponseResult();
TmDdDicSet tmDdDicSet = new TmDdDicSet();
// System.out.println(tmDdDicSetMap);
// tmDdDicSet.setID(Integer.parseInt(String.valueOf(tmDdDicSetMap.get("ID"))));
tmDdDicSet.setDicTypeCode(String.valueOf(tmDdDicSetMap.get("DicTypeCode")));
tmDdDicSet.setDicCode(String.valueOf(tmDdDicSetMap.get("DicCode")));
tmDdDicSet.setDicName(String.valueOf(tmDdDicSetMap.get("DicName")));
tmDdDicSet.setRemark(String.valueOf(tmDdDicSetMap.get("Remark")));
tmDdDicSet.setRecordStartTime(String.valueOf(tmDdDicSetMap.get("RecordStartTime")));
tmDdDicSet.setRecordStopFlag(String.valueOf(tmDdDicSetMap.get("RecordStopFlag")));
tmDdDicSet.setRecordStopTime(String.valueOf(tmDdDicSetMap.get("RecordStopTime")));
System.out.println(tmDdDicSet);
int tmDdDicSetCount = tmDdDicSetService.insertTmDdDicSetV1(tmDdDicSet);
System.out.println("tmDdDicSetCount == " + tmDdDicSetCount);
return responseResult;
}
TmDdDicSetMapper配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.fcht.mapper.TmDdDicSetMapper">
<resultMap id="BaseResultMap" type="com.fcht.entity.TmDdDicSet">
<id column="ID" jdbcType="INTEGER" property="ID" />
<result column="DicTypeCode" jdbcType="VARCHAR" property="DicTypeCode" />
<result column="DicCode" jdbcType="VARCHAR" property="DicCode" />
<result column="DicName" jdbcType="VARCHAR" property="DicName" />
<result column="Remark" jdbcType="VARCHAR" property="Remark" />
<result column="RecordStartTime" jdbcType="DATE" property="RecordStartTime" />
<result column="RecordStopFlag" jdbcType="VARCHAR" property="RecordStopFlag" />
<result column="RecordStopTime" jdbcType="DATE" property="RecordStopTime" />
</resultMap>
<sql id="Base_Column_List">
ID, DicTypeCode, DicCode, DicName, Remark, RecordStartTime, RecordStopFlag, RecordStopTime
</sql>
<select id="selectTmDdDicSetV1" parameterType="Map" resultType="TmDdDicSet">
SELECT <include refid="Base_Column_List" />
FROM tm_dd_dicset tddset
<choose>
<when test="sort=='ASC'">
ORDER BY RecordStopTime ASC
</when>
<otherwise>
ORDER BY RecordStopTime DESC
</otherwise>
</choose>
limit 0, #{limit}
</select>
<select id="getTmDdDicSetCountV1" resultType="java.lang.Integer">
SELECT count(id) FROM tm_dd_dicset tddset
</select>
<insert id="insertTmDdDicSetV1" parameterType="TmDdDicSet"
useGeneratedKeys="true" keyProperty="ID"
>
INSERT INTO tm_dd_dicset
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="ID != null">
ID,
</if>
<if test="DicTypeCode != null">
DicTypeCode,
</if>
<if test="DicCode != null">
DicCode,
</if>
<if test="DicName != null">
DicName,
</if>
<if test="Remark != null">
Remark,
</if>
<if test="RecordStartTime != null">
RecordStartTime,
</if>
<if test="RecordStopFlag != null">
RecordStopFlag,
</if>
<if test="RecordStopTime != null">
RecordStopTime,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="ID != null">
#{ID,jdbcType=VARCHAR},
</if>
<if test="DicTypeCode != null">
#{DicTypeCode,jdbcType=VARCHAR},
</if>
<if test="DicCode != null">
#{DicCode},
</if>
<if test="DicName != null">
#{DicName},
</if>
<if test="Remark != null">
#{Remark},
</if>
<if test="RecordStartTime != null">
#{RecordStartTime},
</if>
<if test="RecordStopFlag != null">
#{RecordStopFlag},
</if>
<if test="RecordStopTime != null">
#{RecordStopTime},
</if>
</trim>
</insert>
</mapper>
TmDdDicSet代码
import java.util.Date;public class TmDdDicSet {
private Integer ID;
private String DicTypeCode;
private String DicCode;
private String DicName;
private String Remark;
private String RecordStartTime;
private String RecordStopFlag;
private String RecordStopTime;
public TmDdDicSet() {
}
public TmDdDicSet(Integer ID, String dicTypeCode, String dicCode, String dicName, String remark, String recordStartTime, String recordStopFlag, String recordStopTime) {
this.ID = ID;
this.DicTypeCode = dicTypeCode;
this.DicCode = dicCode;
this.DicName = dicName;
this.Remark = remark;
this.RecordStartTime = recordStartTime;
this.RecordStopFlag = recordStopFlag;
this.RecordStopTime = recordStopTime;
}
public int getID() {
System.out.println("ID == " + ID);
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getDicTypeCode() {
return DicTypeCode;
}
public void setDicTypeCode(String dicTypeCode) {
DicTypeCode = dicTypeCode;
}
public String getDicCode() {
return DicCode;
}
public void setDicCode(String dicCode) {
DicCode = dicCode;
}
public String getDicName() {
return DicName;
}
public void setDicName(String dicName) {
DicName = dicName;
}
public String getRemark() {
return Remark;
}
public void setRemark(String remark) {
Remark = remark;
}
public String getRecordStartTime() {
return RecordStartTime;
}
public void setRecordStartTime(String recordStartTime) {
RecordStartTime = recordStartTime;
}
public String getRecordStopFlag() {
return RecordStopFlag;
}
public void setRecordStopFlag(String recordStopFlag) {
RecordStopFlag = recordStopFlag;
}
public String getRecordStopTime() {
return RecordStopTime;
}
public void setRecordStopTime(String recordStopTime) {
RecordStopTime = recordStopTime;
}
@Override
public String toString() {
return "TmDdDicSet [ID= " + ID + ", " +
"DicTypeCode= " + DicTypeCode + ", " +
"DicCode= " + DicCode + ", " +
"DicName= " + DicName + ", " +
"RecordStartTime= " + RecordStartTime +
"RecordStopFlag= " + RecordStopFlag +
"RecordStopTime= " + RecordStopTime +
"]";
}
}
完整报错信息
TmDdDicSet [ID= null, DicTypeCode= 1, DicCode= 2, DicName= 3, RecordStartTime= 2023-03-15 15:00:00RecordStopFlag= 1RecordStopTime= 2023-03-15:00:00]ID == null
2023-03-13 18:44:18.302 ERROR 91940 --- [nio-8079-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.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.lang.NullPointerException
### The error may exist in file [/Users/shanguangqing/Desktop/t1/fcht_java/fcht-management/target/classes/mapper/TmDdDicsetMapper.xml]
### The error may involve com.fcht.mapper.TmDdDicSetMapper.insertTmDdDicSetV1
### The error occurred while executing an update
### Cause: java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.fcht.entity.TmDdDicSet.getID(TmDdDicSet.java:31) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_271]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_271]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_271]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_271]
at org.apache.ibatis.reflection.invoker.MethodInvoker.invoke(MethodInvoker.java:44) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:164) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextMap.get(DynamicContext.java:102) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:113) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2719) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:493) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:457) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32) ~[mybatis-3.5.2.jar:3.5.2]
at java.util.ArrayList.forEach(ArrayList.java:1259) ~[?:1.8.0_271]
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.TrimSqlNode.apply(TrimSqlNode.java:55) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32) ~[mybatis-3.5.2.jar:3.5.2]
at java.util.ArrayList.forEach(ArrayList.java:1259) ~[?:1.8.0_271]
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:297) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:64) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:41) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:46) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:592) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184) ~[mybatis-3.5.2.jar:3.5.2]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_271]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_271]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_271]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_271]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) ~[mybatis-spring-2.0.2.jar:2.0.2]
at com.sun.proxy.$Proxy129.insert(Unknown Source) ~[?:?]
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:271) ~[mybatis-spring-2.0.2.jar:2.0.2]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62) ~[mybatis-3.5.2.jar:3.5.2]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57) ~[mybatis-3.5.2.jar:3.5.2]
at com.sun.proxy.$Proxy130.insertTmDdDicSetV1(Unknown Source) ~[?:?]
at com.fcht.service.impl.TmDdDicSetServiceImpl.insertTmDdDicSetV1(TmDdDicSetServiceImpl.java:26) ~[classes/:?]
at com.fcht.controller.TmDdDicSetController.insertTmDdDicSetV1(TmDdDicSetController.java:82) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_271]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_271]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_271]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_271]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88) ~[spring-boot-actuator-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114) ~[spring-boot-actuator-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104) ~[spring-boot-actuator-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.21.jar:9.0.21]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_271]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_271]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.21.jar:9.0.21]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_271]
回答:
数据库没有设置自增id。
如果想要自定义id的话,java需要设置id,比如放一个雪花id.
回答:
你这个问题在于,Integer ID;
。
这块的ID
类型为包装数据类型,而你的get
方法,返回的是int
。
遇到这种场景时,需要有一个拆包的操作。即需要将Integer -> int
转换一下,会自动调用Integer.intValue()
方法。
即: return ID; --> return ID.intValue();
因为ID
为null
,所以出现NPE
本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。
以上是 求助,使用Mybatis插入id为null时报错,请问如何解决? 的全部内容, 来源链接: utcz.com/p/945036.html