首页 > 其他分享 >使用JPA实现分页和模糊查询,以及jpa分页工具类

使用JPA实现分页和模糊查询,以及jpa分页工具类

时间:2023-08-03 22:07:51浏览次数:46  
标签:分页 jpa BigRole createTime JPA roleName null id roleCode


 1 、 首先创建DAO层接口,实现JpaRepository和JpaSpecificationExecutor两个接口

  • JpaRepository<SysOperLog, Integer> 泛型参数分别是:要查询的实体(Entity),实体的主键类型
  • JpaSpecificationExecutor<SysOperLog> 泛型参数:要查的实体

 

@Repository
public interface SysOperLogRepository extends JpaRepository<SysOperLog, Integer>, JpaSpecificationExecutor<SysOperLog> {

}

使用JPA实现分页和模糊查询,以及jpa分页工具类_数据

使用JPA实现分页和模糊查询,以及jpa分页工具类_分页_02

 

2 、 Service层进行查询操作

分页模糊查询三步骤:

  • 创建查询条件对象
  • 创建分页对象
  • 进行查询操作
/**
     * 分页查找SysOperLog
     *
     * @param query    查询条件
     * @param pagenum  页码
     * @param pageSize 每页显示的数据
     * @return
     */
	public Page<SysOperLog> pageQueryListLikeOperDesc(String query, Integer pageNo, Integer pageSize) {
		// 查询条件存在这个对象中
		Specification<SysOperLog> specification = new Specification<SysOperLog>() {
			// 重新Specification的toPredicate方法
			public Predicate toPredicate(Root<SysOperLog> root, CriteriaQuery<?> criteriaQuery,
					CriteriaBuilder criteriaBuilder) {
				// 我要模糊查询的字段是adminName
				Path operDesc = root.get("operDesc");
				// criteriaBuilder.like模糊查询,第一个参数是上一行的返回值,第二个参数是like('%xxx%')中,xxx的值
				Predicate predicate = criteriaBuilder.like(operDesc, "%" + query + "%");
				return predicate;
			}
		};
		//排序
		Sort sort = Sort.by(Sort.Direction.DESC, "id");
		// 分页条件存在这个对象中
		PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize, sort);
		// 进行查询操作,第一个参数是查询条件对象,第二个参数是分页对象
		Page<SysOperLog> page = sysOperLogRepository.findAll(specification, pageRequest);
		// 返回的数据都封装在了Page<SysOperLog>对象中
		return page;
	}

使用JPA实现分页和模糊查询,以及jpa分页工具类_数据_03

使用JPA实现分页和模糊查询,以及jpa分页工具类_java_04

如果要获得Page对象中的数据,可以直接使用page.getContent();方法

List<SysOperLog> sysOperLogList = page.getContent();

使用JPA实现分页和模糊查询,以及jpa分页工具类_分页_05


Page对象中封装的数据(下面data中就是page对象的数据)

{
	"success": true,
	"code": 200,
	"message": "OK",
	"data": {
		"content": [{
			"id": 47,
			"operModule": "角色管理",
			"operMethod": "com.laoxu.java.authman.controller.SysRoleController.saveMenu",
			"operType": "修改",
			"operDesc": "分配权限",
			"reqMethod": "POST",
			"operParam": "{roleId:15}\n{menuIds:[1, 3, 10, 11, 4, 14, 8, 9]}\n",
			"operIp": "0:0:0:0:0:0:0:1",
			"operUri": "/authman/sysRole/saveMenu",
			"operUser": "admin",
			"createTime": "2021-04-14 00:31:56"
		}, {
			"id": 46,
			"operModule": "角色管理",
			"operMethod": "com.laoxu.java.authman.controller.SysRoleController.saveMenu",
			"operType": "修改",
			"operDesc": "分配权限",
			"reqMethod": "POST",
			"operParam": "{roleId:15}\n{menuIds:[1, 3, 10, 11, 8, 9]}\n",
			"operIp": "0:0:0:0:0:0:0:1",
			"operUri": "/authman/sysRole/saveMenu",
			"operUser": "admin",
			"createTime": "2021-04-14 00:31:14"
		}, {
			"id": 28,
			"operModule": "部门管理",
			"operMethod": "com.laoxu.java.authman.controller.SysDeptController.remove",
			"operType": "删除",
			"operDesc": "删除部门",
			"reqMethod": "POST",
			"operParam": "{id:19}\n",
			"operIp": "0:0:0:0:0:0:0:1",
			"operUri": "/authman/sysDept/remove",
			"operUser": "admin",
			"createTime": "2021-04-13 23:50:08"
		}],
		"pageable": {
			"sort": {
				"empty": false,
				"sorted": true,
				"unsorted": false
			},
			"offset": 0,
			"pageSize": 20,
			"pageNumber": 0,
			"paged": true,
			"unpaged": false
		},
		"totalElements": 47,
		"totalPages": 3,
		"last": false,
		"number": 0,
		"size": 20,
		"sort": {
			"empty": false,
			"sorted": true,
			"unsorted": false
		},
		"numberOfElements": 20,
		"first": true,
		"empty": false
	}
}

使用JPA实现分页和模糊查询,以及jpa分页工具类_数据_06

使用JPA实现分页和模糊查询,以及jpa分页工具类_数据_07解释:

pageable为请求page参数;

totalElements这个是总记录数;

totalPages为总页数,起始页为0;

number为返回的表示的页面数,一般和pageable里一样;

size为返回的页面长度,一个和pageable李的一样;

numberOfElements,这个表示,当前返回页面数据内容的时间长度;

first是否是第一页;

last是否是最后一页;

empty是否为空;

sort为返回数据的排序情况;


参考他的:https://www.cnblogs.com/rxx1005/p/12364327.html

 

 

3 、 关于JPA的Page、Pageable、PageRequest、PageImpl、Sort

Page

Page是一个泛型接口,代表查询的单页结果集,也有其他信息。常用以下方法

int getTotalPages() //返回总的页数
long getTotalElements() //返回总行数
List<T> getContent() //返回查询结果集的List

Pageable

Pageable接口常常用于构造翻页查询,通常也有以下方法

int getPageNumber() //获取总页数
int getPageSize() //获取一页的行数
Pageable next() //返回Pageable类型的下一页
boolean hasPrevious() //是否有上一页

PageRequest

PageRequest是Pageable的实现类。用于构造分页查询需要的页码(从0开始)、每页行数、排序等,有如下构造方法

public static PageRequest(int page,int siez)
public static PageRequest(int page,int size,Sort sort)

PageImpl

PageImpl是Page的实现类,可以由List和其他参数构造一个PageImpl对象

PageImpl(List<T> content,Pageable pageable,long toal)//结果集的List,pageable,总行数total

Sort

Sort是一个用于排序的类型,默认升序排序,通常有以下用法

Sort sort = new Sort(Direction.DESC,"id");

 

 

 

4 、 PageImpl的使用

一句话,需要自己给定,content内容,需要自己构建page分页信息,还需要给页面总条数。

如果只传入content,那个,page就是默认的第一页,每页10条数据,页面总数为传入content这个列表的大小。

@ResponseBody
    @RequestMapping("/role/getAll01.html")
    public Result getAllRoles01() throws JsonProcessingException {
        List<BigRole> bigRoleList = new ArrayList<>();
        for (int i = 0; i < 66; i++) {
            BigRole bigRole = new BigRole();
            bigRole.setRoleCode("code"+i);
            bigRole.setRoleName("name"+i);
            bigRoleList.add(bigRole);
        }
        logger.info("bigRoleList: " + bigRoleList);

        Page page1 = new PageImpl(bigRoleList);
        logger.info("page1: " + page1);
        logger.info("page1: " + objectMapper.writeValueAsString(page1));

        PageRequest pageRequest1 = PageRequest.of(1,10);
        Page page2 = new PageImpl(bigRoleList, pageRequest1, bigRoleList.size());
        logger.info("page2: " + page2);
        logger.info("page2: " + objectMapper.writeValueAsString(page2));

        PageRequest pageRequest2 = PageRequest.of(2,10);
        Page page3 = new PageImpl(bigRoleList, pageRequest2, bigRoleList.size());
        logger.info("page3: " + page3);
        logger.info("page3: " + objectMapper.writeValueAsString(page3));

        PageRequest pageRequest3 = PageRequest.of(3,10);
        Page page4 = new PageImpl(bigRoleList.subList(3*10,4*10), pageRequest3, bigRoleList.size());
        logger.info("page4: " + page4);
        logger.info("page4: " + objectMapper.writeValueAsString(page4));

        PageRequest pageRequest4 = PageRequest.of(3,20);
        Page page5 = new PageImpl(bigRoleList.subList(3*20,bigRoleList.size()), pageRequest4, bigRoleList.size());
        logger.info("page5: " + page5);
        logger.info("page5: " + objectMapper.writeValueAsString(page5));

        return ResultUtils.success( page5 );
    }

使用JPA实现分页和模糊查询,以及jpa分页工具类_数据_08

输出:

2023-08-03 20:43:43.922 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:73 -bigRoleList: [BigRole(id=null, roleCode=code0, roleName=name0, createTime=null, createBy=null, updateTime=n
ull, updateBy=null), BigRole(id=null, roleCode=code1, roleName=name1, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code2, roleName=name2, createTime=null, createBy=null, updateTime=null,
 updateBy=null), BigRole(id=null, roleCode=code3, roleName=name3, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code4, roleName=name4, createTime=null, createBy=null, updateTime=null, upd
ateBy=null), BigRole(id=null, roleCode=code5, roleName=name5, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code6, roleName=name6, createTime=null, createBy=null, updateTime=null, updateB
y=null), BigRole(id=null, roleCode=code7, roleName=name7, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code8, roleName=name8, createTime=null, createBy=null, updateTime=null, updateBy=nu
ll), BigRole(id=null, roleCode=code9, roleName=name9, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code10, roleName=name10, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code11, roleName=name11, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code12, roleName=name12, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code13, roleName=name13, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code14, roleName=name14, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code15, roleName=name15, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code16, roleName=name16, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code17, roleName=name17, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code18, roleName=name18, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code19, roleName=name19, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code20, roleName=name20, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code21, roleName=name21, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code22, roleName=name22, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code23, roleName=name23, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code24, roleName=name24, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code25, roleName=name25, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code26, roleName=name26, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code27, roleName=name27, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code28, roleName=name28, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code29, roleName=name29, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code30, roleName=name30, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code31, roleName=name31, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code32, roleName=name32, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code33, roleName=name33, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code34, roleName=name34, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code35, roleName=name35, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code36, roleName=name36, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code37, roleName=name37, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code38, roleName=name38, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code39, roleName=name39, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code40, roleName=name40, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code41, roleName=name41, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code42, roleName=name42, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code43, roleName=name43, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code44, roleName=name44, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code45, roleName=name45, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code46, roleName=name46, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code47, roleName=name47, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code48, roleName=name48, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code49, roleName=name49, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code50, roleName=name50, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code51, roleName=name51, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code52, roleName=name52, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code53, roleName=name53, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code54, roleName=name54, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code55, roleName=name55, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code56, roleName=name56, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code57, roleName=name57, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code58, roleName=name58, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code59, roleName=name59, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code60, roleName=name60, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code61, roleName=name61, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code62, roleName=name62, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code63, roleName=name63, createTime=null, createBy=null, updateTime=null, updateBy=null), BigRole(id=null, roleCode=code64, roleName=name64, createTime=null, createBy=null, updateTime=null, updateBy=null
), BigRole(id=null, roleCode=code65, roleName=name65, createTime=null, createBy=null, updateTime=null, updateBy=null)]
2023-08-03 20:43:43.928 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:76 -page1: Page 1 of 1 containing com.cqsym.bigscreen.entity.BigRole instances
2023-08-03 20:43:44.009 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:77 -page1: {"content":[{"roleCode":"code0","roleName":"name0"},{"roleCode":"code1","roleName":"name1"},{"roleCo
de":"code2","roleName":"name2"},{"roleCode":"code3","roleName":"name3"},{"roleCode":"code4","roleName":"name4"},{"roleCode":"code5","roleName":"name5"},{"roleCode":"code6","roleName":"name6"},{"roleCode":"code7","roleName":"name7"},
{"roleCode":"code8","roleName":"name8"},{"roleCode":"code9","roleName":"name9"},{"roleCode":"code10","roleName":"name10"},{"roleCode":"code11","roleName":"name11"},{"roleCode":"code12","roleName":"name12"},{"roleCode":"code13","role
Name":"name13"},{"roleCode":"code14","roleName":"name14"},{"roleCode":"code15","roleName":"name15"},{"roleCode":"code16","roleName":"name16"},{"roleCode":"code17","roleName":"name17"},{"roleCode":"code18","roleName":"name18"},{"role
Code":"code19","roleName":"name19"},{"roleCode":"code20","roleName":"name20"},{"roleCode":"code21","roleName":"name21"},{"roleCode":"code22","roleName":"name22"},{"roleCode":"code23","roleName":"name23"},{"roleCode":"code24","roleNa
me":"name24"},{"roleCode":"code25","roleName":"name25"},{"roleCode":"code26","roleName":"name26"},{"roleCode":"code27","roleName":"name27"},{"roleCode":"code28","roleName":"name28"},{"roleCode":"code29","roleName":"name29"},{"roleCo
de":"code30","roleName":"name30"},{"roleCode":"code31","roleName":"name31"},{"roleCode":"code32","roleName":"name32"},{"roleCode":"code33","roleName":"name33"},{"roleCode":"code34","roleName":"name34"},{"roleCode":"code35","roleName
":"name35"},{"roleCode":"code36","roleName":"name36"},{"roleCode":"code37","roleName":"name37"},{"roleCode":"code38","roleName":"name38"},{"roleCode":"code39","roleName":"name39"},{"roleCode":"code40","roleName":"name40"},{"roleCode
":"code41","roleName":"name41"},{"roleCode":"code42","roleName":"name42"},{"roleCode":"code43","roleName":"name43"},{"roleCode":"code44","roleName":"name44"},{"roleCode":"code45","roleName":"name45"},{"roleCode":"code46","roleName":
"name46"},{"roleCode":"code47","roleName":"name47"},{"roleCode":"code48","roleName":"name48"},{"roleCode":"code49","roleName":"name49"},{"roleCode":"code50","roleName":"name50"},{"roleCode":"code51","roleName":"name51"},{"roleCode":
"code52","roleName":"name52"},{"roleCode":"code53","roleName":"name53"},{"roleCode":"code54","roleName":"name54"},{"roleCode":"code55","roleName":"name55"},{"roleCode":"code56","roleName":"name56"},{"roleCode":"code57","roleName":"n
ame57"},{"roleCode":"code58","roleName":"name58"},{"roleCode":"code59","roleName":"name59"},{"roleCode":"code60","roleName":"name60"},{"roleCode":"code61","roleName":"name61"},{"roleCode":"code62","roleName":"name62"},{"roleCode":"c
ode63","roleName":"name63"},{"roleCode":"code64","roleName":"name64"},{"roleCode":"code65","roleName":"name65"}],"pageable":"INSTANCE","last":true,"totalPages":1,"totalElements":66,"number":0,"size":66,"sort":{"empty":true,"sorted":
false,"unsorted":true},"numberOfElements":66,"first":true,"empty":false}
2023-08-03 20:43:44.013 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:81 -page2: Page 2 of 7 containing com.cqsym.bigscreen.entity.BigRole instances
2023-08-03 20:43:44.022 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:82 -page2: {"content":[{"roleCode":"code0","roleName":"name0"},{"roleCode":"code1","roleName":"name1"},{"roleCo
de":"code2","roleName":"name2"},{"roleCode":"code3","roleName":"name3"},{"roleCode":"code4","roleName":"name4"},{"roleCode":"code5","roleName":"name5"},{"roleCode":"code6","roleName":"name6"},{"roleCode":"code7","roleName":"name7"},
{"roleCode":"code8","roleName":"name8"},{"roleCode":"code9","roleName":"name9"},{"roleCode":"code10","roleName":"name10"},{"roleCode":"code11","roleName":"name11"},{"roleCode":"code12","roleName":"name12"},{"roleCode":"code13","role
Name":"name13"},{"roleCode":"code14","roleName":"name14"},{"roleCode":"code15","roleName":"name15"},{"roleCode":"code16","roleName":"name16"},{"roleCode":"code17","roleName":"name17"},{"roleCode":"code18","roleName":"name18"},{"role
Code":"code19","roleName":"name19"},{"roleCode":"code20","roleName":"name20"},{"roleCode":"code21","roleName":"name21"},{"roleCode":"code22","roleName":"name22"},{"roleCode":"code23","roleName":"name23"},{"roleCode":"code24","roleNa
me":"name24"},{"roleCode":"code25","roleName":"name25"},{"roleCode":"code26","roleName":"name26"},{"roleCode":"code27","roleName":"name27"},{"roleCode":"code28","roleName":"name28"},{"roleCode":"code29","roleName":"name29"},{"roleCo
de":"code30","roleName":"name30"},{"roleCode":"code31","roleName":"name31"},{"roleCode":"code32","roleName":"name32"},{"roleCode":"code33","roleName":"name33"},{"roleCode":"code34","roleName":"name34"},{"roleCode":"code35","roleName
":"name35"},{"roleCode":"code36","roleName":"name36"},{"roleCode":"code37","roleName":"name37"},{"roleCode":"code38","roleName":"name38"},{"roleCode":"code39","roleName":"name39"},{"roleCode":"code40","roleName":"name40"},{"roleCode
":"code41","roleName":"name41"},{"roleCode":"code42","roleName":"name42"},{"roleCode":"code43","roleName":"name43"},{"roleCode":"code44","roleName":"name44"},{"roleCode":"code45","roleName":"name45"},{"roleCode":"code46","roleName":
"name46"},{"roleCode":"code47","roleName":"name47"},{"roleCode":"code48","roleName":"name48"},{"roleCode":"code49","roleName":"name49"},{"roleCode":"code50","roleName":"name50"},{"roleCode":"code51","roleName":"name51"},{"roleCode":
"code52","roleName":"name52"},{"roleCode":"code53","roleName":"name53"},{"roleCode":"code54","roleName":"name54"},{"roleCode":"code55","roleName":"name55"},{"roleCode":"code56","roleName":"name56"},{"roleCode":"code57","roleName":"n
ame57"},{"roleCode":"code58","roleName":"name58"},{"roleCode":"code59","roleName":"name59"},{"roleCode":"code60","roleName":"name60"},{"roleCode":"code61","roleName":"name61"},{"roleCode":"code62","roleName":"name62"},{"roleCode":"c
ode63","roleName":"name63"},{"roleCode":"code64","roleName":"name64"},{"roleCode":"code65","roleName":"name65"}],"pageable":{"sort":{"empty":true,"sorted":false,"unsorted":true},"offset":10,"pageSize":10,"pageNumber":1,"paged":true,
"unpaged":false},"last":false,"totalPages":7,"totalElements":66,"number":1,"size":10,"sort":{"empty":true,"sorted":false,"unsorted":true},"numberOfElements":66,"first":false,"empty":false}
2023-08-03 20:43:44.029 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:86 -page3: Page 3 of 7 containing com.cqsym.bigscreen.entity.BigRole instances
2023-08-03 20:43:44.030 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:87 -page3: {"content":[{"roleCode":"code0","roleName":"name0"},{"roleCode":"code1","roleName":"name1"},{"roleCo
de":"code2","roleName":"name2"},{"roleCode":"code3","roleName":"name3"},{"roleCode":"code4","roleName":"name4"},{"roleCode":"code5","roleName":"name5"},{"roleCode":"code6","roleName":"name6"},{"roleCode":"code7","roleName":"name7"},
{"roleCode":"code8","roleName":"name8"},{"roleCode":"code9","roleName":"name9"},{"roleCode":"code10","roleName":"name10"},{"roleCode":"code11","roleName":"name11"},{"roleCode":"code12","roleName":"name12"},{"roleCode":"code13","role
Name":"name13"},{"roleCode":"code14","roleName":"name14"},{"roleCode":"code15","roleName":"name15"},{"roleCode":"code16","roleName":"name16"},{"roleCode":"code17","roleName":"name17"},{"roleCode":"code18","roleName":"name18"},{"role
Code":"code19","roleName":"name19"},{"roleCode":"code20","roleName":"name20"},{"roleCode":"code21","roleName":"name21"},{"roleCode":"code22","roleName":"name22"},{"roleCode":"code23","roleName":"name23"},{"roleCode":"code24","roleNa
me":"name24"},{"roleCode":"code25","roleName":"name25"},{"roleCode":"code26","roleName":"name26"},{"roleCode":"code27","roleName":"name27"},{"roleCode":"code28","roleName":"name28"},{"roleCode":"code29","roleName":"name29"},{"roleCo
de":"code30","roleName":"name30"},{"roleCode":"code31","roleName":"name31"},{"roleCode":"code32","roleName":"name32"},{"roleCode":"code33","roleName":"name33"},{"roleCode":"code34","roleName":"name34"},{"roleCode":"code35","roleName
":"name35"},{"roleCode":"code36","roleName":"name36"},{"roleCode":"code37","roleName":"name37"},{"roleCode":"code38","roleName":"name38"},{"roleCode":"code39","roleName":"name39"},{"roleCode":"code40","roleName":"name40"},{"roleCode
":"code41","roleName":"name41"},{"roleCode":"code42","roleName":"name42"},{"roleCode":"code43","roleName":"name43"},{"roleCode":"code44","roleName":"name44"},{"roleCode":"code45","roleName":"name45"},{"roleCode":"code46","roleName":
"name46"},{"roleCode":"code47","roleName":"name47"},{"roleCode":"code48","roleName":"name48"},{"roleCode":"code49","roleName":"name49"},{"roleCode":"code50","roleName":"name50"},{"roleCode":"code51","roleName":"name51"},{"roleCode":
"code52","roleName":"name52"},{"roleCode":"code53","roleName":"name53"},{"roleCode":"code54","roleName":"name54"},{"roleCode":"code55","roleName":"name55"},{"roleCode":"code56","roleName":"name56"},{"roleCode":"code57","roleName":"n
ame57"},{"roleCode":"code58","roleName":"name58"},{"roleCode":"code59","roleName":"name59"},{"roleCode":"code60","roleName":"name60"},{"roleCode":"code61","roleName":"name61"},{"roleCode":"code62","roleName":"name62"},{"roleCode":"c
ode63","roleName":"name63"},{"roleCode":"code64","roleName":"name64"},{"roleCode":"code65","roleName":"name65"}],"pageable":{"sort":{"empty":true,"sorted":false,"unsorted":true},"offset":20,"pageSize":10,"pageNumber":2,"paged":true,
"unpaged":false},"last":false,"totalPages":7,"totalElements":66,"number":2,"size":10,"sort":{"empty":true,"sorted":false,"unsorted":true},"numberOfElements":66,"first":false,"empty":false}
2023-08-03 20:43:44.034 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:91 -page4: Page 4 of 7 containing com.cqsym.bigscreen.entity.BigRole instances
2023-08-03 20:43:44.036 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:92 -page4: {"content":[{"roleCode":"code30","roleName":"name30"},{"roleCode":"code31","roleName":"name31"},{"ro
leCode":"code32","roleName":"name32"},{"roleCode":"code33","roleName":"name33"},{"roleCode":"code34","roleName":"name34"},{"roleCode":"code35","roleName":"name35"},{"roleCode":"code36","roleName":"name36"},{"roleCode":"code37","role
Name":"name37"},{"roleCode":"code38","roleName":"name38"},{"roleCode":"code39","roleName":"name39"}],"pageable":{"sort":{"empty":true,"sorted":false,"unsorted":true},"offset":30,"pageSize":10,"pageNumber":3,"paged":true,"unpaged":fa
lse},"last":false,"totalPages":7,"totalElements":66,"number":3,"size":10,"sort":{"empty":true,"sorted":false,"unsorted":true},"numberOfElements":10,"first":false,"empty":false}
2023-08-03 20:43:44.037 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:96 -page5: Page 4 of 4 containing com.cqsym.bigscreen.entity.BigRole instances
2023-08-03 20:43:44.038 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles01:97 -page5: {"content":[{"roleCode":"code60","roleName":"name60"},{"roleCode":"code61","roleName":"name61"},{"ro
leCode":"code62","roleName":"name62"},{"roleCode":"code63","roleName":"name63"},{"roleCode":"code64","roleName":"name64"},{"roleCode":"code65","roleName":"name65"}],"pageable":{"sort":{"empty":true,"sorted":false,"unsorted":true},"o
ffset":60,"pageSize":20,"pageNumber":3,"paged":true,"unpaged":false},"last":true,"totalPages":4,"totalElements":66,"number":3,"size":20,"sort":{"empty":true,"sorted":false,"unsorted":true},"numberOfElements":6,"first":false,"empty":
false}

使用JPA实现分页和模糊查询,以及jpa分页工具类_数据_09

使用JPA实现分页和模糊查询,以及jpa分页工具类_分页_10

@ResponseBody
    @RequestMapping("/role/getAll02.html")
    public Result getAllRoles02(@RequestBody(required = false) PageVo pageVo) {
        if (null == pageVo) {
            logger.info("pageVo is null");
            PageVo pageVo0 = new PageVo(1, 10);
            pageVo = pageVo0;
        }
        logger.info(pageVo.toString());
        List<BigRole> bigRoleList = bigRoleService.findAll();
        Page page = new PageImpl(bigRoleList, PageRequest.of(pageVo.getPageNum()-1, pageVo.getPageSize()), bigRoleList.size());
        return ResultUtils.success( page );

 输出:

2023-08-03 21:15:16.557 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles02:68 -pageVo is null
2023-08-03 21:15:16.558 INFO [http-nio-8899-exec-1]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles02:72 -PageVo{pageNum=1, pageSize=10}
2023-08-03 21:17:22.970 INFO [http-nio-8899-exec-3]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles02:72 -PageVo{pageNum=1, pageSize=10}
2023-08-03 21:17:54.290 INFO [http-nio-8899-exec-4]c.cqsym.bigscreen.controller.BigUserRoleMenusController.getAllRoles02:72 -PageVo{pageNum=2, pageSize=20}

不传页面会报pageVo is null; 请求页面的是2(第二页),构建pageRequest时使用的1,需要减1;



5 、 PageImpl工具类


import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

public class JpaPageUtils {
	
	private static Integer defaultPageSize = 10;

	public static <T> Page<T> listToPage(List<T> list, int pageNo) {
		PageRequest pageable = PageRequest.of(pageNo - 1, defaultPageSize);
		return getPage(list, pageable);
	}
	
	public static <T> Page<T> listToPage(List<T> list, int pageNo, int pageSize) {
		PageRequest pageable = PageRequest.of(pageNo - 1, pageSize);
		return getPage(list, pageable);
	}
	
	public static <T> Page<T> getPage(List<T> list) {
		PageRequest pageable = PageRequest.of(0, defaultPageSize);
		return getPage(list, pageable);
	}

	public static <T> Page<T> getPage(List<T> list, Pageable pageable) {
		// 第n页起始值
		int pageStart = pageable.getPageNumber() * pageable.getPageSize();
		// 第n页期望结尾值
		int expectPageEnd = pageStart + pageable.getPageSize() - 1;
		return Optional.of(list.size()).filter(size -> size >= pageStart)
				.map(size -> getRealSubList(pageStart, expectPageEnd, list, pageable))
				.orElseGet(() -> getEmptySubList(list, pageable));
	}

	private static <T> PageImpl<T> getRealSubList(int pageStart, int expectPageEnd, List<T> list, Pageable pageable) {
		int realPageEnd = (list.size() > expectPageEnd) ? expectPageEnd + 1	: list.size();
		return new PageImpl<>(list.subList(pageStart, realPageEnd), pageable, list.size());
	}

	private static <T> PageImpl<T> getEmptySubList(List<T> list, Pageable pageable) {
		return new PageImpl<>(new ArrayList(), pageable, list.size());
	}

}

标签:分页,jpa,BigRole,createTime,JPA,roleName,null,id,roleCode
From: https://blog.51cto.com/lenglingx/6953365

相关文章

  • jpa 分页工具类,把 list 转 page 的工具类
    JpaPageUtils.java工具类importjava.util.ArrayList;importjava.util.List;importjava.util.Optional;importorg.springframework.data.domain.Page;importorg.springframework.data.domain.PageImpl;importorg.springframework.data.domain.PageRequest;importo......
  • Spring Data JPA 的开发神级IDEA插件 -- JPA Buddy
    1.概述JPABuddy是一个广泛使用的IntelliJIDEA插件,面向使用JPA数据模型和相关技术(如SpringDataJPA,DB版本控制工具(Flyway,Liquibase),MapStruct等)的新手和有经验的开发人员。该插件提供了可视化设计器、代码生成和其他检查,这些检查应根据JPA的最佳实践简化开发并改进代码。该插......
  • 千万条据下的分页
        1.1. 背景对于开发来说,分页功能碰到的频率还是算蛮高的,基本上在每个模块中都需要都会遇到列表分页的功能。他们实现的都很快,因为基本上只要把之前的代码改改就OK了。他们的实现基本是是如下语句: SELECT*FROMgoodsWHERE......
  • JPA配置实体时 insertable = false, updatable = false
    @Excel(name="创建时间",format="yyyy-MM-ddHH:mm:ss",width=20)@Column(name="created_time",insertable=false,columnDefinition="timestampdefaultcurrent_timestamp")@Temporal(TemporalType.TIMES......
  • (转)WEB页面导出为Word文档后分页&横向打印的方法
    项目中用到了横向打印,今天重新更新了这个脚本.<html><HEAD><title>WEB页面导出为Word文档后分页&横向打印的方法</title></HEAD><SCRIPTLANGUAGE="javascript">......
  • 员工分页查询_代码完善
           ......
  • Dao继承JpaRepository 、 JpaSpecificationExecutor 接口
    @RepositorypublicinterfaceUserRepositoryextendsJpaRepository<User,Integer>{xxx...}关于Dao继承JpaRepository:前要:Jpa:JPA是Spring提供的一种ORM,ORM:对象关系映射(ObjectRelationalMapping,简称ORM),简单的说,ORM是通过使用描述对象和数据库之间映射的......
  • 员工分页查询_代码开发
             ......
  • rest-apiV2.0.0升级为simplest-api开源框架生态之simplest-jpa发布
    什么是simplestsimplest追求存粹简单和极致。旨在为项目快速开发提供一系列的基础能力,方便用户根据项目需求快速进行功能拓展不在去关心一些繁琐。重复工作,而是把重点聚焦到业务。前言程序10年。作为一个多年程序。深知每个项目和程序,都有很多重复性工作要做。入行近10......
  • JPA实体类映射PostgreSQL中的jsonb字段
    前言有时候我们需要在PostgreSQL表中存储jsonb类型的数据,JPA实体类中如何定义这个属性与之对应呢?本篇介绍两种方式:①自定义数据库方言和自定义类型②引入hibernate-types依赖方式一自定义数据库方言和自定义类型自定义方言publicclassCustomPostgreSqlDialect......