Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

9.3. MongoRepository

9.3.1. findAll()

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

9.3.2. deleteAll()

			
repository.deleteAll();
			
			

9.3.3. save()

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

9.3.4. count()

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

9.3.5. findByXXXX

			
List<User> findByName(String name);

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

9.3.6. StartingWith 和 EndingWith

			
List<User> findByNameStartingWith(String regexp);
List<User> findByNameEndingWith(String regexp);

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

9.3.7. Between

			
List<User> findByAgeBetween(int ageGT, int ageLT);

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

9.3.8. PageRequest

			
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
			

9.3.9. @Query