赞
踩
上一篇我们解了Mybatis解析器模块,本篇我们来了解反射模块。本文,我们来分享 MyBatis 的异常模块。
对应 exceptions
包,如下图所示:
在 MyBatis源码分析(二):项目结构 中,简单介绍了这个模块:
定义了 MyBatis 专有的 PersistenceException 和 TooManyResultsException 异常。
实际上,MyBatis 不仅仅在 exceptions
包下有异常,在其他包下也有异常,整体如下类图:
exceptions
包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);
- }
-
- }

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

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

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

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

实际上,我们会看到其他包,会和 parsing
包一样,都会定义其独有的异常类。但是,代码都是相同的。所以,这里就简单整理如下:
reflection
包:ReflectionExceptionlogging
包:LogExceptionbuilder
包:BuilderException、IncompleteElementExceptionscripting
包:ScriptingExceptionbinding
包:BindingExceptiontype
包:TypeExceptionsession
包:SqlSessionExceptioncache
包:CacheExceptiontransaction
包:TransactionExceptiondatasource
包:DataSourceExceptionexecutor
包:ResultMapException、ExecutorException、BatchExecutorExceptionplugin
包:PluginException本文我讲解了Mybatis的异常模块,该模块的代码逻辑还是比较清晰简单,大家一定要多多调试哦!
Mybatis源码解析传送门:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。