Mybatis-Plus主键插入null值报错问题及解决
作者:Win-ch
Mybatis-Plus主键插入null值报错
Mysql数据库主键对应的实体类属性id只要插入null
就会报如下错误:
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.entity.Student' with value '1281212463907368960' Cause: java.lang.IllegalArgumentException: argument type mismatch
### Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.entity.Student' with value '1281212463907368960' Cause: java.lang.IllegalArgumentException: argument type mismatch
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy5.save(Unknown Source)
at com.baizhi.service.Impl.StudentServiceImpl.save(StudentServiceImpl.java:59)
at Testdao.TestService(Testdao.java:24)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.entity.Student' with value '1281212463907368960' Cause: java.lang.IllegalArgumentException: argument type mismatch
at org.apache.ibatis.reflection.wrapper.BeanWrapper.setBeanProperty(BeanWrapper.java:185)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.set(BeanWrapper.java:59)
at org.apache.ibatis.reflection.MetaObject.setValue(MetaObject.java:140)
at com.baomidou.mybatisplus.MybatisDefaultParameterHandler.populateKeys(MybatisDefaultParameterHandler.java:175)
at com.baomidou.mybatisplus.MybatisDefaultParameterHandler.processBatch(MybatisDefaultParameterHandler.java:119)
at com.baomidou.mybatisplus.MybatisDefaultParameterHandler.<init>(MybatisDefaultParameterHandler.java:79)
at com.baomidou.mybatisplus.MybatisXMLLanguageDriver.createParameterHandler(MybatisXMLLanguageDriver.java:37)
at org.apache.ibatis.session.Configuration.newParameterHandler(Configuration.java:532)
at org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:69)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:40)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:46)
at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:545)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
... 28 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
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.apache.ibatis.reflection.invoker.MethodInvoker.invoke(MethodInvoker.java:41)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.setBeanProperty(BeanWrapper.java:180)
... 43 more
经过重重排除确定是Mysql的自增主键对应的实体类属性不能是null值,要想插入null值,需要在主键对应的实体类属性上加自增注解
@TableId(type = IdType.AUTO) private Integer id;
(记录一下使用Mybatis-Plus中碰到的坑,另外button会自动提交表单一直刷新页面,让我的局部刷新操作失效,这两个坑花了我大半个晚上解决。。。)
mybatis插入数据传入值为null时提示无效的列类型
原因:mybatis无法解析值的类型
方法一
因为无法解析null为何种类型,可对传入值在xml中指定类型,如下
<insert id="batchInsertQuestion" useGeneratedKeys="false"> insert all <foreach collection="list" item="item" index="index"> into ZB_APPRAISE_RECORD_QUESTION (id, record_id, question_name, sort_no, created_by, created_time, modified_by, modified_time,option_type,appraise_id) values (#{item.id},#{item.recordId},#{item.questionName,jdbcType=varchar},#{item.sortNo},#{item.createdBy},#{item.createdTime},#{item.modifiedBy},#{item.modifiedTime},#{item.optionType},#{item.appraiseId}) </foreach> select 1 from dual </insert>
方法二
在mybatis-config.xml配置文件中配置:
<?xml version=”1.0” encoding=”UTF-8” ?> <!DOCTYPE configuration PUBLIC “-//mybatis.org//DTD Config 3.0//EN” “http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> … <settings> <setting name="jdbcTypeForNull" value="NULL" /> </settings> … </configuration>
springboot yml配置方法如下
mybatis-plus: configuration: jdbc-type-for-null: 'null' #注意:单引号
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。