Spring Data JDBC 查询指定字段
Spring Boot JDBC About 550 words需求
Repository
默认会查询实体类中的所有字段,但这不是最佳实践。
需求:只查询部分需要的字段。
Projection
官方文档将此定义为Projection
映射。
public interface SysUserRepository extends PagingAndSortingRepository<SysUser, Long>{
Page<SysUserDto> findAllBy(Pageable pageable);
Page<NameOnly> getAllBy(Pageable pageable);
}
public record SysUserDto(
@JsonSerialize(using = ToStringSerializer.class)
Long id,
String username) {
}
public interface NameOnly {
String getUsername();
}
参考
https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#projections
Views: 856 · Posted: 2024-04-16
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...