美文网首页
SpringBoot2.0+的SpringDataJpa中的fi

SpringBoot2.0+的SpringDataJpa中的fi

作者: 小程序 | 来源:发表于2018-04-02 01:24 被阅读1197次

    在撸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!");
            }
    
        }
    

    相关文章

      网友评论

          本文标题:SpringBoot2.0+的SpringDataJpa中的fi

          本文链接:https://www.haomeiwen.com/subject/lrozcftx.html