有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

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