如何在Java中将List <Object []>转换为List <SimpleAccount>?

时间:2019-07-01 14:50:20

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

我正在使用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

上的指南

我有存储库

where ColumnName != ''

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<Object[]>

3 个答案:

答案 0 :(得分:2)

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

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

然后将List<Object[]>映射到List<SimpleAccount>,如下所示:

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

答案 1 :(得分:1)

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<Object[]>转换为List<SimpleAccount>,请再次创建新的构造函数

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

现在,当您从数据库中获得List<Object[]>时,请遍历列表并调用contstructor:

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

答案 2 :(得分:0)

只需返回Account而不是Object []

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