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

1、前言

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

对应 exceptions 包,如下图所示:

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

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

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

2、源码分析

2.1、exceptions

2.1.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); } } |

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

2.1.2、 PersistenceException

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); } } |

2.1.3 ExceptionFactory

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

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public class ExceptionFactory { private ExceptionFactory() { // Prevent Instantiation } /** * 包装异常成 PersistenceException * * @param message 消息 * @param e 发生的异常 * @return PersistenceException */ public static RuntimeException wrapException(String message, Exception e) { return new PersistenceException(ErrorContext.instance().message(message).cause(e).toString(), e); } } |

2.1.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); } } |

2.2、parsing

2.2.1、 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); } } |

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 源码分析(四):反射模块

相关推荐
爱笑的源码基地31 分钟前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
憧憬成为web高手31 分钟前
皮卡丘靶场速通--sql 2
数据库·sql·mybatis
段一凡-华北理工大学44 分钟前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe1 小时前
C++——多态
开发语言·c++
心平气和量大福大2 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
莫逸风3 小时前
【AgentScope 2.0】 0. 学习指南
java·llm·agent·agentscope
从零开始的代码生活_3 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸3 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
z123456789864 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
GIS阵地4 小时前
QgsSingleBandPseudoColorRenderer 完整详解(QGIS 3.40.13 C++)
开发语言·前端·c++·qt·qgis