Spring整合MyBatis时异常处理

1. java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport

解决方式:添加spring-tx依赖,注意版本要一致,否则出现异常
Caused by: java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.3.14.RELEASE</version>
</dependency>

2. org.springframework.beans.MethodInvocationException: Property ‘dataSource’ threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy

解决方式:添加spring-jdbc依赖

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>4.3.14.RELEASE</version>
</dependency>

3. Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ‘;serverTimezone=CTT’.

解决方式:检查数据库连接url的格式

// 正确格式
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT

4. Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatement.isClosed()Z is abstract

我的c3p0数据库连接池依赖为:
在这里插入图片描述
解决方式:将依赖包改为

<!-- 数据库连接池 -->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

总结

项目运行不起来,第一时间确实很慌,尤其是被大量看不懂的英文日志刷屏时,因此找准错误信息很重要,以我现在的经验来说,有两种方式:

  1. 从报错开始位置找
  2. 从报错结束位置找

然后仔细阅读并大胆猜测,推断报错原因

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMybatis 整合后,可以通过 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、付费专栏及课程。

余额充值