后端快捷代码

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.

相关推荐
木棉软糖几秒前
一个MySQL的数据表最多能够存多少的数据?
java
魔尔助理顾问11 分钟前
系统整理Python的循环语句和常用方法
开发语言·后端·python
Ares-Wang15 分钟前
JavaScript》》JS》 Var、Let、Const 大总结
开发语言·前端·javascript
程序视点28 分钟前
Java BigDecimal详解:小数精确计算、使用方法与常见问题解决方案
java·后端
愿你天黑有灯下雨有伞34 分钟前
Spring Boot SSE实战:SseEmitter实现多客户端事件广播与心跳保活
java·spring boot·spring
遇见尚硅谷1 小时前
C语言:*p++与p++有何区别
c语言·开发语言·笔记·学习·算法
Java初学者小白1 小时前
秋招Day20 - 微服务
java
SkyrimCitadelValinor1 小时前
c#中让图片显示清晰
开发语言·c#
艾莉丝努力练剑2 小时前
【数据结构与算法】数据结构初阶:详解排序(二)——交换排序中的快速排序
c语言·开发语言·数据结构·学习·算法·链表·排序算法
狐小粟同学2 小时前
JavaEE--3.多线程
java·开发语言·java-ee