Mybatis-Plus主鍵插入null值報錯問題及解決
Mybatis-Plus主鍵插入null值報錯
Mysql數(shù)據(jù)庫主鍵對應(yīng)的實體類屬性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
經(jīng)過重重排除確定是Mysql的自增主鍵對應(yīng)的實體類屬性不能是null值,要想插入null值,需要在主鍵對應(yīng)的實體類屬性上加自增注解
?@TableId(type = IdType.AUTO) ? ? private Integer id;
(記錄一下使用Mybatis-Plus中碰到的坑,另外button會自動提交表單一直刷新頁面,讓我的局部刷新操作失效,這兩個坑花了我大半個晚上解決。。。)
mybatis插入數(shù)據(jù)傳入值為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' #注意:單引號
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA設(shè)置Maven自動編譯model的實現(xiàn)方法
本文主要介紹了IDEA設(shè)置Maven自動編譯model的實現(xiàn)方法, maven結(jié)構(gòu)的項目,我們在每次修改代碼后都會需要手動編譯,本文就可以解決這個問題,感興趣的可以了解一下2023-08-08Druid基本配置及內(nèi)置監(jiān)控使用_動力節(jié)點Java學院整理
這篇文章主要介紹了Druid基本配置及內(nèi)置監(jiān)控使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08Java使用動態(tài)規(guī)劃算法思想解決背包問題
背包問題(Knapsack problem)是一種組合優(yōu)化的NP完全問題。問題可以描述為:給定一組物品,每種物品都有自己的重量和價格,在限定的總重量內(nèi),我們?nèi)绾芜x擇,才能使得物品的總價格最高2022-04-04