Spring Boot uses Mybatis Plus to query single field data and specified field data
In SpringBoot, we use Myba Plus and usually only need a certain field or a few fields in the query results. Without further ado, just go to the code:
object entity class
@Data
public class Admin implements Serializable {
private static final long serialVersionUID = 7406871806983647481L;
@TableId(value = "admin_id")
private String adminId;
private String adminName;
private String adminMobile;
private String adminPassword;
}
Use Stream to filter required fields
List<String> collect = adminService.list().stream().map(Admin::getAdminMobile).collect(Collectors.toList());
Use Mybatis Plus to filter the required fields
// 查询单个字段
List<String> strings = adminService.listObjs(new QueryWrapper<Admin>()
.lambda().select(Admin::getAdminMobile), Object::toString);
// 查询多个字段,其它不需要查询的字段则为null
List<Object> objects = adminService.listObjs(new QueryWrapper<Admin>()
.lambda().select(Admin::getAdminMobile, Admin::getAdminName));