jdk LocalDateTime mybatis 空指针解决办法

在使用MyBatis框架与JDK8的LocalDateTime进行数据交互时,遇到数据库timestamp字段为null导致的NullPointerException。本文详细分析了问题原因,并提供了解决方案,即升级MySQL驱动版本至5.1.47,该版本增加了对null值的判断,避免了异常抛出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入jar包:

1. mysql.mysql-connector-java:5.1.39

2. org.mybatis.mybatis:3.5.2

3. org.mybatis.mybatis-spring:2.0.2

在项目中的mybats升级使用了jdk8的LocalDateTime等后,数据库timesstamp字段有的记录是null,导致查询时出现下面错误

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'UPDATE_TIME' from result set.  Cause: java.lang.NullPointerException
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:78)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
	at com.sun.proxy.$Proxy154.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
	at com.sun.proxy.$Proxy181.find(Unknown Source)
	at cn.enn.ygego.sunny.sv.service.online.impl.LogisticsBrandServiceImpl.list(LogisticsBrandServiceImpl.java:67)
	at cn.enn.ygego.sunny.sv.controller.online.LogisticsBrandController.list(LogisticsBrandController.java:49)
	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)

不能把null转换为LocalDateTime。通过跟踪代码,发现问题出在mysql的驱动上,JDBC42ResultSet的getObject代码如下:

public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
        if (type == null) {
            throw SQLError.createSQLException("Type parameter can not be null", "S1009", this.getExceptionInterceptor());
        } else if (type.equals(LocalDate.class)) {
            return type.cast(this.getDate(columnIndex).toLocalDate());
        } else if (type.equals(LocalDateTime.class)) {
            return type.cast(this.getTimestamp(columnIndex).toLocalDateTime());
        } else if (type.equals(LocalTime.class)) {
            return type.cast(this.getTime(columnIndex).toLocalTime());
        } else {
            if (type.equals(OffsetDateTime.class)) {
                try {
                    return type.cast(OffsetDateTime.parse(this.getString(columnIndex)));
                } catch (DateTimeParseException var5) {
                }
            } else if (type.equals(OffsetTime.class)) {
                try {
                    return type.cast(OffsetTime.parse(this.getString(columnIndex)));
                } catch (DateTimeParseException var4) {
                }
            }

            return super.getObject(columnIndex, type);
        }
    }
return type.cast(this.getTimestamp(columnIndex).toLocalDateTime());代码中this.getTimestamp(columnIndex)返回null,再次执行toLocalDateTime(),当然报错。

解决方式升级mysql驱动,我升级到5.1.47,其他版本没有测试,在5.1.47中代码如下:

public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
        if (type == null) {
            throw SQLError.createSQLException("Type parameter can not be null", "S1009", this.getExceptionInterceptor());
        } else if (type.equals(LocalDate.class)) {
            Date date = this.getDate(columnIndex);
            return date == null ? null : type.cast(date.toLocalDate());
        } else if (type.equals(LocalDateTime.class)) {
            Timestamp timestamp = this.getTimestamp(columnIndex);
            return timestamp == null ? null : type.cast(timestamp.toLocalDateTime());
        } else if (type.equals(LocalTime.class)) {
            Time time = this.getTime(columnIndex);
            return time == null ? null : type.cast(time.toLocalTime());
        } else {
            String string;
            if (type.equals(OffsetDateTime.class)) {
                try {
                    string = this.getString(columnIndex);
                    return string == null ? null : type.cast(OffsetDateTime.parse(string));
                } catch (DateTimeParseException var5) {
                }
            } else if (type.equals(OffsetTime.class)) {
                try {
                    string = this.getString(columnIndex);
                    return string == null ? null : type.cast(OffsetTime.parse(string));
                } catch (DateTimeParseException var4) {
                }
            }

            return super.getObject(columnIndex, type);
        }
    }

明显看到增加null判断。

 

### JDK 17 Compatible MyBatis Version MyBatis通用Mapper项目已经更新至支持JDK 17的版本,并且其设计目标之一就是简化MyBatis的开发流程,提供便捷的增删改查功能[^1]。因此,在选择与JDK 17兼容的MyBatis版本时,建议关注官方发布的最新稳定版。 通常情况下,MyBatis的核心库本身对于较新的JDK版本具有良好的向后兼容性。然而,为了确保最佳实践并避免潜在问题,推荐使用MyBatis 3.5.x系列中的较高版本(如3.5.9及以上),这些版本经过测试能够很好地适配JDK 17环境[^4]。 另外需要注意的是,如果采用Spring Boot 3作为集成框架,则应进一步确认所选MyBatis版本与其之间的相互依赖关系是否一致。因为Spring Boot 3默认基于JDK 17构建,并可能对某些第三方组件提出了特定的要求或优化建议。 以下是配置文件中关于Maven依赖的一个示例片段: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version><!-- 或更高 --> </dependency> <!-- 如果需要整合Spring --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.0</version><!-- 需要验证具体版本号 --> </dependency> ``` #### 关于其他相关技术栈的选择提示 当实际操作过程中遇到诸如依赖冲突等问题时,务必仔细核对各个组成部分间的版本匹配情况。例如,在早期的一些教程里提到过Idea配合Spring MVC加MyBatis再辅以较低版本JDK (像jdk1.8) 的组合方案[^2];但随着时代发展以及新技术普及率提高,现在更倾向于选用现代化工具链来满足日益增长的应用需求。 最后提醒一点,尽管上述内容提供了较为清晰的方向指导,但在正式实施前仍需参照官方文档或者社区活跃讨论区获取最精确的信息。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值