《Netkiller Spring Cloud 手札》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);

转载于:https://my.oschina.net/neochen/blog/2247869

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值