博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatisplus按某一字段查询的一个坑
阅读量:764 次
发布时间:2019-03-24

本文共 973 字,大约阅读时间需要 3 分钟。

概述

baomidou.mybatisplus 封装的方法按指定字段查询数据库时,返回的 list 只有长度,没有元素。

代码如下:
我只需要查obtainName这一个额字段就可以了,但是 debug 时结果集 creditReportRecords 长度为 188,但是返回时取 obtainName 的时候报空。

public List
getObtainerList() {
List
creditReportRecords = this.baseMapper.selectList( Wrappers.
lambdaQuery() .select(CreditReportRecord::getObtainName)); if (CollectionUtils.isEmpty(creditReportRecords)){
return null; } return creditReportRecords.stream() .map(CreditReportRecord::getObtainName).collect(Collectors.toList()); }

原因

数据库中的所有记录的 obtainName 字段都为 null 导致 list 的元素为null,所以报NPE。

解决方法

查询的时候对 obtainName 字段做空过滤,那空的直接干掉。

List
creditReportRecords = this.baseMapper.selectList( Wrappers.
lambdaQuery() .select(CreditReportRecord::getObtainName) .isNotNull(CreditReportRecord::getObtainName));

转载地址:http://lunkk.baihongyu.com/

你可能感兴趣的文章