后端快捷代码

1.BeanUtils.copyProperties

BeanUtils.copyProperties(wrongBook, vo) 是 Spring 框架提供的一个工具方法,用于将一个 Java 对象的属性值复制到另一个 Java 对象。这在 DTO(数据传输对象)和实体类之间的转换中非常常用。

bash 复制代码
// 源类(数据库实体)
class WrongBook {
    private String id;
    private String questionId;
    private String studentAnswer;
    private boolean isCorrect;
    // getters/setters
}

// 目标类(VO - 视图对象)
class WrongBookVO {
    private String id;
    private String questionId;
    private String studentAnswer;
    private boolean isCorrect;
    private WringQuestionVo questionVo; // 额外属性
    // getters/setters
}
使用 BeanUtils 复制属性:
bash 复制代码
WrongBook source = new WrongBook();
source.setId("1");
source.setQuestionId("Q1001");
source.setStudentAnswer("A");
source.setCorrect(true);

WrongBookVO target = new WrongBookVO();
BeanUtils.copyProperties(source, target);

// 结果:
// target.getId() → "1"
// target.getQuestionId() → "Q1001"
// target.getStudentAnswer() → "A"
// target.isCorrect() → true
// target.getQuestionVo() → null(源对象没有该属性,保持默认值)

注意事项

  1. 属性名必须相同 :如果 sourcequestionId,而 targetqId,则不会复制。
  2. 类型需兼容intInteger 可兼容,但 StringInteger 不可。
  3. 复杂类型:只复制基本类型和嵌套对象的引用,不会递归复制。

2.questionList.stream()

复制代码
将题目ID映射到题目对象(用于快速查找)
Map<String, Question> questionMap = questionList.stream()
        .collect(Collectors.toMap(Question::getId, q -> q));

把questionList,按quesiton里面的id写成map的形式!

3.getRecords().stream()

复制代码
List<String> questionIds = wrongBooksPage.getRecords().stream()
        .map(WrongBooks::getQuestionId)
        .filter(Objects::nonNull)
        .collect(Collectors.toList());

提取wrongBooksPage集合里面的某一个字段成list集合,这里提取的是id

4.

5.

6.

7.

8.

9.

相关推荐
兮动人2 小时前
Spring中@Configuration注解的proxyBeanMethods属性详解
java·后端·spring
zl9798992 小时前
SpringBoot-数据访问之Druid
java·spring boot
猫头虎2 小时前
解决升级IDEA2025.2后,每次打开Maven项目爆红的问题:Windows和Mac解决方案
java·ide·macos·maven·intellij-idea·idea·intellij idea
NFG89C2 小时前
Adobe Lightroom Classic 2025解锁版 (专业照片管理)
java·adobe·工具
蒙娜丽宁2 小时前
Rust 性能优化指南:内存管理、并发调优与基准测试案例
开发语言·性能优化·rust
蒙奇D索大3 小时前
【计算机网络】408计算机网络高分指南:物理层编码与调制技术精讲
java·前端·学习·计算机网络
豐儀麟阁贵3 小时前
5.2 类
java·开发语言
九皇叔叔3 小时前
Java循环结构全解析:从基础用法到性能优化(含经典案例)
java·开发语言·python
漫漫不慢.3 小时前
蓝桥杯-16955 岁月流转
java·jvm·蓝桥杯
JanelSirry3 小时前
如何查看java死锁?具体怎么做,怎么避免
java·开发语言