public List<AccountVO> queryAccountList(String id, String name, String tel) { Account account = new Account(); account.setId(id); account.setName(name); account.setTel(tel); List<Account> list = accountMapper.selectAccountList1(account); if (org.springframework.util.CollectionUtils.isEmpty(list)) { return new ArrayList<>(); } return list.stream().map(this::AccountVO2).collect(Collectors.toList()); }请改写代码
时间: 2024-02-26 15:58:36 浏览: 105
可以使用 Java 8 的 Optional 类型来改写代码,让代码更简洁易读。改写后的代码如下:
```
public List<AccountVO> queryAccountList(String id, String name, String tel) {
Account account = new Account();
Optional.ofNullable(id).ifPresent(account::setId);
Optional.ofNullable(name).ifPresent(account::setName);
Optional.ofNullable(tel).ifPresent(account::setTel);
List<Account> list = accountMapper.selectAccountList1(account);
return list.stream().map(AccountVO2::new).collect(Collectors.toList());
}
```
这样代码更加简洁,使用了 Optional 类型来避免了繁琐的 null 判断,使用方法引用来创建 AccountVO2 对象。
相关问题
package com.example.entity; import javax.persistence.*; import java.util.List; @Table(name = "yonghu_info") public class YonghuInfo extends Account { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "zhanghao") private String zhanghao; @Column(name = "mima") private String mima; @Column(name = "xingming") private String xingming; @Column(name = "xingbie") private String xingbie; @Column(name = "shouji") private String shouji; @Column(name = "shenfenzheng") private String shenfenzheng; @Column(name = "zhaopian") private String zhaopian; @Column(name = "beizhu") private String beizhu; @Column(name = "level") private String level; @Column(name = "addtime") private String addtime; @Column(name = "status") private String status; @Transient private List<Long> zhaopianflst; //yoxuxtupTransiexnt public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public List<Long> getZhaopianflst() { return zhaopianflst; } public void setZhaopianflst(List<Long> zhaopianflst) { this.zhaopianflst = zhaopianflst; }
这是一段 Java 代码,用于定义一个名为 YonghuInfo 的实体类,继承自 Account 类。其中使用了注解来定义数据库表名和字段,同时包含了一些基本属性,如账号、密码、姓名、性别、手机号、身份证号、照片、备注、等级、添加时间和状态等。此外还有一个列表属性 zhaopianflst,用于存储照片 id 列表,但有一个注解 Transient 表示该属性不会被持久化到数据库中。
package com.example.entity; import javax.persistence.*; import java.util.List; @Table(name = "xitongjianjie_info") public class XitongjianjieInfo extends Account { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "leibie") private String leibie; @Column(name = "neirong") private String neirong; @Column(name = "addtime") private String addtime; @Column(name = "status") private String status; //yoxuxtupTransiexnt public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } //gextsexttpzidxuan //public String getFileIds() { // return fileIds; // } // // public void setFileIds(String fileIds) { // this.fileIds = fileIds; // } public String getLeibie() { return leibie; } public void setLeibie(String leibie) { this.leibie = leibie; } public String getNeirong() { return neirong; } public void setNeirong(String neirong) { this.neirong = neirong; } public String getAddtime() { return addtime; } public void setAddtime(String addtime) { this.addtime = addtime == null ? null : addtime.trim(); } public void setId(Long id) { this.id = id; } public Long getId() { return this.id; } }
这是一个 Java 实体类,表名为 xitongjianjie_info,继承了 Account 类,包含 id、leibie、neirong、addtime、status 等字段,并且每个字段都有对应的 getter 和 setter 方法。
阅读全文