如何为Page<>接口创建DTO?

huangapple go评论93阅读模式
英文:

How do I make a DTO for the Page<> interface?

问题

我正在使用Spring Boot(MVC)和Hibernate编写一个在线商店。问题是,当我获取饮料列表时,JSON会给我返回Page接口中不必要的信息。我不知道如何为这些接口创建DTO以摆脱这些字段。在这种情况下我应该怎么做?是否有现成的解决方案?

  1. public Page<DrinkDTO> getAllDrinks(int page, int pageSize) {
  2. PageRequest pageRequest = PageRequest.of(page, pageSize, Sort.by("id"));
  3. final Page<Drink> drinks = drinkRepository.findAll(pageRequest);
  4. return drinkMapper.drinksToDrinksDTO(drinks);
  5. }
英文:

I am writing an online store using Spring Boot (MVC) and Hiberbate. The problem is that when I get a list of drinks, JSON gives me unnecessary information from the Page interface. I don't know how you can create an DTO for the interfaces to get rid of these fields. What should I do in this situation. Can someone have a ready-made solution?

如何为Page<>接口创建DTO?

  1. public Page&lt;DrinkDTO&gt; getAllDrinks(int page, int pageSize) {
  2. PageRequest pageRequest = PageRequest.of(page, pageSize, Sort.by(&quot;id&quot;));
  3. final Page&lt;Drink&gt; drinks = drinkRepository.findAll(pageRequest);
  4. return drinkMapper.drinksToDrinksDTO(drinks);
  5. }

答案1

得分: 1

  1. @Data
  2. @AllArgsConstructor
  3. public class CustomPage {
  4. Long totalElements;
  5. int totalPages;
  6. int number;
  7. int size;
  8. }
  9. @Data
  10. public class PageDTO<T> {
  11. List<T> content;
  12. CustomPage customPage;
  13. public PageDTO(Page<T> page) {
  14. this.content = page.getContent();
  15. this.customPage = new CustomPage(page.getTotalElements(),
  16. page.getTotalPages(), page.getNumber(), page.getSize());
  17. }
  18. }
  19. Service for example:
  20. public PageDTO<DrinkDTO> getAllDrinks(int page, int pageSize) {
  21. PageRequest pageRequest = PageRequest.of(page, pageSize, Sort.by("id"));
  22. final Page<Drink> drinks = drinkRepository.findAll(pageRequest);
  23. return new PageDTO<DrinkDTO>(drinkMapper.drinksToDrinksDTO(drinks));
  24. }
英文:
  1. @Data
  2. @AllArgsConstructor
  3. public class CustomPage {
  4. Long totalElements;
  5. int totalPages;
  6. int number;
  7. int size;
  8. }
  1. @Data
  2. public class PageDTO&lt;T&gt; {
  3. List&lt;T&gt; content;
  4. CustomPage customPage;
  5. public PageDTO(Page&lt;T&gt; page) {
  6. this.content = page.getContent();
  7. this.customPage = new CustomPage(page.getTotalElements(),
  8. page.getTotalPages(), page.getNumber(), page.getSize());
  9. }

Service for example:

  1. public PageDTO&lt;DrinkDTO&gt; getAllDrinks(int page, int pageSize) {
  2. PageRequest pageRequest = PageRequest.of(page, pageSize, Sort.by(&quot;id&quot;));
  3. final Page&lt;Drink&gt; drinks = drinkRepository.findAll(pageRequest);
  4. return new PageDTO&lt;DrinkDTO&gt;(drinkMapper.drinksToDrinksDTO(drinks));
  5. }

答案2

得分: -1

我使用原生查询,然后大多数时候进行DTO投影。

这里是DTO投影的示例:

  1. public interface OvertimeRequestView {
  2. public Long getId();
  3. public String getEmployeeFirstName();
  4. public String getEmployeeLastName();
  5. public Long getOvertimeHours();
  6. public Date getOvertimeDate();
  7. public String getDescription();
  8. public String getStatus();
  9. public String getApproverName();
  10. public default String getEmployeeFullName() {
  11. String lastName = this.getEmployeeLastName();
  12. String firstName = this.getEmployeeFirstName();
  13. if (null != firstName) {
  14. return firstName.concat(" ").concat(lastName);
  15. }
  16. return lastName;
  17. }
  18. }

这是带有原生查询的存储库。请注意,由于查询返回'id'列,因此在上面的DTO中有一个getId()方法,由于它具有employeeFirstName,所以在DTO中有getEmployeeFirstName()等等。还请注意,我包含了一个计数查询,如果查询复杂且包含连接,没有计数查询,查询有时会失败。

  1. @Query(value = "select ovr.id,\n" +
  2. " u.first_name as employeeFirstName,\n" +
  3. " u.last_name as employeeLastName,\n" +
  4. " ovr.overtime_date as overtimeDate,\n" +
  5. " ovr.description as description,\n" +
  6. " ovr.overtime_hours as overtimeHours\n" +
  7. "from overtime_requests ovr\n" +
  8. " left join employees e on ovr.employee_id = e.id\n" +
  9. " left join users u on e.user_id = u.id",
  10. nativeQuery = true,
  11. countQuery = "select count(ovr.id)\n" +
  12. "from overtime_requests ovr\n" +
  13. " left join employees e on ovr.employee_id = e.id\n" +
  14. " left join users u on e.user_id = u.id")
  15. public Page<OvertimeRequestView> getAllActive(Pageable pageable);

要了解更多信息,请查阅 spring data documentation

英文:

I use native query and then I i do dto projections most of the time.

Here is an example of DTO projection

  1. public interface OvertimeRequestView {
  2. public Long getId();
  3. public String getEmployeeFirstName();
  4. public String getEmployeeLastName();
  5. public Long getOvertimeHours();
  6. public Date getOvertimeDate();
  7. public String getDescription();
  8. public String getStatus();
  9. public String getApproverName();
  10. public default String getEmployeeFullName() {
  11. String lastName = this.getEmployeeLastName();
  12. String firstName = this.getEmployeeFirstName();
  13. if (null != firstName) {
  14. return firstName.concat(&quot; &quot;).concat(lastName);
  15. }
  16. return lastName;
  17. }
  18. }

and here is the repository with a native query. notice that since the query returns 'id' column I have a getId() method in the dto above,and since it has employeeFirstName i have getEmployeeFirstName() in the dto and so on. Notice also that I include a count query, without a count query, the queries sometime fail especially if the queries are complex and contain joins

  1. @Query(value = &quot;select ovr.id,\n&quot; +
  2. &quot; u.first_name as employeeFirstName,\n&quot; +
  3. &quot; u.last_name as employeeLastName,\n&quot; +
  4. &quot; ovr.overtime_date as overtimeDate,\n&quot; +
  5. &quot; ovr.description as description,\n&quot; +
  6. &quot; ovr.overtime_hours as overtimeHours\n&quot; +
  7. &quot;from overtime_requests ovr\n&quot; +
  8. &quot; left join employees e on ovr.employee_id = e.id\n&quot; +
  9. &quot; left join users u on e.user_id = u.id&quot;,
  10. nativeQuery = true,
  11. countQuery = &quot;select count(ovr.id)\n&quot; +
  12. &quot;from overtime_requests ovr\n&quot; +
  13. &quot; left join employees e on ovr.employee_id = e.id\n&quot; +
  14. &quot; left join users u on e.user_id = u.id&quot;)
  15. public Page&lt;OvertimeRequestView&gt; getAllActive(Pageable pageable);

For more you can check from spring data documentation

huangapple
  • 本文由 发表于 2020年7月30日 23:18:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63176297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定