master
parent
5485ca0b44
commit
182f6b97e9
|
|
@ -4,7 +4,12 @@ import com.zhangmeng.online.exam.dao.PermissionDao;
|
||||||
import com.zhangmeng.online.exam.dto.Result;
|
import com.zhangmeng.online.exam.dto.Result;
|
||||||
import com.zhangmeng.online.exam.entity.Permission;
|
import com.zhangmeng.online.exam.entity.Permission;
|
||||||
import com.zhangmeng.online.exam.entity.Role;
|
import com.zhangmeng.online.exam.entity.Role;
|
||||||
|
import com.zhangmeng.online.exam.utils.PageUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
|
@ -25,9 +30,26 @@ public class PermissionController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private PermissionDao permissionDao;
|
private PermissionDao permissionDao;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* // 基本分页(无排序)
|
||||||
|
* Pageable pageable = PageRequest.of(0, 10);
|
||||||
|
*
|
||||||
|
* // 分页+排序(单字段)
|
||||||
|
* Pageable pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "createTime");
|
||||||
|
*
|
||||||
|
* // 分页+多字段排序
|
||||||
|
* Sort sort = Sort.by(Sort.Order.asc("age"), Sort.Order.desc("name"));
|
||||||
|
* Pageable pageable = PageRequest.of(0, 10, sort);
|
||||||
|
* @param pageNum
|
||||||
|
* @param pageSize
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@RequestMapping("/list")
|
@RequestMapping("/list")
|
||||||
public Result list() {
|
public Result list(Integer pageNum, Integer pageSize) {
|
||||||
List<Permission> list = permissionDao.findAll();
|
|
||||||
|
Pageable pageable = PageRequest.of(PageUtils.getPageNum(pageNum), pageSize, Sort.Direction.DESC, "addTime");
|
||||||
|
Page<Permission> all = permissionDao.findAll(pageable);
|
||||||
|
List<Permission> list = all.getContent();
|
||||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||||
for (Permission permission : list) {
|
for (Permission permission : list) {
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
|
@ -37,6 +59,6 @@ public class PermissionController {
|
||||||
map.put("url", permission.getUrl());
|
map.put("url", permission.getUrl());
|
||||||
resultList.add(map);
|
resultList.add(map);
|
||||||
}
|
}
|
||||||
return Result.success(resultList);
|
return Result.success(pageNum, pageSize, all.getTotalElements(), resultList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ public class UserController {
|
||||||
private EntityManager entityManager;
|
private EntityManager entityManager;
|
||||||
|
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
public Result userList() {
|
public Result userList(Integer pageNum,Integer pageSize) {
|
||||||
List<User> list = userDao.findAll();
|
List<User> list = userDao.findAll();
|
||||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||||
for (User user : list) {
|
for (User user : list) {
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,17 @@ public class Result {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result success(int pageNum, int pageSize, long total, Object data) {
|
||||||
|
Result result = new Result();
|
||||||
|
result.setPageNum(pageNum);
|
||||||
|
result.setPageSize(pageSize);
|
||||||
|
result.setTotal(total);
|
||||||
|
result.setCode(SUCCESS);
|
||||||
|
result.setMessage("success");
|
||||||
|
result.setData(data);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static Result success(Object data) {
|
public static Result success(Object data) {
|
||||||
Result result = new Result();
|
Result result = new Result();
|
||||||
result.setCode(SUCCESS);
|
result.setCode(SUCCESS);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.zhangmeng.online.exam.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zm
|
||||||
|
* @date 2025/3/14 16:36
|
||||||
|
* @version: 1.0
|
||||||
|
*/
|
||||||
|
public class PageUtils {
|
||||||
|
|
||||||
|
public static int getPageNum(Integer pageNum){
|
||||||
|
if (pageNum == null){
|
||||||
|
pageNum = 0;
|
||||||
|
}else if (pageNum <= 0){
|
||||||
|
pageNum = 0;
|
||||||
|
}else {
|
||||||
|
pageNum = pageNum - 1;
|
||||||
|
}
|
||||||
|
return pageNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue