前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring data MongoDB 之 MongoRepository

Spring data MongoDB 之 MongoRepository

原创
作者头像
netkiller
修改2018-10-23 18:03:34
2.1K0
修改2018-10-23 18:03:34
举报
文章被收录于专栏:Netkiller

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。

代码语言:javascript
复制
@EnableMongoRepositories(basePackages = "cn.netkiller.repository")		
5.2.3.2. findAll()
代码语言:javascript
复制
	@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()
代码语言:javascript
复制
repository.deleteAll();
5.2.3.4. save()
代码语言:javascript
复制
repository.save(new City("Shenzhen", "China"));
5.2.3.5. count()
代码语言:javascript
复制
	@RequestMapping("count")
	public long count() {
		return repository.count();
	}
5.2.3.6. exists() 判断是否存在
代码语言:javascript
复制
boolean isExists = userRepository.exists(user.getId());			
5.2.3.7. existsById()
代码语言:javascript
复制
memberRepository.existsById(id);			
5.2.3.8. findByXXXX
代码语言:javascript
复制
List<User> findByName(String name);

List<User> users = userRepository.findByName("Eric");
5.2.3.9. findAll with Sort
代码语言:javascript
复制
List<User> users = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));			
5.2.3.10. FindAll with Pageable
代码语言:javascript
复制
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 旧版本
代码语言:javascript
复制
Page<User> findByLastname(String lastname, Pageable pageable);			
代码语言:javascript
复制
	@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条记录

代码语言:javascript
复制
					第一页 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
代码语言:javascript
复制
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
代码语言:javascript
复制
List<User> findByAgeBetween(int ageGT, int ageLT);

List<User> users = userRepository.findByAgeBetween(20, 50);
5.2.3.13. @Query
代码语言:javascript
复制
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);

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Netkiller Spring Cloud 手札
    • Spring Cloud Cookbook
      • Mr. Neo Chan, 陈景峯(BG7NYT)
      • 5.2.3. MongoRepository
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档