How to add cache feature in Spring Data JPA CRUDRepository

后端 未结 4 714
半阙折子戏
半阙折子戏 2020-12-31 04:27

I want to add \"Cacheable\" annotation in findOne method, and evict the cache when delete or happen methods happened.

How can I do that ?

4条回答
  •  温柔的废话
    2020-12-31 05:04

    I solved the this in the following way and its working fine


    public interface BookRepositoryCustom {
    
        Book findOne(Long id);
    
    }
    

    public class BookRepositoryImpl extends SimpleJpaRepository implements BookRepositoryCustom {
    
        @Inject
        public BookRepositoryImpl(EntityManager entityManager) {
            super(Book.class, entityManager);
        }
    
        @Cacheable(value = "books", key = "#id")
        public Book findOne(Long id) {
            return super.findOne(id);
        }
    
    }
    

    public interface BookRepository extends JpaRepository, BookRepositoryCustom {
    
    }
    

提交回复
热议问题