问答详情
源自:2-2 常用注解

插入数据的问题

entity:
@Data
@TableName("sys_area")
public class Area {

    @TableId
    private Integer areaId;

    private String areaName;

    private Integer priority;

    private Date createTime;

    private Date lastEditTime;
}
Test方法
    @Test
    public void addUer(){

        Area area = new Area();
        area.setAreaName("南苑");
        area.setPriority(3);
//        area.setAreaId(1);
        area.setCreateTime(new Date());
        area.setLastEditTime(new Date());
        areaMapper.insert(area);
//        System.out.println("insertID="+id);
    }
报错如下
Creating a new SqlSessionSqlSession
 [org.apache.ibatis.session.defaults.DefaultSqlSession@319c3a25] was not registered for synchronization because synchronization is not active
 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@319c3a25]org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'areaId' of 'class com.imooc.secondapp.entiy.Area' with value '1294924723589332994' Cause: java.lang.IllegalArgumentException: argument type mismatch	
 at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)	
 at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)	
 at com.sun.proxy.$Proxy53.insert(Unknown Source)	
 at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)	
 at com.baomidou.mybatisplus.core.override.PageMapperMethod.execute(PageMapperMethod.java:68)	
 at com.baomidou.mybatisplus.core.override.PageMapperProxy.invoke(PageMapperProxy.java:64)	
 at com.sun.proxy.$Proxy61.insert(Unknown Source)	
 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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)	
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)	
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)	
 at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)	
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)	
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)	
 at com.sun.proxy.$Proxy62.insert(Unknown Source)	
 at com.imooc.secondapp.SecondAppApplicationTests.addUer(SecondAppApplicationTests.java:38)	
 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.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)	
 at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)	
 at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)	
 at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)	
 at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)	
 at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)	
 at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)	
 at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)	
 at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)	
 at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)	
 at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)	
 at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)	
 at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)	
 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)	
 at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)	
 at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)	at java.util.ArrayList.forEach(ArrayList.java:1249)	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)	at java.util.ArrayList.forEach(ArrayList.java:1249)	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)Caused by: org.apache.ibatis.reflection.ReflectionException: Could not set property 'areaId' of 'class com.imooc.secondapp.entiy.Area' with value '1294924723589332994' 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.core.MybatisDefaultParameterHandler.populateKeys(MybatisDefaultParameterHandler.java:209)	at com.baomidou.mybatisplus.core.MybatisDefaultParameterHandler.processBatch(MybatisDefaultParameterHandler.java:147)	at com.baomidou.mybatisplus.core.MybatisDefaultParameterHandler.<init>(MybatisDefaultParameterHandler.java:59)	at com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver.createParameterHandler(MybatisXMLLanguageDriver.java:37)	at org.apache.ibatis.session.Configuration.newParameterHandler(Configuration.java:545)	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:558)	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)	at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)	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.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)	... 80 moreCaused 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)	... 101 more2020-08-16 17:11:27.324  INFO 7676 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...2020-08-16 17:11:27.329  INFO 7676 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.Process finished with exit code 255


提问者:江湖卖药的 2020-08-16 17:16

个回答

  • 老猿
    2020-08-30 18:22:24

    我怀疑这应该是MP默认主键策略的问题,你没配置策略,他的默认策略是雪花算法,用雪花算法生成主键了。而你主键类型为Integer,最大21亿左右,超范围了,所以报错。后来你配置了策略为AUTO,就是数据库自增,MP就不给你设置主键了,所以正常。

  • 江湖卖药的
    2020-08-16 17:38:39

    但是我用这样就可以插入了。

    我这个样子写就没有问题了。老师请问这是问什么了?

    http://img1.mukewang.com/5f38fe6e00018ecd11140790.jpg