【问题标题】:How to convert List<Object[]> to List<SimpleAccount> in Java?如何在 Java 中将 List<Object[]> 转换为 List<SimpleAccount>?
【发布时间】:2019-11-12 05:39:43
【问题描述】:

我正在使用 JDK 12、Spring Boot 2.1.5.RELEASE、Spring Data JPA。我遵循https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.sorting的指南

我有仓库

package com.example.repository;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.example.entity.Account;

import java.util.List;

@Repository
public interface AccountRepository extends JpaRepository<Account, Integer> {

    @Query("SELECT a.id, a.accountNumber, a.accountName FROM Account a WHERE a.grade = 2")
    List<Object[]> findAllAccount(Sort sort);

}

package com.example.dto;

public class SimpleAccount {

    private Integer id;
    private String accountNumber;
    private String accountName;

    public SimpleAccount() {

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

}

实体Account 的字段比实体SimpleAccount 多。

在控制器上

package com.example.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.example.common.UtilityList;
import com.example.dto.SimpleAccount;
import com.example.entity.Account;
import com.example.repository.AccountRepository;

import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AccountController {

    @Autowired
    AccountRepository accountRepository;

    @RequestMapping(value = "/accounts_mulcol_json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String accountsMulColJSON() throws JsonProcessingException {
        List<Object[]> accountsList = accountRepository.findAllAccount(Sort.by("accountNumber"));
        List<SimpleAccount> simpleAccountList = new ArrayList<>();

如何将List&lt;Object[]&gt; 转换为List&lt;SimpleAccount&gt;

【问题讨论】:

    标签: java spring-data-jpa spring-data


    【解决方案1】:

    另一种解决方案是为SimpleAccount 类编写一个构造函数,如下所示:

    public SimpleAccount(Object[] objects) {
        setId((Integer) objects[0]);
        setAccountNumber((String) objects[1]);
        setAccountName((String) objects[2]);
    }
    

    然后像这样将List&lt;Object[]&gt; 映射到List&lt;SimpleAccount&gt;

    List<SimpleAccount> simpleAccountList = accountRepository.findAllAccount(Sort.by("accountNumber")).stream().map(SimpleAccount::new).collect(Collectors.toList());
    

    【讨论】:

      【解决方案2】:

      SimpleAccount创建额外的构造函数

      public SimpleAccount(Integer id, String accountNumber, String accountName) {
          ...
      }
      

      并以这种方式定义您的@Query

      @Query("SELECT new com.example.dto.SimpleAccount(a.id, a.accountNumber, a.accountName) FROM Account a WHERE a.grade = 2")
      List<SimpleAccount> findAllAccount(Sort sort);
      

      如果您需要手动将List&lt;Object[]&gt;转换为List&lt;SimpleAccount&gt;,请重新创建新的构造函数

      public SimpleAccount(Object[] data) {
          this.id = (Integer) data[0];
          this.accountNumber = (String) data[1];
          ...
      }
      

      现在,当您从数据库中获取 List&lt;Object[]&gt; 时,迭代列表并调用构造函数:

      List<Object[]> accountsList = accountRepository.findAllAccount(Sort.by("accountNumber"));
      List<SimpleAccount> simpleAccountList = new ArrayList<>();
      for (Object[] obj: accountsList) {
          simpleAccountList.add(new SimpleAccount(obj));
      }
      

      【讨论】:

      • 您的回答很有帮助。你能把List&lt;Object[]&gt;转换成List&lt;SimpleAccount&gt;吗?
      • 不需要这样做。春天有魔法。只需按原样复制粘贴解决方案即可。
      【解决方案3】:

      只需返回 Account 而不是 Object[]

      @Query("SELECT a FROM Account a WHERE a.grade = 2")
      List<Account> findAllAccount(Sort sort);
      

      【讨论】:

      • 我试图达到良好的性能。你能帮我选择几个字段,而不是选择所有字段吗?当我从多个表中查询时,我需要一个概括?
      猜你喜欢
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2013-04-01
      • 2016-01-03
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多