后端快捷代码

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 分钟前
系统管理工具,多功能隐私清理文件粉碎工具
java·网络·windows·r语言·电脑·excel·symfony
随丶芯7 分钟前
IDEA安装leetcode-editor插件
java·开发语言
范什么特西13 分钟前
下载idea旧版本
java·ide·intellij-idea
计算机毕设指导619 分钟前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
qq_124987075320 分钟前
基于微信小程序的宠物交易平台的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·小程序·毕业设计·计算机毕业设计
小毅&Nora21 分钟前
【Java线程安全实战】⑧ 阶段同步的艺术:Phaser 与 Condition 的高阶玩法
java·多线程
Ccjf酷儿21 分钟前
C++语言程序设计 (郑莉)第六章 数组、指针和字符串
开发语言·c++
内存不泄露22 分钟前
基于Spring Boot和Vue的企业办公自动化系统设计与实现
java·vue.js·spring boot·intellij-idea
禹曦a23 分钟前
Java实战:Spring Boot 构建电商订单管理系统RESTful API
java·开发语言·spring boot·后端·restful
code_lfh23 分钟前
Spring Boot测试类的使用参考
java·spring boot·junit