當前位置:
首頁 > 知識 > Spring data MongoDB 之 MongoRepository

Spring data MongoDB 之 MongoRepository

Netkiller Spring Cloud 手札

Spring Cloud Cookbook

Mr. Neo Chan, 陳景峯(BG7NYT)

中國廣東省深圳市望海路半島城邦三期 518067 +86 13113668890 <netkiller@msn.com>

$Id: book.xml 606 2013-05-29 09:52:58Z netkiller $

版權 ? 2015-2018 Neo Chan

版權聲明

轉載請與作者聯繫,轉載時請務必標明文章原始出處和作者信息及本聲明。

http://www.netkiller.cn

http://netkiller.github.io

http://netkiller.sourceforge.net

我的系列文檔

編程語言

Netkiller Architect 手札

Netkiller Developer 手札

Netkiller Java 手札

Netkiller Spring 手札

Netkiller PHP 手札

Netkiller Python 手札

Netkiller Testing 手札

Netkiller Cryptography 手札

Netkiller Perl 手札

Netkiller Docbook 手札

Netkiller Project 手札

Netkiller Database 手札

5.2.3. MongoRepository

5.2.3.1. 掃描倉庫介面

默認不需要設置,除非你的包不在當前包下,或者命令不是 repository。

@EnableMongoRepositories(basePackages = "cn.netkiller.repository")

5.2.3.2. findAll()

@RequestMapping(value = "read", method = RequestMethod.GET, produces = { "application/xml", "application/json" })
@ResponseStatus(HttpStatus.OK)
public List<Withdraw> read() {
return repository.findAll();
}

5.2.3.3. deleteAll()

repository.deleteAll();

5.2.3.4. save()

repository.save(new City("Shenzhen", "China"));

5.2.3.5. count()

@RequestMapping("count")
public long count() {
return repository.count();
}

5.2.3.6. exists() 判斷是否存在

boolean isExists = userRepository.exists(user.getId());

5.2.3.7. existsById()

memberRepository.existsById(id);

5.2.3.8. findByXXXX

List<User> findByName(String name);
List<User> users = userRepository.findByName("Eric");

5.2.3.9. findAll with Sort

List<User> users = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));

5.2.3.10. FindAll with Pageable

Pageable pageable = PageRequest.of(0, 1);
Page<User> page = userRepository.findAll(pageable);
List<User> users = pages.getContent();

5.2.3.10.1. PageRequest - springboot 1.x 舊版本

Page<User> findByLastname(String lastname, Pageable pageable);
@RequestMapping(value = "read/{size}/{page}", method = RequestMethod.GET, produces = { "application/xml", "application/json" })
@ResponseStatus(HttpStatus.OK)
public List<Withdraw> readPage(@PathVariable int size, @PathVariable int page){
PageRequest pageRequest = new PageRequest(page-1,size);
return repository.findAll(pageRequest).getContent();
}

URL翻頁參數,每次返回10條記錄

第一頁 http://localhost:8080/v1/withdraw/read/10/1.json
第二頁 http://localhost:8080/v1/withdraw/read/10/2.json
...
第五頁 http://localhost:8080/v1/withdraw/read/10/5.json

5.2.3.11. StartingWith 和 EndingWith

List<User> findByNameStartingWith(String regexp);
List<User> findByNameEndingWith(String regexp);
List<User> users = userRepository.findByNameStartingWith("N");
List<User> users = userRepository.findByNameEndingWith("o");

5.2.3.12. Between

List<User> findByAgeBetween(int ageGT, int ageLT);
List<User> users = userRepository.findByAgeBetween(20, 50);

5.2.3.13. @Query

public interface PersonRepository extends MongoRepository<Person, String> {
@Query("{ "name" : ?0 }")
List<Person> findWithQuery(String userId);
}
@Query(value = "{"statusHistories":{$elemMatch:{"status":{$in:["PROCESSABLE"]}}},"created" : { "$gt" : { "$date" : ":?0" } , "$lt" : { "$date" : ":?1"}}}", count = true)
Long countMe(@Param("dateFrom") Date datefrom, @Param("dateTo") Date dateTo);


Spring data MongoDB 之 MongoRepository

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

C井——分享幾種常用的編碼轉換,base64、MD5、string
深入理解Git的實現原理

TAG:程序員小新人學習 |