我的 API 中的 @query 有一些问题。 在我的数据库中,我有 1 个包含 5 列的表:id、imie、nazwisko、wiek、miasto。
我正在尝试对我的数据库进行自定义查询,然后我们开始 -> 错误。
当我使@Query 像这样时:
@Query(value = "SELECT * FROM osoba WHERE osoba.id = :id", nativeQuery = true)
结果:
{
"id": 1,
"imie": "Szymon",
"nazwisko": "Markowski",
"wiek": 55,
"miasto": "Sosnowiec"
}
并且知道我正在尝试进行另一个查询:
@Query(value = "SELECT imie FROM osoba WHERE osoba.id = :id", nativeQuery = true)
我们开始吧。它会产生如下问题:
2019-09-10 21:44:29.141 ERROR 8068 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Column 'id' not found.
2019-09-10 21:44:29.158 ERROR 8068 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT imie FROM osoba WHERE osoba.id = ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
有什么想法吗?
最佳答案
您的问题是您的存储库不知道您返回的列类型以及如何将它们与您的实体相匹配。首先,您需要更改自定义方法的返回类型,如下所示:
@Repository
public interface OsobaRepository extend JpaRepository<Osoba, Integer> {
@Query(value = "SELECT imie FROM osoba WHERE osoba.id = :id",
nativeQuery = true)
String findNameById(@Param("id") int id);
}
现在,如果您想要将结果从 String
转换为 Json
,在您的 OsobaServiceImpl
(如果它存在)中您需要创建一个实例Osoba
类,填充它并返回到 Controller ,如下所示:
@Service
public ServiceOsobaImpl implementation ServiceOsoba {
private final OsobaRepository osobaRepository;
@Autowired
public ServiceOsobaImpl(OsobaRepository osobaRepository) {
this.osobaRepository = osobaRepository;
}
// your methods
...
public Osoba findNameById(int id) {
String name = osobaRepository.findNameById(id);
Osoba osoba = new Osoba();
osoba.setName(name);
return osoba;
}
}
还有另一种选择,当您使用一个字段创建简单的 OsobaDto
并使用它时(类似于 previos 选项):
public OsobaDto implements Serializable {
private String name;
public OsobaDto() {}
//getter and setter
}
@Service
public ServiceOsobaImpl implementation ServiceOsoba {
private final OsobaRepository osobaRepository;
@Autowired
public ServiceOsobaImpl(OsobaRepository osobaRepository) {
this.osobaRepository = osobaRepository;
}
// your methods
...
public OsobaDto findNameById(int id) {
String name = osobaRepository.findNameById(id);
OsobaDto osobaDto = new OsobaDto();
osobaDto.setName(name);
return osobaDto;
}
}
两个选项的响应都是 json 格式的。当然,如果您在 Controller 方法上方指定 produces = MediaType.APPLICATION_JSON_VALUE
(或自己的类比变体)。
更新。
如果您使用 Osoba
类并且不想看到 null
值,请执行以下操作。
创建新界面:
public interface OnlyName {}
下一步是将您的实体标记为@Validated
,并将字段imie
注释为@JsonView
:
@Entity
@Validated
public class Osoba {
// your code
@JsonView(OnlyName.class)
private String imie;
}
和 Controller :
@RestController
@RequestMapping("/api/osoba")
public class OsobaAPI {
@Autowired
private OsobaRepo osobaRepo;
@JsonView(OnlyName.class)
@PostMapping
@RequestMapping("/getID")
public Osoba findOsobaById(@RequestParam("id") int id){
String name = osobaRepo.findNameById(id);
Osoba osoba = new Osoba();
osoba.setImie(name);
return osoba;
}
}
关于java - 使用 spring boot 对数据库的错误查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57877324/