SpringBoot2.0+的SpringDataJpa中的findOne()变化

在撸SpringBoot2.0+的时候,JpaRepository中的findOne()传id报错

Inferred type 'S' for type parameter 'S' is not within its bound; 

SpringBoot 1.+的版本JpaRepository为

@NoRepositoryBean
public interface CrudRepository extends Repository {
     S save(S var1);

     Iterable save(Iterable var1);

    T findOne(ID var1);

    boolean exists(ID var1);

    Iterable findAll();

    Iterable findAll(Iterable var1);

    long count();

    void delete(ID var1);

    void delete(T var1);

    void delete(Iterable var1);

    void deleteAll();
}

SpringBoot2+版本的JpaRepository为

@NoRepositoryBean
public interface JpaRepository extends PagingAndSortingRepository, QueryByExampleExecutor {
    List findAll();

    List findAll(Sort var1);

    List findAllById(Iterable var1);

     List saveAll(Iterable var1);

    void flush();

     S saveAndFlush(S var1);

    void deleteInBatch(Iterable var1);

    void deleteAllInBatch();

    T getOne(ID var1);

     List findAll(Example var1);

     List findAll(Example var1, Sort var2);
}

新版本获取findOne()方法可用类似方法解决

  @Test
   public void findOneTest(){
        Product product = new Product();
        product.setCategoryId(1);
        Example example = Example.of(product);
        Optional productOptional = respository.findOne(example);

        if (productOptional.isPresent()) {
            Product productResult =   productOptional.get();
            System.out.println(productResult.toString());
        } else {
            // handle not found, return null or throw
            System.out.println("no exit!");
        }

    }

你可能感兴趣的:(SpringBoot2.0+的SpringDataJpa中的findOne()变化)