专业编程基础技术教程

网站首页 > 基础教程 正文

mybatis中Error attempting to get column from result set处理方案

ccvgpt 2024-07-26 00:29:22 基础教程 58 ℃

对于服务端开发而言,持久层技术使用最多的莫过于mybatis,今天就来聊一个出现频率最高的异常信息:Error attempting to get column ‘xx‘ from result set.在mybatis框架刚入门学习或是平常处理业务问题的时候大概率都会碰到,今天就聊一下这个异常如何产生以及如何处理。

先说一下此报错信息的出处:BaseTypeHandler.java中getResult

mybatis中Error attempting to get column from result set处理方案

public T getResult(ResultSet rs, String columnName) throws SQLException {
    try {
    // 结果集中对数据库字段与实体类属性进行映射处理
    return getNullableResult(rs, columnName);
    } catch (Exception e) {
    throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e);
    }
}

此异常之前还会有异常,也就是说该异常会有多种原因导致,也就对应了该异常出现的解决方案会有多种.下面说一下常见的原因:

1.封装集合中字段名与数据库列名不一致,处理方案就是检查是否一致以及是否有该字段;

2.使用lombok或是其他操作导致没有无参构造或是无get/set;处理方式就是补全get/set;

3.使用Druid.因为版本问题导致对于时间类型LocalDateTime.class处理异常;此种处理方案:不使用Druid或是使用其他数据库连接源进行替换或是Druid版本升级.

这里说一下第三种异常处理方案的处理定位处理过程.

问题场景描述

按照日期查询符合条件的课程记录信息,实体类中时间字段:startTime(格式为YYYY-MM-dd HH:mm:ss),数据库中字段start_time,字段类型dateTime.

堆栈异常信息(篇幅原因删减部分堆栈信息):

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set. Cause: java.sql.SQLFeatureNotSupportedException
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set. Cause: java.sql.SQLFeatureNotSupportedException
at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:87)
... 73 more
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set. Cause: java.sql.SQLFeatureNotSupportedException
at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85)
... 99 more
Caused by: java.sql.SQLFeatureNotSupportedException
at com.alibaba.druid.pool.DruidPooledResultSet.getObject(DruidPooledResultSet.java:1771)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:69)
at com.sun.proxy.$Proxy190.getObject(Unknown Source)
at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:38)
at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:28)
at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85)
... 101 more

看堆栈信息,首先从最开始的位置开始看,很明显是LocalDateTimeTypeHandler.java中getNullableResult抛出的异常,最早出现的异常信息:java.sql.SQLFeatureNotSupportedException.

先看LocalDateTimeTypeHandler.java中getNullableResult

public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
		return rs.getObject(columnName, LocalDateTime.class);
}

rs.getObject有很多中实现方式,由于使用的是Druid数据库连接源(最初使用的版本是1.0.28),所以直接定位到DruidPooledResultSet.java中getObject:

public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
	throw new SQLFeatureNotSupportedException();
}

按照druid1.0.28翻看源码发现对于日期类型处理说明LocalDateTime.class是不支持的,直接抛出SQLFeatureNotSupportedException.修改pom.xml中Druid版本,升级到1.2.1,然后看一下该版本的具体实现:

public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
try {
    return this.rs.getObject(columnLabel, type);
    } catch (Throwable var4) {
    throw this.checkException(var4);
    }
}

说明高版本对LocalDateTime.class处理是支持的,重新测试之后正常!

以上是关于Error attempting to get column ‘xx‘ from result set异常问题的分析以及处理过程,大家如果遇到其他场景也可在评论区进行留言,相互交流技术进步会更快!

—— END ——

作者| 卖柴火的小男孩啊

多年后端开发经验,坚持分享更多java干货内容!

如果你喜欢我的文章,不妨点赞、转发、收藏一下哦!

Tags:

最近发表
标签列表