gpt4 book ai didi

java - 如何使用 findById() 在数据库中搜索字符串而不是 id?

转载 作者:行者123 更新时间:2023-11-29 09:33:46 24 4
gpt4 key购买 nike

如何通过 ID 搜索字符串 Pathvariable 而不是整数?我喜欢寻找名字。

下面的方法效果很好,但只是将 ID 作为整数值。

谢谢:)


@RestController
@RequestMapping(value = "/etbestandservice")
public class ETBestandserviceController {

@Autowired
ETBestandserviceRepository etbestandRepos;

@GetMapping(value = "/find/{id}", produces = "application/json")
public Optional<ETBestandservice> findErsatzteile(@PathVariable("id") Integer id, ETBestandservice et) {

id = et.getId();
return etbestandRepos.findById(id);
}
}

最佳答案

添加Optional<ETBestandservice> findByName(String name) Spring JPA 中的方法 ETBestandserviceRepository接口(interface),Spring 将在启动时为您生成预期的查询。
如果查询可能返回多个结果,则替换 Optional<ETBestandservice>通过List<ETBestandservice> .
更多信息here

关于java - 如何使用 findById() 在数据库中搜索字符串而不是 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58137626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com