update 优化 解耦 security 与 mybatis 模块代码依赖
parent
e78994e9c9
commit
6467a0776c
|
|
@ -29,10 +29,6 @@
|
||||||
<artifactId>spring-web</artifactId>
|
<artifactId>spring-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mybatis.spring.boot</groupId>
|
|
||||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>mybatis-plus-annotation</artifactId>
|
<artifactId>mybatis-plus-annotation</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.ruoyi.common.mybatis.handler;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.mybatis.spring.MyBatisSystemException;
|
||||||
|
import org.springframework.dao.DuplicateKeyException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mybatis异常处理器
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class MybatisExceptionHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主键或UNIQUE索引,数据重复异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(DuplicateKeyException.class)
|
||||||
|
public R<Void> handleDuplicateKeyException(DuplicateKeyException e, HttpServletRequest request) {
|
||||||
|
String requestURI = request.getRequestURI();
|
||||||
|
log.error("请求地址'{}',数据库中已存在记录'{}'", requestURI, e.getMessage());
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -10,8 +10,6 @@ import com.ruoyi.common.core.domain.R;
|
||||||
import com.ruoyi.common.core.exception.DemoModeException;
|
import com.ruoyi.common.core.exception.DemoModeException;
|
||||||
import com.ruoyi.common.core.exception.ServiceException;
|
import com.ruoyi.common.core.exception.ServiceException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.mybatis.spring.MyBatisSystemException;
|
|
||||||
import org.springframework.dao.DuplicateKeyException;
|
|
||||||
import org.springframework.validation.BindException;
|
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;
|
||||||
|
|
@ -80,31 +78,6 @@ public class GlobalExceptionHandler {
|
||||||
return R.fail(e.getMessage());
|
return R.fail(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 主键或UNIQUE索引,数据重复异常
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(DuplicateKeyException.class)
|
|
||||||
public R<Void> handleDuplicateKeyException(DuplicateKeyException e, HttpServletRequest request) {
|
|
||||||
String requestURI = request.getRequestURI();
|
|
||||||
log.error("请求地址'{}',数据库中已存在记录'{}'", requestURI, e.getMessage());
|
|
||||||
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…
Reference in New Issue