mysql 字段 datetime值为null 与 java LocaldateTime 映射空指针问题

  • Post author:
  • Post category:java




[解决] mysql 字段 datetime值为null 与 java LocaldateTime 映射空指针问题

报错内容如下:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column '**' 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)

问题:数据库中dateTime字段为null

在这里插入图片描述

通过追踪代码发现 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);
        }
    }

type.cast方法:

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


解决办法:

升级mysql驱动版本为5.1.47(mysql-connector-java)

5.1.47的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)) {
            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值判断


原文链接