news 2026/4/29 17:07:13

MyBatis-Plus 中 查询排除大字段方法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
MyBatis-Plus 中 查询排除大字段方法
以下spring boot代码: package com.weiyu.model; import com.baomidou.mybatisplus.annotation.*; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Data; import lombok.EqualsAndHashCode; import java.math.BigDecimal; import java.time.LocalDateTime; /** * 资金信息实体类 */ @Data @EqualsAndHashCode(callSuper = false) @TableName("CapitalInfoManage") public class CapitalInfo { /** * 主键id */ @TableId(value = "cim_ID", type = IdType.AUTO) private Integer id; /** * 资金序号 */ @TableField("cim_CapitalNo") private String capitalNo; /** * 资金名称 */ @TableField("cim_CapitalName") @NotBlank(message = "资金名称不能为空") private String capitalName; /** * 资金类别 */ @TableField("cim_CapitalType") @NotBlank(message = "资金类别不能为空") private String capitalType; /** * 指标预算总额 */ @TableField("cim_CapitalTotal") @NotNull(message = "指标预算总额不能为空") private BigDecimal capitalTotal; /** * 指标剩余额 */ @TableField("cim_CapitalLeaveTotal") @NotNull(message = "指标剩余额不能为空") private BigDecimal capitalLeaveTotal; /** * 指标可用总额 */ @TableField("cim_CapitalValidTotal") @NotNull(message = "指标可用总额不能为空") private BigDecimal capitalValidTotal; /** * 指标类别 */ @TableField("cim_CapitalIndexType") @NotBlank(message = "指标类别不能为空") private String capitalIndexType; /** * 资金账户 */ @TableField("cim_CapitalAccount") @NotBlank(message = "资金账户不能为空") private String capitalAccount; /** * 资金来源 */ @TableField("cim_CapitalSource") @NotBlank(message = "资金来源不能为空") private String capitalSource; /** * 指标来源 */ @TableField("cim_CapitalIndexSource") @NotBlank(message = "指标来源不能为空") private String capitalIndexSource; /** * 资金年份 */ @TableField("cim_CapitalYear") @NotNull(message = "资金年份不能为空") @Min(value = 2000, message = "资金年份不能早于2000年") @Max(value = 2100, message = "资金年份不能晚于2100年") private Integer capitalYear; /** * 资金状态 */ @TableField("cim_State") @NotNull(message = "状态不能为空") private Integer capitalState; /** * 备注信息 */ @TableField("cim_Remark") private String remark; /** * 创建时间 */ @TableField(value = "cim_CreateDate", fill = FieldFill.INSERT) private LocalDateTime createTime; /** * 附件 */ @TableField(value = "cim_File") private byte[] fileContent; /** * 附件名称 */ @TableField(value = "cim_FileName") private String fileName; /** * 附件是否为空 */ @TableField(value = "cim_FileIsNull") private Boolean fileIsNull; } /** * 获取资金信息列表 */ public List<CapitalInfoVO> queryList(@NotNull CapitalInfoQueryDTO queryDTO) { // 处理日期范围,避免为 null if (queryDTO.getCreateDateRange() == null) { queryDTO.setCreateDateRange(new ArrayList<>()); } LocalDateTime beginDateTime = null; LocalDateTime endDateTime = null; if (queryDTO.getCreateDateRange() != null && queryDTO.getCreateDateRange().size() == 2) { List<LocalDateTime> dateTimeRange = DateUtils.parseDateTimeRange(queryDTO.getCreateDateRange()); if (dateTimeRange.size() == 2) { beginDateTime = dateTimeRange.get(0); endDateTime = dateTimeRange.get(1); } } LambdaQueryWrapper<CapitalInfo> queryWrapper = new LambdaQueryWrapper<>(); // 构造查询条件 queryWrapper // 模糊查询 - 资金序号 .like(StringUtils.hasText(queryDTO.getCapitalNo()), CapitalInfo::getCapitalNo, queryDTO.getCapitalNo()) // 模糊查询 - 资金名称 .like(StringUtils.hasText(queryDTO.getCapitalName()), CapitalInfo::getCapitalName, queryDTO.getCapitalName()) // 精确查询 - 资金类别 .eq(StringUtils.hasText(queryDTO.getCapitalType()), CapitalInfo::getCapitalType, queryDTO.getCapitalType()) // 精确查询 - 指标类别 .eq(StringUtils.hasText(queryDTO.getCapitalIndexType()), CapitalInfo::getCapitalIndexType, queryDTO.getCapitalIndexType()) // 模糊查询 - 资金账户 .like(StringUtils.hasText(queryDTO.getCapitalAccount()), CapitalInfo::getCapitalAccount, queryDTO.getCapitalAccount()) // 精确查询 - 资金来源 .eq(StringUtils.hasText(queryDTO.getCapitalSource()), CapitalInfo::getCapitalSource, queryDTO.getCapitalSource()) // 精确查询 - 指标来源 .eq(StringUtils.hasText(queryDTO.getCapitalIndexSource()), CapitalInfo::getCapitalIndexSource, queryDTO.getCapitalIndexSource()) // 精确查询 - 资金年份 .eq(queryDTO.getCapitalYear() != null, CapitalInfo::getCapitalYear, queryDTO.getCapitalYear()) // 精确查询 - 资金状态 .eq(queryDTO.getCapitalState() != null, CapitalInfo::getCapitalState, queryDTO.getCapitalState()) // 模糊查询 - 备注信息 .like(StringUtils.hasText(queryDTO.getRemark()), CapitalInfo::getRemark, queryDTO.getRemark()) // between - 创建日期 .between( queryDTO.getCreateDateRange().size() == 2, CapitalInfo::getCreateTime, beginDateTime, endDateTime ) // 倒序排序 - 资金年份 .orderByDesc(CapitalInfo::getCapitalYear) // 倒序排序 - 主键id .orderByDesc(CapitalInfo::getId); List<CapitalInfo> capitalInfos = capitalInfoMapper.selectList(queryWrapper); // List<CapitalInfo> -> List<CapitalInfoVO> return convertToCapitalInfoVOs(capitalInfos); } 如何设置不会查询附件fileContent

要避免在查询列表时返回大字段fileContent,有以下几种方法:

方法一:使用 MyBatis-Plus 的 select 方法指定查询字段(推荐)

修改查询方法,显式指定要查询的字段,排除fileContent

java

public List<CapitalInfoVO> queryList(@NotNull CapitalInfoQueryDTO queryDTO) { // ... 日期处理代码保持不变 ... LambdaQueryWrapper<CapitalInfo> queryWrapper = new LambdaQueryWrapper<>(); // 显式指定要查询的字段,排除 fileContent queryWrapper.select( CapitalInfo::getId, CapitalInfo::getCapitalNo, CapitalInfo::getCapitalName, CapitalInfo::getCapitalType, CapitalInfo::getCapitalTotal, CapitalInfo::getCapitalLeaveTotal, CapitalInfo::getCapitalValidTotal, CapitalInfo::getCapitalIndexType, CapitalInfo::getCapitalAccount, CapitalInfo::getCapitalSource, CapitalInfo::getCapitalIndexSource, CapitalInfo::getCapitalYear, CapitalInfo::getCapitalState, CapitalInfo::getRemark, CapitalInfo::getCreateTime, CapitalInfo::getFileName, CapitalInfo::getFileIsNull ); // 构造查询条件 queryWrapper .like(StringUtils.hasText(queryDTO.getCapitalNo()), CapitalInfo::getCapitalNo, queryDTO.getCapitalNo()) // ... 其他条件保持不变 ... .between( queryDTO.getCreateDateRange().size() == 2, CapitalInfo::getCreateTime, beginDateTime, endDateTime ) .orderByDesc(CapitalInfo::getCapitalYear) .orderByDesc(CapitalInfo::getId); List<CapitalInfo> capitalInfos = capitalInfoMapper.selectList(queryWrapper); return convertToCapitalInfoVOs(capitalInfos); }

方法二:在实体类上使用@TableField(select = false)

在实体类的fileContent字段上添加select = false属性:

java

/** * 附件 */ @TableField(value = "cim_File", select = false) private byte[] fileContent;

这样所有查询都不会包含这个字段。

方法三:使用单独的查询 DTO 或 VO

创建专门的查询 DTO 或 VO,不包含大字段:

java

@Data public class CapitalInfoListDTO { private Integer id; private String capitalNo; private String capitalName; private String capitalType; private BigDecimal capitalTotal; private BigDecimal capitalLeaveTotal; private BigDecimal capitalValidTotal; private String capitalIndexType; private String capitalAccount; private String capitalSource; private String capitalIndexSource; private Integer capitalYear; private Integer capitalState; private String remark; private LocalDateTime createTime; private String fileName; private Boolean fileIsNull; // 不包含 fileContent 字段 }

然后在查询时使用:

java

@Select("SELECT cim_ID, cim_CapitalNo, cim_CapitalName, ... FROM CapitalInfoManage") List<CapitalInfoListDTO> queryListDTO(CapitalInfoQueryDTO queryDTO);

方法四:使用 MyBatis-Plus 的excludeColumns(3.x版本)

如果是 MyBatis-Plus 3.x 版本,可以使用 excludeColumns:

java

LambdaQueryWrapper<CapitalInfo> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.selectExclude(CapitalInfo::getFileContent);

方法五:使用 SQL 片段

如果使用了 XML mapper,可以在 SQL 中明确指定字段:

xml

<select id="queryList" resultType="com.weiyu.model.CapitalInfo"> SELECT cim_ID, cim_CapitalNo, cim_CapitalName, cim_CapitalType, cim_CapitalTotal, cim_CapitalLeaveTotal, cim_CapitalValidTotal, cim_CapitalIndexType, cim_CapitalAccount, cim_CapitalSource, cim_CapitalIndexSource, cim_CapitalYear, cim_State, cim_Remark, cim_CreateDate, cim_FileName, cim_FileIsNull <!-- 不查询 cim_File 字段 --> FROM CapitalInfoManage WHERE ... </select>

建议

推荐使用方法一,原因:

  1. 清晰明确,维护方便

  2. 性能最优,SQL 中只查询需要的字段

  3. 不影响其他需要查询附件内容的方法

  4. 符合 MyBatis-Plus 的最佳实践

如果所有查询都不需要fileContent,可以使用方法二;如果需要更细粒度的控制,使用方法一。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/24 3:12:35

救命神器!8款AI论文工具测评:本科生毕业论文全攻略

救命神器&#xff01;8款AI论文工具测评&#xff1a;本科生毕业论文全攻略 2026年AI论文工具测评&#xff1a;为何要关注这些工具&#xff1f; 随着人工智能技术的不断进步&#xff0c;越来越多的本科生开始依赖AI论文工具来提升写作效率、优化内容质量。然而&#xff0c;面对市…

作者头像 李华
网站建设 2026/4/25 3:39:29

停止幻想!Java就业只会越来越难!

最近小伙伴在我后台留言是这样的&#xff1a;现在就这光景&#xff0c;不比以前&#xff0c;会个CRUD就有人要&#xff0c;即使大部分公司依然只需要做CRUD的事情......现在去面试&#xff0c;只会CRUD还要被吐槽&#xff1a;面试造火箭&#xff0c;工作拧螺丝&#xff0c;就是…

作者头像 李华
网站建设 2026/4/23 12:50:46

小程序计算机毕设之基于springboot+协同过滤算法的美妆护理类的购物平台基于协同过滤算法的彩妆商城系统的设计与实现小程序(完整前后端代码+说明文档+LW,调试定制等)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/4/24 2:30:40

抖音直播间弹幕实时提取

// ==UserScript== // @name 抖音直播间弹幕暴力提取 (V9.1 Protobuf 稳定版) // @namespace http://tampermonkey.net/ // @version 9.1 // @description 移除冗余字段定义,解决 Type Missing 报错 | 100% 精准提取用户名和内容 // @author Gemini &a…

作者头像 李华