后端快捷代码

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.

相关推荐
q***33371 小时前
oracle 12c查看执行过的sql及当前正在执行的sql
java·sql·oracle
tobebetter95274 小时前
How to manage python versions on windows
开发语言·windows·python
Y***h1875 小时前
第二章 Spring中的Bean
java·后端·spring
9***P3345 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
8***29315 小时前
解决 Tomcat 跨域问题 - Tomcat 配置静态文件和 Java Web 服务(Spring MVC Springboot)同时允许跨域
java·前端·spring
CoderYanger5 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz5 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
q***06295 小时前
Tomcat的升级
java·tomcat
多多*6 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
青云交6 小时前
Java 大视界 -- Java 大数据在智能物流无人配送车路径规划与协同调度中的应用
java·spark·路径规划·大数据分析·智能物流·无人配送车·协同调度