如何覆盖Mongo存储库的{find}方法

时间:2015-10-09 15:43:03

标签: spring spring-mvc

我正在尝试覆盖mongo存储库的findAll(Pageable pageable)方法,但不能完全实现。

我创建了一个名为MyRestRepository的存储库,如下所示:

public interface MyRestRepository extends MongoRepository<Notify, String>, MyRestRepositoryCustom {
   ...
}

MyRestRepositoryCustom界面中,我添加了以下代码:

@Modifying
Page<Notify> findAll(Pageable pageable);

最后我实现了MyRestRepositoryCustom

public class MyRestRepositoryImpl implements MyRestRepositoryCustom {
 @Override
 public Page<Notify> findAll(Pageable pageable) {
      // This doesn't work
      Page<Notify> results = super.findAll(pageable);
      // Do other stuff here to results...
      return results;

}

我想在原始存储库上调用findAll方法,以便我可以获取页面的内容然后进行修改。但是,这不起作用。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

MyRestRepositoryImpl必须延长SimpleMongoRepository。您不必在界面中定义findAll

public class MyRestRepositoryImpl extends SimpleMongoRepository implements MyRestRepositoryCustom {
 //call the super instructor
 public   MyRestRepositoryImpl(....) {
       super(....);
 }

 @Override
 public Page<Notify> findAll(Pageable pageable) {
      // This doesn't work
      Page<Notify> results = super.findAll(pageable);
      // Do other stuff here to results...
      return results;

}
}