【MybatisPlus】数据库的datetime类型字段为空的时候,报错空指针?
一、发现经历
事情是这样的,我今天本来要演示系统,就去前端同学的页面上点一点。不小心点到了其他同事编写的服务,然后界面就报错了。这给我吓得,这还能演示吗这。然后,我就去服务器查看了一下日志,发现了如下景象:

看到这景象啊,我第一件事情就是查看堆栈,也没找到自己写的代码啊,好好的咋就报错了。
于是,我第一件事情,复制报错信息找到百度网站。复制粘贴,往上一怼!好家伙,竟然找不到一个和我症状一样的。
那我只能自己处理了。
二、问题定位
从堆栈信息定位一个问题的位置其实是很简单的,但是要明白为什么错误,还是得多看一会儿的。
我定睛看了一阵子,发现了
at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:38)
这一行报错,于是我就跟进源码查看。这个源码,写的也简单。
1 /**
2 * Copyright 2009-2019 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.ibatis.type;
17
18 import java.sql.CallableStatement;
19 import java.sql.PreparedStatement;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.time.LocalDateTime;
23
24 /**
25 * @since 3.4.5
26 * @author Tomas Rohovsky
27 */
28 public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {
29
30 @Override
31 public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType)
32 throws SQLException {
33 ps.setObject(i, parameter);
34 }
35
36 @Override
37 public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
38 return rs.getObject(columnName, LocalDateTime.class);
39 }
40
41 @Override
42 public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
43 return rs.getObject(columnIndex, LocalDateTime.class);
44 }
45
46 @Override
47 public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
48 return cs.getObject(columnIndex, LocalDateTime.class);
49 }
50 }
一共也就这么几行。报错的是:
public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getObject(columnName, LocalDateTime.class);
}
这个方法。这里面发生了空指针。
这时候,我发现还是最细的堆栈信息。于是,我到最细的那一层。

也就是这一行。
看到这里,我明白了。实际上就是
getTimestamp(columnIndex)
这个获取时间的时候,数据库的submit_time字段没有值,然后cast的时候,发生了空指针。
三、问题解决
弄清了病症,也就好下药治疗了。我脑中浮现出两个想法:
1、让submit_time有值
这不简单吗,没值的时候有问题,我让你有值不就完事了。哈哈。开个玩笑,要结合业务处理的哈。这种方法明显不可取。
2、重写LocalDateTimeTypeHandler这个类,然后添加空指针判断。
这个方法不错,可以解决实际问题。
于是,我查看了其他类型的处理。
1 /**
2 * Copyright 2009-2018 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.ibatis.type;
17
18 import java.sql.CallableStatement;
19 import java.sql.PreparedStatement;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22
23 /**
24 * @author Clinton Begin
25 */
26 public class DoubleTypeHandler extends BaseTypeHandler<Double> {
27
28 @Override
29 public void setNonNullParameter(PreparedStatement ps, int i, Double parameter, JdbcType jdbcType)
30 throws SQLException {
31 ps.setDouble(i, parameter);
32 }
33
34 @Override
35 public Double getNullableResult(ResultSet rs, String columnName)
36 throws SQLException {
37 double result = rs.getDouble(columnName);
38 return result == 0 && rs.wasNull() ? null : result;
39 }
40
41 @Override
42 public Double getNullableResult(ResultSet rs, int columnIndex)
43 throws SQLException {
44 double result = rs.getDouble(columnIndex);
45 return result == 0 && rs.wasNull() ? null : result;
46 }
47
48 @Override
49 public Double getNullableResult(CallableStatement cs, int columnIndex)
50 throws SQLException {
51 double result = cs.getDouble(columnIndex);
52 return result == 0 && cs.wasNull() ? null : result;
53 }
54
55 }

看了Double的类型处理【1、wasNull设置标志位 2、获取的外面通过标志位判空】,我又看了Integer的类型处理。。。
1 /**
2 * Copyright 2009-2018 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.ibatis.type;
17
18 import java.sql.CallableStatement;
19 import java.sql.PreparedStatement;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22
23 /**
24 * @author Clinton Begin
25 */
26 public class IntegerTypeHandler extends BaseTypeHandler<Integer> {
27
28 @Override
29 public void setNonNullParameter(PreparedStatement ps, int i, Integer parameter, JdbcType jdbcType)
30 throws SQLException {
31 ps.setInt(i, parameter);
32 }
33
34 @Override
35 public Integer getNullableResult(ResultSet rs, String columnName)
36 throws SQLException {
37 int result = rs.getInt(columnName);
38 return result == 0 && rs.wasNull() ? null : result;
39 }
40
41 @Override
42 public Integer getNullableResult(ResultSet rs, int columnIndex)
43 throws SQLException {
44 int result = rs.getInt(columnIndex);
45 return result == 0 && rs.wasNull() ? null : result;
46 }
47
48 @Override
49 public Integer getNullableResult(CallableStatement cs, int columnIndex)
50 throws SQLException {
51 int result = cs.getInt(columnIndex);
52 return result == 0 && cs.wasNull() ? null : result;
53 }
54 }

Integer的类型处理也是【1、wasNull设置标志位 2、获取的外面通过标志位判空】。
这真是好家伙,坐不住了。其他的类型都处理了,合着就我用的LocalDateTime没做空指针处理。这哪能行。重写,必须重写。
1 package com.vsofo;
2
3 import org.apache.ibatis.type.LocalDateTimeTypeHandler;
4 import org.springframework.stereotype.Component;
5
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.time.LocalDateTime;
9
10 @Component
11 public class LocalDateTimeTypeHandlerPlus extends LocalDateTimeTypeHandler {
12
13 @Override
14 public LocalDateTime getResult(ResultSet rs, String columnName) throws SQLException {
15 Object object = rs.getObject(columnName);
16 if(object==null){
17 return null;
18 }
19 return super.getResult(rs, columnName);
20 }
21 }
我直接继承,然后重写获取逻辑。
1、先获取数据库的值
2、判空,如果值为空,直接返回。
3、如果,值不为空,进行原来的强转逻辑。
重写之后,问题完美解决了。
四、心得体会
我觉着吧,框架是个好东西,能让我们开发项目事半功倍。但是如果给你埋了一个大雷,也可能让你事倍功半。
所以,我们用框架开发的过程中,应该多用多测多实践。找出影响项目的问题,然后解决它。
分享结束,谢谢大家观看哈!
【MybatisPlus】数据库的datetime类型字段为空的时候,报错空指针?的更多相关文章
- Mysql 数据库date, datetime类型设置0000-00-00默认值(default)报错问题
Mysql 数据库date, datetime类型设置0000-00-00默认值报错问题 现象:MySQL5.7版本之后,date, datetime类型设置默认值"0000-00-00&q ...
- DataTable 数据导入MS ACCESS 数据库中 数字类型字段为空的解决办法
string strSql = "insert into GongCheng (GCSY,GCBH,GCBHOLD,GCMC,GCKCJD,GCJSDW,GCSJDW,GCKCDW,GCSG ...
- Oracle数据库在给表添加字段的sql中用comment报错
原因:不同于mysql,Oracle数据库在添加表字段时不能直接用comment,而是单独写一个sql语句,如下: alter table SYS_USER add SENDMSG_LASTTIME ...
- 如何把datetime类型字段修改为int类型
如何把datetime类型字段修改为int类型 我有一个表为:table1 其中有一个datetime类型的字段 a 现在我想我想把字段a的类型改为int类型 当我执行以下命令时报如下的错误a ...
- C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法
原文:C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法 本人新手,在.Net中写WebAPI的时候,当接口返回的json数据含有日期时间类型的字段时, ...
- 多线程中,ResultSet为空,报错空指针
最近在数据库查询数据时,由于数据量太大,使用了多线程,通过线程池建了好几个线程,然后调用了一个封装好的jdbc查询语句. 结果在多线程中,ResultSet报错空指针. 仔细查阅后,才发现多个线程访问 ...
- 解决Entity Framework中DateTime类型字段异常
从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值 具体的错误原因是:C#中的DateTime类型比SqlServer中的datetime范围大.SqlServe ...
- MSSQL数据库中Text类型字段在PHP中被截断之解 (转)
在PHP中使用了MSSQL数据库,恰巧数据库中又使用了Text类型字段,于是问题产生了.每次从数据库中查询得到的数据总是被莫名的截断,一开始是以为我使用的PHP框架中对字符串的长度有所限制,后来发现这 ...
- 数据库中float类型字段,转化到前端显示,统一保留两位小数
客户的一个需求,mybatis查询到的数据库的数据进行转换,采用TypeHandler<T>的方式.float保留两位精度可以采用DecimalFormat 直接贴上最终的解决代码(事情没 ...
随机推荐
- 008-Java中方法的使用(进阶篇)
目录 一.方法的重载(overload) 一.什么是方法的重载 二.方法执行时的内存变化 一.JVM主要三块内存空间 二.关于栈的数据结构(如图) 三.方法执行过程内存变化(用以下代码演示) 三.方法 ...
- 老学长的TODOLIST
初期: 一.基本算法: (1)枚举(poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法 (4)递推 (5)构造法(poj3295)(这种 ...
- Codeforces Global Round 14 E. Phoenix and Computers
题目链接 点我跳转 题目大意 给定 \(N\) 台电脑,起初每台电脑都是关闭的 现在你可以随意打开电脑,但如果第 \(i-1\).第 \(i+1\) 台电脑是开启的,则第 \(i\) 台电脑也会自动开 ...
- 使用netty实现socks5协议
一.socks5协议简介 SOCKS是一种网络传输协议,主要用于客户端与外网服务器之间通讯的中间传递. SOCKS是"SOCKetS"的缩写[注 1]. 当防火墙后的客户端要访问外 ...
- vue.js中使用set方法 this.$set
vue教程中有这样一个注意事项: 第一种具体情况如下: 运行结果: 当利用索引改变数组某一项时,页面不会刷新.解决方法如下: 运行结果: 三种方式都可以解决,使用Vue.set.vm.$set()或者 ...
- POJ2118基础矩阵快速幂
题意: an=Σ1<=i<=kan-ibi mod 10 000 for n >= k,题意看了好久才懂,有点蛋疼啊, 这个题目要是能看懂题意就简单了,先给你k,然后给 ...
- POJ2195费用流+BFS建图
题意: 给你一个n*m的地图,上面有w个人,和w个房子,每个人都要进房子,每个房子只能进一个人,问所有人都进房子的路径总和最少是多少? 思路: 比较简单的最大流,直接建立两排, ...
- c# 通过 p/invoke 使用 c的加密程序 参数传递问题
最近项目中使用需要上位机和下位机通过rs232通信,涉及到通讯加密问题, 硬件那边主要是pcb layout的,于是我就把加密的活拦了过来,锻炼锻炼 首先说明问题: 在c中,加密解密都测试通过,然后在 ...
- GIF图片裁剪出指定大小的GIF图片
前言 最近在博客后台上传图片的时候,突然发现上传gif图片的时候裁剪图片有问题.既没法裁剪gif指定区域的图片,又没法裁剪指定区域生成一个新的指定大小的gif图.本来想直接去找个裁剪的库直接放上去的, ...
- 【Github搬砖】Python入门网络爬虫之精华版
Python学习网络爬虫主要分3个大的版块:抓取,分析,存储 另外,比较常用的爬虫框架Scrapy,这里最后也详细介绍一下. 首先列举一下本人总结的相关文章,这些覆盖了入门网络爬虫需要的基本概念和技巧 ...