类型QueryByExampleExecutor <Contact>的方法findOne(Example <S>)不适用于参数(Long)

M.S*_*SIM 3 spring-data spring-data-jpa spring-boot

查找方法对我不起作用,但在其他项目中效果很好。

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo.entities.Contact;

public interface ContactRepository extends JpaRepository<Contact, Long>{

}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我打电话找一个,但给个工作。

@RequestMapping(value="/contact/{id}",method=RequestMethod.GET)
    public Contact getContact(@PathVariable Long id){
        return repo.findOne(id); //here give a error
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

一些CRUD Repository方法在Spring Data和

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
    T findOne(ID id);
Run Code Online (Sandbox Code Playgroud)

是其中之一。现在您应该使用

public interface CrudRepository<T, ID> extends Repository<T, ID> {
    Optional<T> findById(ID id);
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,哪些方法被重命名,请参阅此博客改进的命名库方法。

仍然有一个findOne方法,但这是从

public interface QueryByExampleExecutor<T> {
    <S extends T> Optional<S> findOne(Example<S> example);
Run Code Online (Sandbox Code Playgroud)

这也是SimpleJpaRepository的接口。这就是为什么出现错误的原因,因为此方法等待一个Example作为参数。