Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`mybatis`.`orderdetail`, CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ### The error may involve com.ssm.mapper.ItemsMapper.deleteByPrimaryKey-Inline ### The error occurred while setting parameters ### SQL: delete from items where id = ? ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`mybatis`.`orderdetail`, CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ; SQL []; Cannot delete or update a parent row: a foreign key constraint fails (`mybatis`.`orderdetail`, CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION); nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`mybatis`.`orderdetail`, CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 1.以上是因为,该条记录的某个字段作为的另外一个表的外链。而在删除操作时会自动检查外链。 解决办法一:先判断删除关联数据,然后再删除(这样比较符合业务逻辑比较安全)。解决办法二:不检查外链,设置FOREIGN_KEY_CHECKS变量: SET FOREIGN_KEY_CHECKS = 0; DELETE FROM `goods` WHERE `goods_id` = '11' 删除完成后设置 SET FOREIGN_KEY_CHECKS = 1;
ps:关闭唯一性校验 set unique_checks=0; set unique_checks=1;
2.解决的方法是java.sql.Statement.getGeneratedKeys(),需要西东捕获自动生成的主键
未完待续