代码之家  ›  专栏  ›  技术社区  ›  letthefireflieslive

FUNDALL与FordAL与CRUDIDENT库的冲突

  •  1
  • letthefireflieslive  · 技术社区  · 8 年前

    登录

    @ApiModel
    @Entity
    public class Login {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;
    
        private LocalDateTime loginDateTime;
    
        /** Other fields ***/
    } 
    

    登录

    interface LoginDateOnly {
    
        @Value("#{target.loginDateTime.toLocalDate()}")
        LocalDate getDateFromLoginDateTime();
    
    }
    

    后勤储备

    @RepositoryRestResource(collectionResourceRel = "login", path = "login")
    public interface LoginRepository extends PagingAndSortingRepository<Login, Long> {
    
        Collection<LoginDateOnly> findAll();
    
        /** Other query methods **/
    }
    

    我只想得到 全部的 我的登录记录,使用 本地日期部分 我的 loginDateTime 使用 http://host/api/login . 但目前我遇到了与Crudrepository的findall()的冲突。如何使用投影尽可能地解决这个问题。我做的是“查询”和“命名”查询。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Cepr0    8 年前

    findAll 方法签名是:

    List<T> findAll();
    

    如果要覆盖它,则不能使用其他签名。

    要获得投影列表,只需为此定义另一种方法,例如:

    Collection<LoginDateOnly> findAllBy();
    

    但正如我所看到的,您正在使用spring数据rest,因此在本例中不需要定义新方法。您应该首先添加注释 @Projection 对你的预测:

    @Projection(name = "loginDateOnly", types = Login.class)
    interface LoginDateOnly {
        //...
    }
    

    然后在请求url中使用其名称:

    GET http://host/api/login?projection=loginDateOnly
    

    请参阅文档中的更多信息: Projections and Excerpts