案例:当你删除的表数据包含外键,关联其他表数据时。删除当前数据,会造成其他数据成为“孤儿”,可能会造成数据混乱。因此我们需要再MySQL中进行外键约束
具体的SQL语句:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tb_node
-- ----------------------------
DROP TABLE IF EXISTS `tb_node`;
CREATE TABLE `tb_node` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键Id',
`region_id` int(11) NOT NULL COMMENT '区域Id',
`partner_id` int(11) NOT NULL COMMENT '合作商Id',
`node_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '区域名称',
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '详细地址',
`business_type` int(11) NULL DEFAULT NULL COMMENT '商圈类型',
`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`create_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '创建人',
`update_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '更新人',
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
INDEX `区域Id`(`region_id`) USING BTREE,
INDEX `合作商Id`(`partner_id`) USING BTREE,
CONSTRAINT `区域Id` FOREIGN KEY (`region_id`) REFERENCES `tb_region` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `合作商Id` FOREIGN KEY (`partner_id`) REFERENCES `tb_partner` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '点位表' ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
在ruoYi的前后端分离框架中,捕获异常:
package com.dkd.framework.web.exception;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import com.dkd.common.constant.HttpStatus;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.exception.DemoModeException;
import com.dkd.common.exception.ServiceException;
import com.dkd.common.utils.StringUtils;
/**
* 全局异常处理器
*
* @author ruoyi
*/
@RestControllerAdvice
public class GlobalExceptionHandler
{
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 数据完整性异常
*/
@ExceptionHandler(DataIntegrityViolationException.class)
public AjaxResult handleDataIntegrityViolationException(DataIntegrityViolationException e)
{
log.error(e.getMessage(), e);
if (StringUtils.containsIgnoreCase(e.getMessage(), "Duplicate entry"))
{
return AjaxResult.error("数据已存在");
}
if(StringUtils.containsIgnoreCase(e.getMessage(), "foreign")){
return AjaxResult.error("无法删除,有其他数据引用!");
}
return AjaxResult.error("数据完整性异常,请检查数据是否完整");
}
}
再次点击删除: