Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理

Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理

结论

在全局异常处理类中添加MyBatisSystemException即可单独对MyBatis中和数据库操作相关异常操作进行全局处理,同时屏蔽sql内容,只返回文字 “服务错误,请联系系统管理员” 给前端。

@Slf4j
@ControllerAdvice
public class ExceptionHandlerAdvice {

    /**
     * Add before 2024-05-20
     * Sql查询失败在spring的包装下会统一抛出非受检异常,单独捕获,防止sql语句被返回给前端
     * update by 2024-05-20 由于org.springframework.jdbc在sql查询报错后会抛出BadSqlGrammarException异常,
     * 往上追溯,发现MyBatisSystemException和BadSqlGrammarException有共同父类异常DataAccessException,
     * 故将原始捕获MyBatisSystemException修改为捕获共同父类DataAccessException异常,防止sql语句被返回给前端
     */
    @ResponseBody
    @ExceptionHandler(DataAccessException.class)
    public Object handleBindException(HttpServletRequest req, MyBatisSystemException e) {
        String path = "http://"+req.getRemoteAddr()+":"+req.getServerPort() + req.getRequestURI();
        log.error("访问 "+path +"报错,报错信息为: "+ e.getMessage(), e);
        return new BaseResult<>(CodeEnum.E500, false, "服务错误,请联系系统管理员。");
    }
    
    //拦截所有Exception,展示Error页面
    @ResponseBody
    @ExceptionHandler({Exception.class})
    public BaseResult errorHandler(HttpServletRequest req, Exception e) {
        String path = "http://"+req.getRemoteAddr()+":"+req.getServerPort() + req.getRequestURI();
        log.error("访问 "+path +"报错,报错信息为: "+ e.getMessage(), e);
        return new BaseResult<>(CodeEnum.E500, false, e.getMessage());
    }
}

1 java异常体系

在这里插入图片描述

1.Throwable

所有的异常都是Throwable的直接或者间接子类。Throwable有两个直接子类,Error和Exception。

2.Error

Error是错误,对于所有的编译时期的错误以及系统错误都是通过Error抛出的。这些错误表示故障发生于虚拟机自身、或者发生在虚拟机试图执行应用时,如Java虚拟机运行错误(Virtual MachineError)、类定义错误(NoClassDefFoundError)等。这些错误是不可查的,因为它们在应用程序的控制和处理能力之 外,而且绝大多数是程序运行时不允许出现的状况。对于设计合理的应用程序来说,即使确实发生了错误,本质上也不应该试图去处理它所引起的异常状况。在 Java中,错误通过Error的子类描述。

3.Exception

它规定的异常是程序本身可以处理的异常。异常和错误的区别是,异常是可以被处理的,而错误是没法处理的。

4.Checked Exception【受检异常】

可检查的异常,这是编码时非常常用的,所有checked exception都是需要在代码中处理的。它们的发生是可以预测的,正常的一种情况,可以合理的处理。例如IOException。

5.Unchecked Exception【非受检异常】

RuntimeException及其子类都是unchecked exception。比如NPE空指针异常,除数为0的算数异常ArithmeticException等等,这种异常是运行时发生,无法预先捕捉处理的。Error也是unchecked exception,也是无法预先处理的。

参考:https://juejin.cn/post/6965407291260534820

2 Spring框架异常处理

Spring 提供方便的 API 把具体技术相关的异常(比如由JDBOHibernate or JDO 抛出的)转化为一致的 unchecked 异常。

3 定位Spring框架转化为哪种unchecked异常

3.1 捕获RuntimeException定位Spring框架转化抛出的异常类

直接在ExceptionHandlerAdvice中捕获RuntimeException,然后DEBUG,查看异常class类型,发现都是继承自MyBatisSystemException

在这里插入图片描述

3.2 进一步查看包名判断

进一步查看包名发现为org.springframework.dao,基本可以判定捕获MyBatisSystemException可以实现要求

package org.mybatis.spring;

import org.springframework.dao.UncategorizedDataAccessException;

public class MyBatisSystemException extends UncategorizedDataAccessException {
    private static final long serialVersionUID = -5284728621670758939L;

    public MyBatisSystemException(Throwable cause) {
        super((String)null, cause);
    }
}

3.3 识别MyBatisSystemException下级实现

MyBatisSystemException目前没有下级实现类

3.3 识别MyBatisSystemException继承实现

可以看到继承父类均为abstract修饰,一直到NestedRuntimeException继承RuntimeException。则已经找到MyBatisSystemException的所有上级继承父类,进一步确认MyBatisSystemException符合作为全局异常捕获ExceptionHandler的最上级实现异常类型,而不会漏异常捕获。

package org.springframework.dao;

import org.springframework.lang.Nullable;

public abstract class UncategorizedDataAccessException extends NonTransientDataAccessException {
    public UncategorizedDataAccessException(@Nullable String msg, @Nullable Throwable cause) {
        super(msg, cause);
    }
}
package org.springframework.dao;

import org.springframework.lang.Nullable;

public abstract class NonTransientDataAccessException extends DataAccessException {
    public NonTransientDataAccessException(String msg) {
        super(msg);
    }

    public NonTransientDataAccessException(@Nullable String msg, @Nullable Throwable cause) {
        super(msg, cause);
    }
}
package org.springframework.dao;

import org.springframework.core.NestedRuntimeException;
import org.springframework.lang.Nullable;

public abstract class DataAccessException extends NestedRuntimeException {
    public DataAccessException(String msg) {
        super(msg);
    }

    public DataAccessException(@Nullable String msg, @Nullable Throwable cause) {
        super(msg, cause);
    }
}
package org.springframework.core;

import org.springframework.lang.Nullable;

public abstract class NestedRuntimeException extends RuntimeException {
    private static final long serialVersionUID = 5439915454935047936L;

    public NestedRuntimeException(String msg) {
        super(msg);
    }

    public NestedRuntimeException(@Nullable String msg, @Nullable Throwable cause) {
        super(msg, cause);
    }

    @Nullable
    public String getMessage() {
        return NestedExceptionUtils.buildMessage(super.getMessage(), this.getCause());
    }

    @Nullable
    public Throwable getRootCause() {
        return NestedExceptionUtils.getRootCause(this);
    }

    public Throwable getMostSpecificCause() {
        Throwable rootCause = this.getRootCause();
        return (Throwable)(rootCause != null ? rootCause : this);
    }

    public boolean contains(@Nullable Class<?> exType) {
        if (exType == null) {
            return false;
        } else if (exType.isInstance(this)) {
            return true;
        } else {
            Throwable cause = this.getCause();
            if (cause == this) {
                return false;
            } else if (cause instanceof NestedRuntimeException) {
                return ((NestedRuntimeException)cause).contains(exType);
            } else {
                while(cause != null) {
                    if (exType.isInstance(cause)) {
                        return true;
                    }

                    if (cause.getCause() == cause) {
                        break;
                    }

                    cause = cause.getCause();
                }

                return false;
            }
        }
    }

    static {
        NestedExceptionUtils.class.getName();
    }
}
 }

                    cause = cause.getCause();
                }

                return false;
            }
        }
    }

    static {
        NestedExceptionUtils.class.getName();
    }
}

3.4 识别BadSqlGrammarException继承实现

可以看到BadSqlGrammarException的父类异常,一直到DataAccessException继承NestedRuntimeException,再继承RuntimeException。则已经找到BadSqlGrammarException的所有上级继承父类,进一步确认DataAccessException符合全局捕获MyBatisSystemException和BadSqlGrammarException全局异常捕获ExceptionHandler的最上级实现异常类型,而不会漏异常捕获。

package org.springframework.jdbc;

import java.sql.SQLException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;

public class BadSqlGrammarException extends InvalidDataAccessResourceUsageException {
    private final String sql;

    public BadSqlGrammarException(String task, String sql, SQLException ex) {
        super(task + "; bad SQL grammar [" + sql + "]", ex);
        this.sql = sql;
    }

    public SQLException getSQLException() {
        return (SQLException)this.getCause();
    }

    public String getSql() {
        return this.sql;
    }
}
package org.springframework.dao;

public class InvalidDataAccessResourceUsageException extends NonTransientDataAccessException {
    public InvalidDataAccessResourceUsageException(String msg) {
        super(msg);
    }

    public InvalidDataAccessResourceUsageException(String msg, Throwable cause) {
        super(msg, cause);
    }
}
package org.springframework.dao;

import org.springframework.lang.Nullable;

public abstract class NonTransientDataAccessException extends DataAccessException {
    public NonTransientDataAccessException(String msg) {
        super(msg);
    }

    public NonTransientDataAccessException(@Nullable String msg, @Nullable Throwable cause) {
        super(msg, cause);
    }
}
package org.springframework.dao;

import org.springframework.core.NestedRuntimeException;
import org.springframework.lang.Nullable;

public abstract class DataAccessException extends NestedRuntimeException {
    public DataAccessException(String msg) {
        super(msg);
    }

    public DataAccessException(@Nullable String msg, @Nullable Throwable cause) {
        super(msg, cause);
    }
}
  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring 与 Mybatis 整合后,可以通过 AOP 切面来捕获 Mybatis 执行 SQL 时的异常。 具体步骤如下: 1. 创建一个切面类,使用 @Aspect 注解标注为切面类,并在类中定义一个方法用于捕获异常。 2. 在切面方法上使用 @AfterThrowing 注解指定需要捕获异常类型和处理方法。 3. 在 Spring 配置文件中配置切面类和异常处理方法。 以下是示例代码: ```java @Aspect public class SqlExceptionAspect { // 定义切入点,这里选择 Mybatis 的 Executor 类 @Pointcut("execution(* org.apache.ibatis.executor.Executor.*(..))") public void pointcutExecutor() {} // 异常处理方法 @AfterThrowing(pointcut = "pointcutExecutor()", throwing = "ex") public void handleSqlException(Exception ex) { if (ex instanceof SQLException) { // 处理 SQL 异常 System.out.println("捕获SQL 异常:" + ex.getMessage()); } else { // 处理其他异常 System.out.println("捕获到其他异常:" + ex.getMessage()); } } } ``` 在 Spring 配置文件中添加以下代码: ```xml <!-- 配置切面类 --> <bean id="sqlExceptionAspect" class="com.example.SqlExceptionAspect"/> <!-- 配置 AOP 自动代理 --> <aop:aspectj-autoproxy/> <!-- 配置切面 --> <aop:config> <aop:aspect ref="sqlExceptionAspect"> <aop:after-throwing method="handleSqlException" throwing="ex" pointcut="execution(* org.apache.ibatis.executor.Executor.*(..))"/> </aop:aspect> </aop:config> ``` 这样就可以在执行 Mybatis SQL捕获异常了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值