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: 308 · Posted: 2024-04-16

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh