在擼SpringBoot2.0+的時候车胡,JpaRepository中的findOne()傳id報錯
Inferred type 'S' for type parameter 'S' is not within its bound;
SpringBoot 1.+的版本JpaRepository為
@NoRepositoryBean
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
<S extends T> S save(S var1);
<S extends T> Iterable<S> save(Iterable<S> var1);
T findOne(ID var1);
boolean exists(ID var1);
Iterable<T> findAll();
Iterable<T> findAll(Iterable<ID> var1);
long count();
void delete(ID var1);
void delete(T var1);
void delete(Iterable<? extends T> var1);
void deleteAll();
}
SpringBoot2+版本的JpaRepository為
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
List<T> findAll();
List<T> findAll(Sort var1);
List<T> findAllById(Iterable<ID> var1);
<S extends T> List<S> saveAll(Iterable<S> var1);
void flush();
<S extends T> S saveAndFlush(S var1);
void deleteInBatch(Iterable<T> var1);
void deleteAllInBatch();
T getOne(ID var1);
<S extends T> List<S> findAll(Example<S> var1);
<S extends T> List<S> findAll(Example<S> var1, Sort var2);
}
新版本獲取findOne()方法可用類似方法解決
@Test
public void findOneTest(){
Product product = new Product();
product.setCategoryId(1);
Example<Product> example = Example.of(product);
Optional<Product> 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!");
}
}