mtjmtj7的小站
mtjmtj7的小站
© mtjmtj7
All Rights Reserved.

Spring-data-jpa分页查询

1,dao层。要传入参数Pageable

@Query(value = "select new User(u.uid,u.username,u.usernickname,u.usertype,u.userimg,u.userregisttime,u.lastlogintime) from User u where u.usertype = ?1")
	public Page<User> findByUsertype(String userType,Pageable pageable);

2,service层。注意返回类型

	public Page<User> findByType(int page, int size){
		Pageable pageable = new PageRequest(page-1, size, Sort.Direction.ASC,"uid");
		Page<User> pageList = userDao.findByUsertype("1",pageable);
		return pageList;
	}

3,controller处理

	@RequestMapping("userGetAll")
	@ResponseBody
	public String usreGetAll(ModelMap map,int page, int limit) {
		Page<User> pageList = userService.findByType(page, limit);
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", pageList.getTotalElements());
		map.put("data", pageList.getContent());
		return JSONObject.toJSONString(map);
	}

打赏
2019-01-17
60 阅读
暂无评论

发表评论