diff --git a/config/dev/datasource.yml b/config/dev/datasource.yml index 56644b81..7d0989f8 100644 --- a/config/dev/datasource.yml +++ b/config/dev/datasource.yml @@ -23,6 +23,8 @@ spring: p6spy: true # 开启seata代理,开启后默认每个数据源都代理,如果某个不需要代理可单独关闭 seata: true + # 严格模式 匹配不到数据源则报错 + strict: true druid: initial-size: 5 min-idle: 5 diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java index 8e9a2519..fefb3a3d 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java @@ -10,6 +10,7 @@ import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.exception.DemoModeException; import com.ruoyi.common.core.exception.ServiceException; import lombok.extern.slf4j.Slf4j; +import org.mybatis.spring.MyBatisSystemException; import org.springframework.dao.DuplicateKeyException; import org.springframework.validation.BindException; import org.springframework.web.HttpRequestMethodNotSupportedException; @@ -89,6 +90,21 @@ public class GlobalExceptionHandler { return R.fail("数据库中已存在该记录,请联系管理员确认"); } + /** + * Mybatis系统异常 通用处理 + */ + @ExceptionHandler(MyBatisSystemException.class) + public R handleCannotFindDataSourceException(MyBatisSystemException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + String message = e.getMessage(); + if (message.contains("CannotFindDataSourceException")) { + log.error("请求地址'{}', 未找到数据源", requestURI); + return R.fail("未找到数据源,请联系管理员确认"); + } + log.error("请求地址'{}', Mybatis系统异常", requestURI, e); + return R.fail(message); + } + /** * 业务异常 */