java如何使用findById()搜索数据库中的字符串id?
如何按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);
}
}
# 1 楼答案
在Spring JPA
ETBestandserviceRepository
接口中添加一个Optional<ETBestandservice> findByName(String name)
方法,Spring将在启动时为您生成预期的查询如果查询可能返回多个结果,请将
Optional<ETBestandservice>
替换为List<ETBestandservice>
更多信息here