数据库主键重复java报错信息_Mybatis-plus 数据库自增主键为int 类型对应Java类类型为Integer 报错问题...

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.jpgk.entity.report.OrgWarehouseGoods' with value '1247386398982488065'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.$Proxy87.insert(Unknown Source)

at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)

at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:58)

at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:61)

at com.sun.proxy.$Proxy132.insertBatch(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:343)

at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)

at com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor.invoke(DynamicDataSourceAnnotationInterceptor.java:49)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)

at com.sun.proxy.$Proxy133.insertBatch(Unknown Source)

at com.jpgk.springboot.base.DefaultBaseService.insertBatch(DefaultBaseService.java:45)

at com.jpgk.springboot.base.DefaultBaseService$$FastClassBySpringCGLIB$$e79299e8.invoke()

at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)

at com.jpgk.report.service.impl.OrgWarehouseGoodsServiceImpl$$EnhancerBySpringCGLIB$$d26d0ac1.insertBatch()

at com.jpgk.report.job.utils.JobUtil.insertBatch(JobUtil.java:20)

at com.jpgk.report.job.service.iml.BackupJobService.warehouseGoodsInsertBatch(BackupJobService.java:37)

at com.jpgk.report.job.OrgWarehouseGoodsBackupJob.orgWarehouseGoodsBackupJob(OrgWarehouseGoodsBackupJob.java:33)

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.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)

at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)

at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)

at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)

at java.util.concurrent.FutureTask.run(FutureTask.java)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

Caused by: org.apache.ibatis.reflection.ReflectionException: Could not set property'id' of 'class com.jpgk.entity.report.OrgWarehouseGoods' with value '1247386398982488065'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:181)

at com.baomidou.mybatisplus.core.MybatisDefaultParameterHandler.processBatch(MybatisDefaultParameterHandler.java:94)

at com.baomidou.mybatisplus.core.MybatisDefaultParameterHandler.(MybatisDefaultParameterHandler.java:52)

at com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver.createParameterHandler(MybatisXMLLanguageDriver.java:34)

at com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver.createParameterHandler(MybatisXMLLanguageDriver.java:28)

at org.apache.ibatis.session.Configuration.newParameterHandler(Configuration.java:579)

at org.apache.ibatis.executor.statement.BaseStatementHandler.(BaseStatementHandler.java:69)

at org.apache.ibatis.executor.statement.PreparedStatementHandler.(PreparedStatementHandler.java:41)

at org.apache.ibatis.executor.statement.RoutingStatementHandler.(RoutingStatementHandler.java:46)

at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:592)

at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doUpdate(MybatisSimpleExecutor.java:52)

at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)

at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)

at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)

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)

...39more

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:44)

at org.apache.ibatis.reflection.wrapper.BeanWrapper.setBeanProperty(BeanWrapper.java:180)

...60 more

### 回答1: Mybatis-Plus可以通过注解和配置文件的方式设置主键自增。 1. 注解方式: 在实体中,使用注解`@TableId`来标识主键,并设置`type`为`IdType.AUTO`,表示自增。 示例代码: ``` @Data public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; } ``` 2. 配置文件方式: 在Mybatis-Plus的配置文件中,设置全局的主键类型自增。 示例代码: ``` mybatis-plus: global-config: db-config: id-type: auto ``` 以上两种方式都可以实现主键自增的功能。 ### 回答2: Mybatis-plus是一个优秀的ORM框架,它的主键设置可以支持自增功能。下面是主键自增的设置过程: 1. 在数据库表中设置主键自增数据库表中创建主键时需要设置auto_increment属性来启用自增功能,如下所示: CREATE TABLE user ( id INT(11) NOT NULL AUTO_INCREMENT, username VARCHAR(50) DEFAULT NULL, password VARCHAR(50) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=INNODB DEFAULT CHARSET=utf8; 2. 在实体中添加主键自增注解 Mybatis-plus的实体需要使用注解来标识主键生成策略,这里使用的是@TableId注解,将type设置为IdType.AUTO可以启用自增功能,如下所示: @Data public class User { @TableId(type = IdType.AUTO) private Long id; private String username; private String password; } 3. 在mapper.xml中配置主键自增属性 在mapper.xml中需要添加useGeneratedKeys和keyProperty属性配置主键自增属性,如下所示: <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user(username, password) VALUES (#{username}, #{password}) </insert> 至此,Mybatis-plus主键自增设置已完成。使用这种方式可以让我们更方便地进行数据库操作,可以极大地提高开发效率。 ### 回答3: Mybatis-Plus是基于Mybatis的增强工具,它提供了很多便捷的操作数据库的方法,如使用注解方式实现插入、更新、删除等操作。当我们需要设置主键自增时,可以通过配置实现,以下是具体的方法: 1. 在数据库表中设置主键自增 首先我们需要在数据库表中设置主键自增,其方法如下: 创建表: CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `age` int(11) DEFAULT NULL, `email` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 在表中设置为自增长: ALTER TABLE `user` MODIFY COLUMN `id` BIGINT(20) NOT NULL AUTO_INCREMENT; 2. Mybatis-Plus配置主键自增 在我们使用Mybatis-Plus插入数据时,如果要设置主键自增,可以在实体中的@Id注解上添加@KeySequence注解,具体的方法如下: 在实体中添加注解: @Data @NoArgsConstructor @KeySequence("seq_user") public class User { @TableId(value = "id", type = IdType.AUTO) private Long id; private String name; private Integer age; private String email; } 其中,@KeySequence中的参数为数据库中的自增长序列,需要根据实际情况进行设置。 然后在插入数据时,由于设置了@Id注解,Mybatis-Plus会自动将数据库中的自增长序列作为主键值进行插入,无需再手动设置主键值,具体方法如下: @Autowired private UserMapper userMapper; @Test public void testInsert(){ User user = new User(); user.setName("Tom"); user.setAge(17); user.setEmail("tom@example.com"); userMapper.insert(user); } 以上就是Mybatis-Plus设置主键自增的具体方法。通过这种方法,不仅可以避免了手动设置主键值的麻烦,还可以有效地提高插入数据的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值