【问题标题】:How to search database for string instead id with findById()?如何使用 findById() 在数据库中搜索字符串而不是 id?
【发布时间】:2019-09-27 15:38:44
【问题描述】:

如何按 ID 搜索字符串 Pathvariable 而不是 Integer?我喜欢找名字。

下面的方法效果很好,但只是将 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);
    }
}

【问题讨论】:

  • 能否请您稍微澄清一下您的问题?

标签: java mysql spring-data-jpa findby


【解决方案1】:

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

【讨论】:

  • 我已经尝试过了,但它对我不起作用。我得到这个异常:UnsatisfiedDependencyException:创建名称为“ETBestandserviceController”的bean时出错:通过字段“etbestandRepos”表示的依赖关系不满足;
  • 这个异常确实与查询@volkanb有关
猜你喜欢
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
  • 2014-12-04
  • 1970-01-01
  • 2023-03-13
  • 2012-02-29
相关资源
最近更新 更多