Mybatis-Plus主鍵插入null值報(bào)錯(cuò)問(wèn)題及解決
Mybatis-Plus主鍵插入null值報(bào)錯(cuò)
Mysql數(shù)據(jù)庫(kù)主鍵對(duì)應(yīng)的實(shí)體類(lèi)屬性id只要插入null
就會(huì)報(bào)如下錯(cuò)誤:
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)過(guò)重重排除確定是Mysql的自增主鍵對(duì)應(yīng)的實(shí)體類(lèi)屬性不能是null值,要想插入null值,需要在主鍵對(duì)應(yīng)的實(shí)體類(lèi)屬性上加自增注解
?@TableId(type = IdType.AUTO) ? ? private Integer id;
(記錄一下使用Mybatis-Plus中碰到的坑,另外button會(huì)自動(dòng)提交表單一直刷新頁(yè)面,讓我的局部刷新操作失效,這兩個(gè)坑花了我大半個(gè)晚上解決。。。)
mybatis插入數(shù)據(jù)傳入值為null時(shí)提示無(wú)效的列類(lèi)型
原因:mybatis無(wú)法解析值的類(lèi)型
方法一
因?yàn)闊o(wú)法解析null為何種類(lèi)型,可對(duì)傳入值在xml中指定類(lèi)型,如下
<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ǎn)我?hào)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Socket編程實(shí)例(五)- NIO UDP實(shí)踐
這篇文章主要講解Java Socket編程中NIO UDP的實(shí)例,希望能給大家做一個(gè)參考。2016-06-06IDEA設(shè)置Maven自動(dòng)編譯model的實(shí)現(xiàn)方法
本文主要介紹了IDEA設(shè)置Maven自動(dòng)編譯model的實(shí)現(xiàn)方法, maven結(jié)構(gòu)的項(xiàng)目,我們?cè)诿看涡薷拇a后都會(huì)需要手動(dòng)編譯,本文就可以解決這個(gè)問(wèn)題,感興趣的可以了解一下2023-08-08Druid基本配置及內(nèi)置監(jiān)控使用_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Druid基本配置及內(nèi)置監(jiān)控使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Mybatis中Collection集合標(biāo)簽的使用詳解
這篇文章主要介紹了Mybatis中Collection集合標(biāo)簽的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java使用動(dòng)態(tài)規(guī)劃算法思想解決背包問(wèn)題
背包問(wèn)題(Knapsack problem)是一種組合優(yōu)化的NP完全問(wèn)題。問(wèn)題可以描述為:給定一組物品,每種物品都有自己的重量和價(jià)格,在限定的總重量?jī)?nèi),我們?nèi)绾芜x擇,才能使得物品的總價(jià)格最高2022-04-04SpringBoot項(xiàng)目整合mybatis的方法步驟與實(shí)例
今天小編就為大家分享一篇關(guān)于SpringBoot項(xiàng)目整合mybatis的方法步驟與實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Java如何實(shí)現(xiàn)通過(guò)鍵盤(pán)輸入一個(gè)數(shù)組
這篇文章主要介紹了Java實(shí)現(xiàn)通過(guò)鍵盤(pán)輸入一個(gè)數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Sax解析xml_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Sax解析xml,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08