1、首先创建DAO层接口,实现JpaRepository和JpaSpecificationExecutor两个接口
-
JpaRepository<SysOperLog, Integer>
泛型参数分别是:要查询的实体(Entity),实体的主键类型 -
JpaSpecificationExecutor<SysOperLog>
泛型参数:要查的实体
@Repository
public interface SysOperLogRepository extends JpaRepository<SysOperLog, Integer>, JpaSpecificationExecutor<SysOperLog> {
}
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;
}
如果要获得Page对象中的数据,可以直接使用page.getContent();
方法
List<SysOperLog> sysOperLogList = page.getContent();
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
}
}
标签:authman,false,分页,operDesc,JPA,查询,true,page From: https://blog.51cto.com/lenglingx/6393380