博客
关于我
mybatisplus按某一字段查询的一个坑
阅读量:767 次
发布时间:2019-03-24

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

优化后的技术文章

baomidou.mybatisplus封装的方法按指定字段查询数据库时,返回的 List 只有长度,没有元素。此问题通常发生在查询条件不正确或者数据存储存在问题时。让我详细分析一下如何解决这一问题,并帮助你找到最优的查询方式。

问题描述

你的代码如下:

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

此代码的目的是从数据库中获取一个叫做obtainName的字段的值。然而在调试过程中,creditReportRecords的长度是188,说明数据库中存在大量的记录。但是,当返回List时,返回的内容却是空的,这说明creditReportRecords中的getObtainName方法返回了null,从而引发NullPointerException错误。

原因分析

  • 字段存储问题:数据库中obtainName字段的值可能全部为null,导致creditReportRecords列表中存在大量null元素。
  • 查询条件不对:如果查询条件没有设置过滤,且数据库中该字段的值均为null,则会直接返回所有记录,包含了null,从而导致最终结果为空。
  • 解决方法

    为了避免NullPointerException错误发生,需要在查询时对字段进行过滤,排除null值的记录。以下是优化后的代码:

    public List< String >      getObtainerList() {    List< CreditReportRecord >          creditReportRecords = this.baseMapper.selectList(            Wrappers.< CreditReportRecord >                .lambdaQuery()                .select(CreditReportRecord::getObtainName)                .isNotNull(CreditReportRecord::getObtainName)        );    return !CollectionUtils.isEmpty(creditReportRecords)        ? creditReportRecords.stream()            .map(CreditReportRecord::getObtainName)            .filter(name -> name != null)            .collect(Collectors.toList())        : null;}

    优点分析:

  • 直接筛选:在lambdaQuery中直接使用isNotNull方法,确保只返回obtainName字段不为null的记录。这样可以减少后续处理的负担。
  • 去空处理:检查creditReportRecords是否为空,避免直接调用stream()方法。如果creditReportRecords为空,则直接返回null,减少不必要的计算。
  • 数据安全:通过filter(name -> name != null)方法进一步确保返回的值是有效的,避免在最高级调用中出现NullPointerException
  • 总结

    通过对查询条件进行优化,使用isNotNull方法对字段进行过滤,可以避免因为null值而导致的错误。同时,合理处理List为空的情况,可以提升代码的健壮性。当数据库中存在大量字段需要通过查询条件过滤时,可以通过逐步筛选,确保只返回有效数据,从而提高应用的运行效率。

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

    你可能感兴趣的文章
    python | h5py,一个无敌的关于 HDF5 的 Python 库!
    查看>>
    python | huey,一个非常厉害的 任务调度 Python 库!
    查看>>
    python | hypothesis,一个有趣的 Python 库!
    查看>>
    python | Indico,一个超酷的 Python 库!
    查看>>
    python | isort,一个有趣的 自动整理导入语句 的Python 库!
    查看>>
    python | jinja,一个超酷的 Python 库!
    查看>>
    python | joblib,一个强大的 Python 库!
    查看>>
    python调用git bash_Python学习第70课-用Git Bash在命令行打开sublime
    查看>>
    python | jsonschema,一个实用的 验证 JSON 数据结构 Python 库!
    查看>>
    python课程的中期报告范文_课题研究中期总结报告范文
    查看>>
    python | lxml,一个超酷的 关于XML/HTML 文档 Python 库!
    查看>>
    python | mplfinance,一个有趣的金融数据可视化 Python 库!
    查看>>
    python | nipy,一个强大的关于 神经影像数据分析 的Python 库!
    查看>>
    python | NLTK,一个强大的 自然语言处理 Python 库!
    查看>>
    python | nupic,一个强大的 处理时间序列的Python 库!
    查看>>
    python | orange3,一个神奇的 Python 库!
    查看>>
    python | pdfminer,一个神奇的 关于PDF 文件的 Python 库!
    查看>>
    python | pendulum,一个有趣的 日期和时间 Python 库!
    查看>>
    python | pluginbase,一个神奇的 关于插件框架 的Python 库!
    查看>>
    python | ply,一个无敌的 词法和语法分析工具 的Python 库!
    查看>>