解决报错问题:SQLIntegrityConstraintViolationException: Duplicate entry '4' for key 'PRIMARY'

解决报错问题:SQLIntegrityConstraintViolationException: Duplicate entry ‘4’ for key ‘PRIMARY’

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4813a18a] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@703e71fd] will not be managed by Spring
==> Preparing: INSERT INTO wx_main_detail ( home_id, sort_order, create_time ) values ( ?, ?, ? )
==> Parameters: 3(Integer), 1(Integer), 2019-11-27 10:51:58.805(Timestamp)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4813a18a]
2019-11-27 10:51:58.865 ERROR 119624 — [ XNIO-1 task-29] c.u.l.c.e.GlobalExceptionHandler : 异常信息uri=/sys/WxMainHome/saveEditIn,method=POST,e={}

org.springframework.dao.DuplicateKeyException:

Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry ‘4’ for key ‘PRIMARY’

The error may involve com.ultrapower.life.mapper.WxMainDetailMapper.insertId-Inline

The error occurred while setting parameters

SQL: INSERT INTO wx_main_detail ( home_id, sort_order, create_time ) values ( ?, ?, ? )

Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry ‘4’ for key ‘PRIMARY’

; Duplicate entry ‘4’ for key ‘PRIMARY’; nested exception is java.sql.SQLIntegrityConstraintViolationException: Duplicate entry ‘4’ for key ‘PRIMARY’
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:242)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate S q l S e s s i o n I n t e r c e p t o r . i n v o k e ( S q l S e s s i o n T e m p l a t e . j a v a : 446 ) a t c o m . s u n . p r o x y . SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) at com.sun.proxy. SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)atcom.sun.proxy.Proxy101.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)

看报错信息,“Duplicate entry ‘4’ for key ‘PRIMARY’”表示,主键有重复的,因此,错误的原因就是值重复了,解决此类问题就是将插入的值不能是数据库已经有了的主键,

在我这个项目中主要是把表的id设置为不是自动递增(去掉勾选)

解决报错问题:SQLIntegrityConstraintViolationException: Duplicate entry '4' for key 'PRIMARY'_第1张图片

在我的插入语句的mapper文件里插入需要的id即可:

解决报错问题:SQLIntegrityConstraintViolationException: Duplicate entry '4' for key 'PRIMARY'_第2张图片

后面我的项目就跑起来了!!!!!!!!!!!!!!!!

你可能感兴趣的:(Java,SQL,SQL报错)