用户、角色、权限模糊查询优化;权限列表页数据接口性能优化

This commit is contained in:
godchao 2019-08-14 15:12:01 +08:00
parent a07b0d9471
commit 7c4278a3a4
9 changed files with 145 additions and 18 deletions

View File

@ -271,6 +271,56 @@ public class BeanUtils {
return allListMap; return allListMap;
} }
/***
* Key-Object-List列表Map
* @param allLists
* @param fields
* @param <T>
* @return
*/
public static <T> Map<String, List<T>> convertToStringKeyObjectListMap(List<T> allLists, String... fields){
if (allLists == null || allLists.isEmpty()){
return null;
}
Map<String, List<T>> allListMap = new LinkedHashMap<>(allLists.size());
// 转换为map
try {
for (T model : allLists){
String key = null;
if(V.isEmpty(fields)){
//未指定字段以id为key
key = getStringProperty(model, Cons.FieldName.id.name());
}
// 指定了一个字段以该字段为key类型同该字段
else if(fields.length == 1){
key = getStringProperty(model, fields[0]);
}
else{ // 指定了多个字段以字段S.join的结果为key类型为String
List list = new ArrayList();
for(String fld : fields){
list.add(getProperty(model, fld));
}
key = S.join(list);
}
if(key != null){
List<T> list = allListMap.get(key);
if (list == null){
list = new ArrayList<T>();
allListMap.put(key, list);
}
list.add(model);
}
else{
log.warn(model.getClass().getName() + " 的属性 "+fields[0]+" 值存在 null转换结果需要确认!");
}
}
} catch (Exception e){
log.warn("转换key-model-list异常", e);
}
return allListMap;
}
/*** /***
* 构建上下级关联的树形结构的model * 构建上下级关联的树形结构的model
* @param allModels * @param allModels

View File

@ -12,6 +12,7 @@ import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.KeyValue; import com.diboot.core.vo.KeyValue;
import com.diboot.core.vo.Pagination; import com.diboot.core.vo.Pagination;
import com.diboot.core.vo.Status; import com.diboot.core.vo.Status;
import com.diboot.example.dto.SysUserDto;
import com.diboot.example.entity.Department; import com.diboot.example.entity.Department;
import com.diboot.example.entity.SysUser; import com.diboot.example.entity.SysUser;
import com.diboot.example.entity.User; import com.diboot.example.entity.User;
@ -60,8 +61,8 @@ public class SysUserController extends BaseCrudRestController {
@GetMapping("/list") @GetMapping("/list")
@AuthorizationWrapper(value = @RequiresPermissions("list"), name = "列表") @AuthorizationWrapper(value = @RequiresPermissions("list"), name = "列表")
public JsonResult getVOList(SysUser sysUser, Pagination pagination, HttpServletRequest request) throws Exception{ public JsonResult getVOList(SysUserDto sysUserDto, Pagination pagination, HttpServletRequest request) throws Exception{
QueryWrapper<User> queryWrapper = super.buildQueryWrapper(sysUser); QueryWrapper<SysUserDto> queryWrapper = super.buildQueryWrapper(sysUserDto);
return super.getVOListWithPaging(queryWrapper, pagination, SysUserVO.class); return super.getVOListWithPaging(queryWrapper, pagination, SysUserVO.class);
} }

View File

@ -0,0 +1,16 @@
package com.diboot.example.dto;
import com.diboot.core.binding.query.BindQuery;
import com.diboot.core.binding.query.Comparison;
import lombok.Data;
@Data
public class SysUserDto {
@BindQuery(comparison = Comparison.LIKE)
private String username;
@BindQuery(comparison = Comparison.EQ)
private String status;
}

View File

@ -9,6 +9,7 @@ import com.diboot.core.vo.Pagination;
import com.diboot.core.vo.Status; import com.diboot.core.vo.Status;
import com.diboot.shiro.authz.annotation.AuthorizationPrefix; import com.diboot.shiro.authz.annotation.AuthorizationPrefix;
import com.diboot.shiro.authz.annotation.AuthorizationWrapper; import com.diboot.shiro.authz.annotation.AuthorizationWrapper;
import com.diboot.shiro.dto.PermissionDto;
import com.diboot.shiro.entity.Permission; import com.diboot.shiro.entity.Permission;
import com.diboot.shiro.service.PermissionService; import com.diboot.shiro.service.PermissionService;
import com.diboot.shiro.vo.PermissionVO; import com.diboot.shiro.vo.PermissionVO;
@ -49,8 +50,8 @@ public class PermissionController extends BaseCrudRestController {
*/ */
@GetMapping("/list") @GetMapping("/list")
@AuthorizationWrapper(value = @RequiresPermissions("list"), name = "列表") @AuthorizationWrapper(value = @RequiresPermissions("list"), name = "列表")
public JsonResult getVOList(Permission permission, Pagination pagination, HttpServletRequest request) throws Exception{ public JsonResult getVOList(PermissionDto permissionDto, Pagination pagination, HttpServletRequest request) throws Exception{
QueryWrapper<Permission> queryWrapper = super.buildQueryWrapper(permission); QueryWrapper<PermissionDto> queryWrapper = super.buildQueryWrapper(permissionDto);
// 查询当前页的Entity主表数据 // 查询当前页的Entity主表数据
List<Permission> entityList = permissionService.getPermissionList(queryWrapper, pagination); List<Permission> entityList = permissionService.getPermissionList(queryWrapper, pagination);

View File

@ -13,6 +13,7 @@ import com.diboot.core.vo.Status;
import com.diboot.shiro.authz.annotation.AuthorizationCache; import com.diboot.shiro.authz.annotation.AuthorizationCache;
import com.diboot.shiro.authz.annotation.AuthorizationPrefix; import com.diboot.shiro.authz.annotation.AuthorizationPrefix;
import com.diboot.shiro.authz.annotation.AuthorizationWrapper; import com.diboot.shiro.authz.annotation.AuthorizationWrapper;
import com.diboot.shiro.dto.RoleDto;
import com.diboot.shiro.entity.Permission; import com.diboot.shiro.entity.Permission;
import com.diboot.shiro.entity.Role; import com.diboot.shiro.entity.Role;
import com.diboot.shiro.service.RoleService; import com.diboot.shiro.service.RoleService;
@ -50,8 +51,8 @@ public class RoleController extends BaseCrudRestController {
@GetMapping("/list") @GetMapping("/list")
@AuthorizationWrapper(value = @RequiresPermissions("list"), name = "列表") @AuthorizationWrapper(value = @RequiresPermissions("list"), name = "列表")
@AuthorizationCache @AuthorizationCache
public JsonResult getVOList(Role role, Pagination pagination, HttpServletRequest request) throws Exception{ public JsonResult getVOList(RoleDto roleDto, Pagination pagination, HttpServletRequest request) throws Exception{
QueryWrapper<Role> queryWrapper = super.buildQueryWrapper(role); QueryWrapper<RoleDto> queryWrapper = super.buildQueryWrapper(roleDto);
// 获取结果 // 获取结果
List<RoleVO> voList = roleService.getRoleList(queryWrapper, pagination); List<RoleVO> voList = roleService.getRoleList(queryWrapper, pagination);
// 返回结果 // 返回结果

View File

@ -0,0 +1,15 @@
package com.diboot.shiro.dto;
import com.diboot.core.binding.query.BindQuery;
import com.diboot.core.binding.query.Comparison;
import lombok.Data;
@Data
public class PermissionDto {
@BindQuery(comparison = Comparison.LIKE)
private String menuCode;
@BindQuery(comparison = Comparison.LIKE)
private String menuName;
}

View File

@ -0,0 +1,15 @@
package com.diboot.shiro.dto;
import com.diboot.core.binding.query.BindQuery;
import com.diboot.core.binding.query.Comparison;
import lombok.Data;
@Data
public class RoleDto {
@BindQuery(comparison = Comparison.LIKE)
private String code;
@BindQuery(comparison = Comparison.EQ)
private String status;
}

View File

@ -1,8 +1,10 @@
package com.diboot.shiro.service; package com.diboot.shiro.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.service.BaseService; import com.diboot.core.service.BaseService;
import com.diboot.core.vo.Pagination; import com.diboot.core.vo.Pagination;
import com.diboot.shiro.dto.PermissionDto;
import com.diboot.shiro.entity.Permission; import com.diboot.shiro.entity.Permission;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -18,7 +20,7 @@ import java.util.List;
public interface PermissionService extends BaseService<Permission> { public interface PermissionService extends BaseService<Permission> {
//获取权限资源列表 //获取权限资源列表
List<Permission> getPermissionList(QueryWrapper<Permission> queryWrapper, Pagination pagination); List<Permission> getPermissionList(QueryWrapper queryWrapper, Pagination pagination);
/** /**
* 批量创建或更新或删除entityentity.id存在如果deleted = 1表示逻辑删除=0表示更新若entity.id不存在否则新建 * 批量创建或更新或删除entityentity.id存在如果deleted = 1表示逻辑删除=0表示更新若entity.id不存在否则新建

View File

@ -1,5 +1,6 @@
package com.diboot.shiro.service.impl; package com.diboot.shiro.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod; import com.baomidou.mybatisplus.core.enums.SqlMethod;
@ -10,8 +11,10 @@ import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.diboot.core.service.impl.BaseServiceImpl; import com.diboot.core.service.impl.BaseServiceImpl;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.V; import com.diboot.core.util.V;
import com.diboot.core.vo.Pagination; import com.diboot.core.vo.Pagination;
import com.diboot.shiro.dto.PermissionDto;
import com.diboot.shiro.entity.Permission; import com.diboot.shiro.entity.Permission;
import com.diboot.shiro.mapper.PermissionMapper; import com.diboot.shiro.mapper.PermissionMapper;
import com.diboot.shiro.service.PermissionService; import com.diboot.shiro.service.PermissionService;
@ -24,7 +27,9 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors;
/** /**
* 许可授权相关Service * 许可授权相关Service
@ -37,19 +42,40 @@ import java.util.Objects;
public class PermissionServiceImpl extends BaseServiceImpl<PermissionMapper, Permission> implements PermissionService { public class PermissionServiceImpl extends BaseServiceImpl<PermissionMapper, Permission> implements PermissionService {
@Override @Override
public List<Permission> getPermissionList(QueryWrapper<Permission> queryWrapper, Pagination pagination) { public List<Permission> getPermissionList(QueryWrapper queryWrapper, Pagination pagination) {
queryWrapper.lambda().groupBy(Permission::getMenuCode); queryWrapper.groupBy("menu_code");
List<Permission> menuList = super.getEntityList(queryWrapper, pagination); List<Permission> parentPermissionList = super.getEntityList(queryWrapper, pagination);
if(V.notEmpty(menuList)){ if (V.isEmpty(parentPermissionList)){
for(Permission menu : menuList){ return parentPermissionList;
LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper();
wrapper.eq(Permission::getMenuCode, menu.getMenuCode());
List<Permission> permissionList = super.getEntityList(wrapper);
menu.setPermissionList(permissionList);
}
} }
return menuList; // 获取权限编码列表
List<String> permissionMenuCodeList = parentPermissionList.stream()
.map(Permission::getMenuCode)
.distinct()
.collect(Collectors.toList());
if (V.isEmpty(permissionMenuCodeList)){
return parentPermissionList;
}
LambdaQueryWrapper<Permission> allSubListQueryWrapper = new LambdaQueryWrapper<>();
allSubListQueryWrapper.in(Permission::getMenuCode, permissionMenuCodeList);
// 获取所有子级权限列表
List<Permission> allSubPermissionList = super.getEntityList(allSubListQueryWrapper);
// 整理出每一个父级下的所有子级权限列表
Map<String, List<Permission>> subPermissionListMap = BeanUtils.convertToStringKeyObjectListMap(allSubPermissionList,
BeanUtils.convertToFieldName(Permission::getMenuCode));
for (Permission permission : parentPermissionList){
List<Permission> subPermissionList = subPermissionListMap.get(permission.getMenuCode());
if (V.notEmpty(subPermissionList)){
permission.setPermissionList(subPermissionList);
}
}
return parentPermissionList;
} }
/** /**