后端快捷代码

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.

相关推荐
程序员张31 小时前
Maven编译和打包插件
java·spring boot·maven
ybq195133454312 小时前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339463 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8503 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
枯萎穿心攻击3 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
DKPT4 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
Eiceblue5 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
好奇的菜鸟5 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
m0_555762905 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
DuelCode6 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis