当前位置:   article > 正文

MyBatis 源码分析(五):异常模块

MyBatis 源码分析(五):异常模块

1、前言

上一篇我们解了Mybatis解析器模块,本篇我们来了解反射模块。本文,我们来分享 MyBatis 的异常模块。

对应 exceptions 包,如下图所示:

29d4c9c730d11fa64c68b8056a8622fd.png

在 MyBatis源码分析(二):项目结构 中,简单介绍了这个模块:

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

实际上,MyBatis 不仅仅在 exceptions 包下有异常,在其他包下也有异常,整体如下类图

6acb070c543723744769dc1c6da9bb7f.png

2、源码分析

2.1、exceptions 包

2.1.1 IbatisException

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

  1. @Deprecated
  2. public class IbatisException extends RuntimeException {
  3. private static final long serialVersionUID = 3880206998166270511L;
  4. public IbatisException() {
  5. super();
  6. }
  7. public IbatisException(String message) {
  8. super(message);
  9. }
  10. public IbatisException(String message, Throwable cause) {
  11. super(message, cause);
  12. }
  13. public IbatisException(Throwable cause) {
  14. super(cause);
  15. }
  16. }

 

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

2.1.2、 PersistenceException

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

  1. public class PersistenceException extends IbatisException {
  2. private static final long serialVersionUID = -7537395265357977271L;
  3. public PersistenceException() {
  4. super();
  5. }
  6. public PersistenceException(String message) {
  7. super(message);
  8. }
  9. public PersistenceException(String message, Throwable cause) {
  10. super(message, cause);
  11. }
  12. public PersistenceException(Throwable cause) {
  13. super(cause);
  14. }
  15. }

 

2.1.3 ExceptionFactory

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

  1. public class ExceptionFactory {
  2. private ExceptionFactory() {
  3. // Prevent Instantiation
  4. }
  5. /**
  6. * 包装异常成 PersistenceException
  7. *
  8. * @param message 消息
  9. * @param e 发生的异常
  10. * @return PersistenceException
  11. */
  12. public static RuntimeException wrapException(String message, Exception e) {
  13. return new PersistenceException(ErrorContext.instance().message(message).cause(e).toString(), e);
  14. }
  15. }

 

2.1.4、TooManyResultsException

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

  1. public class TooManyResultsException extends PersistenceException {
  2. private static final long serialVersionUID = 8935197089745865786L;
  3. public TooManyResultsException() {
  4. super();
  5. }
  6. public TooManyResultsException(String message) {
  7. super(message);
  8. }
  9. public TooManyResultsException(String message, Throwable cause) {
  10. super(message, cause);
  11. }
  12. public TooManyResultsException(Throwable cause) {
  13. super(cause);
  14. }
  15. }

 

2.2、parsing 包

2.2.1、 ParsingException

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

  1. public class ParsingException extends PersistenceException {
  2. private static final long serialVersionUID = -176685891441325943L;
  3. public ParsingException() {
  4. super();
  5. }
  6. public ParsingException(String message) {
  7. super(message);
  8. }
  9. public ParsingException(String message, Throwable cause) {
  10. super(message, cause);
  11. }
  12. public ParsingException(Throwable cause) {
  13. super(cause);
  14. }
  15. }

 

2.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

3、总结

本文我讲解了Mybatis的异常模块,该模块的代码逻辑还是比较清晰简单,大家一定要多多调试哦!


Mybatis源码解析传送门:

MyBatis源码分析(一):搭建调试环境

MyBatis源码分析(二):项目结构

MyBatis源码分析(三):解析器模块

MyBatis 源码分析(四):反射模块

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/215622
推荐阅读
相关标签
  

闽ICP备14008679号