add 增加 Mybatis 全局异常处理 开启多数据源切换 严格模式 找不到数据源报错
This commit is contained in:
parent
5d3e977761
commit
517c5705b6
@ -47,6 +47,8 @@ spring:
|
|||||||
p6spy: true
|
p6spy: true
|
||||||
# 设置默认的数据源或者数据源组,默认值即为 master
|
# 设置默认的数据源或者数据源组,默认值即为 master
|
||||||
primary: master
|
primary: master
|
||||||
|
# 严格模式 匹配不到数据源则报错
|
||||||
|
strict: true
|
||||||
datasource:
|
datasource:
|
||||||
# 主库数据源
|
# 主库数据源
|
||||||
master:
|
master:
|
||||||
|
@ -54,6 +54,8 @@ spring:
|
|||||||
p6spy: false
|
p6spy: false
|
||||||
# 设置默认的数据源或者数据源组,默认值即为 master
|
# 设置默认的数据源或者数据源组,默认值即为 master
|
||||||
primary: master
|
primary: master
|
||||||
|
# 严格模式 匹配不到数据源则报错
|
||||||
|
strict: true
|
||||||
datasource:
|
datasource:
|
||||||
# 主库数据源
|
# 主库数据源
|
||||||
master:
|
master:
|
||||||
|
@ -9,9 +9,10 @@ import com.ruoyi.common.core.domain.R;
|
|||||||
import com.ruoyi.common.exception.DemoModeException;
|
import com.ruoyi.common.exception.DemoModeException;
|
||||||
import com.ruoyi.common.exception.ServiceException;
|
import com.ruoyi.common.exception.ServiceException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.mybatis.spring.MyBatisSystemException;
|
||||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||||
import org.springframework.validation.BindException;
|
|
||||||
import org.springframework.dao.DuplicateKeyException;
|
import org.springframework.dao.DuplicateKeyException;
|
||||||
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
@ -82,6 +83,21 @@ public class GlobalExceptionHandler {
|
|||||||
return R.fail("数据库中已存在该记录,请联系管理员确认");
|
return R.fail("数据库中已存在该记录,请联系管理员确认");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mybatis系统异常 通用处理
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(MyBatisSystemException.class)
|
||||||
|
public R<Void> 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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 业务异常
|
* 业务异常
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user