当前位置:   article > 正文

精尽 MyBatis 源码分析 —— 异常模块_mybatis 异常源码

mybatis 异常源码

1.概述

定义了 MyBatis 专有的 PersistenceException 和 TooManyResultsException 异常。

2. exceptions 包

2.1 IbatisException

org.apache.ibatis.exceptions.IbatisException ,实现 RuntimeException 类,IBatis 的异常基类。代码如下:

@Deprecated
public class IbatisException extends RuntimeException {

  private static final long serialVersionUID = 3880206998166270511L;

  public IbatisException() {
    super();
  }

  public IbatisException(String message) {
    super(message);
  }

  public IbatisException(String message, Throwable cause) {
    super(message, cause);
  }

  public IbatisException(Throwable cause) {
    super(cause);
  }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

实际上,IbatisException 已经在 2015 年被废弃,取代它的是 PersistenceException 类。

2.2 IbatisException

org.apache.ibatis.exceptions.PersistenceException ,继承 IbatisException 类,目前 MyBatis 真正的异常基类。代码如下:

public class PersistenceException extends IbatisException {

  private static final long serialVersionUID = -7537395265357977271L;

  public PersistenceException() {
    super();
  }

  public PersistenceException(String message) {
    super(message);
  }

  public PersistenceException(String message, Throwable cause) {
    super(message, cause);
  }

  public PersistenceException(Throwable cause) {
    super(cause);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
2.3 ExceptionFactory

org.apache.ibatis.exceptions.ExceptionFactory ,异常工厂。代码如下:

public class ExceptionFactory {

  private ExceptionFactory() {
    // Prevent Instantiation
  }

  public static RuntimeException wrapException(String message, Exception e) {
    return new PersistenceException(ErrorContext.instance().message(message).cause(e).toString(), e);
  }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
2.4 TooManyResultsException

org.apache.ibatis.exceptions.TooManyResultsException ,继承 PersistenceException 类,查询返回过多结果的异常。期望返回一条,实际返回了多条。代码如下:

public class TooManyResultsException extends PersistenceException {

  private static final long serialVersionUID = 8935197089745865786L;

  public TooManyResultsException() {
    super();
  }

  public TooManyResultsException(String message) {
    super(message);
  }

  public TooManyResultsException(String message, Throwable cause) {
    super(message, cause);
  }

  public TooManyResultsException(Throwable cause) {
    super(cause);
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
2.5 ParsingException

org.apache.ibatis.parsing.ParsingException ,继承 PersistenceException 类,解析异常。代码如下:

public class ParsingException extends PersistenceException {
  private static final long serialVersionUID = -176685891441325943L;

  public ParsingException() {
    super();
  }

  public ParsingException(String message) {
    super(message);
  }

  public ParsingException(String message, Throwable cause) {
    super(message, cause);
  }

  public ParsingException(Throwable cause) {
    super(cause);
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

3. 其它包

实际上,我们会看到其他包,会和 parsing 包一样,都会定义其独有的异常类。但是,代码都是相同的。所以,这里就简单整理如下:

reflection 包:ReflectionException
logging 包:LogException
builder 包:BuilderException、IncompleteElementException
scripting 包:ScriptingException
binding 包:BindingException
type 包:TypeException
session 包:SqlSessionException
cache 包:CacheException
transaction 包:TransactionException
datasource 包:DataSourceException
executor 包:ResultMapException、ExecutorException、BatchExecutorException
plugin 包:PluginException

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/215624?site
推荐阅读
相关标签
  

闽ICP备14008679号