项目场景:
公司项目需要对在线用户进行管理,在线用户的ID信息存储于缓存redis,列表信息显示的时候,需要通过ID查询本地数据库获取用户信息,分页和关键字模糊查询。
思路:
step1:获取redis中所有在线用户ID数据;
step2:根据ID获取本地数据库的用户信息,得到List<UserDto> list结果集合;
step3:然后通过List流的anyMatch和关键字的String的contains方法达到关键字模糊查询的效果;
step4:手动PageHelper分页显示(因为list流筛选操作了结果集List,所以PageHelper.startPage(pageIndex,pageSize)会失效,所以需要手动分页,具体原因可以看LZ另一篇PageHelper失效的帖子介绍:点击这里)
开始实操:
提示:帖子标题很明确了,这里LZ只写思路step3、step4
业务类实操显示如下:LZ这里模糊查询了三个字段,账户、用户名和角色,模糊查询的字段越多这里代码越臃肿,这里可以优化封装list.stream().anyMatch()和list.stream().filter().collect()
@Override
public ResponseResult getUserList(Integer pageIndex, Integer pageSize, String search) {
//step1:
/**
* 省略
*/
//step2:根据ID获取本地数据库的用户信息
List<UserDto> list = new ArrayList<>();
/**
* 省略获取用户信息
*/
//step3:关键字筛选模糊查询
search = Optional.ofNullable(search).orElse("");//校验非空并转换空字符串
if (list.size() > 0) {
String finalSearch = search;
boolean nameFlag = list.stream().anyMatch(userDto -> userDto.getUsername().contains(finalSearch));
if (!nameFlag) {//模糊匹配字段
boolean serialnoFlag = list.stream().anyMatch(userDto -> userDto.getAccount().contains(finalSearch));
if (!serialnoFlag) {
boolean roleFlag = list.stream().anyMatch(userDto -> userDto.getRole().contains(finalSearch));
if (!roleFlag) {
list.clear();
} else {
list = list.stream()
.filter(userDto -> userDto.getRole().contains(finalSearch))
.collect(Collectors.toList());//过滤筛选信息
}
} else {
list = list.stream()
.filter(userDto -> userDto.getAccount().contains(finalSearch))
.collect(Collectors.toList());
}
} else {
list = list.stream()
.filter(userDto -> userDto.getUsername().contains(finalSearch))
.collect(Collectors.toList());
}
//step4:手动分页
int total = list.size(); //计算总记录数
List<UserDto> collect = list.stream()
.skip((pageIndex - 1) * pageSize)
.limit(pageSize)
.collect(Collectors.toList());//pageHelper无效,流操作:skip跳记录和limit限制显示记录数
int pageSum = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;//计算总页数
PageHelper.startPage(pageIndex, pageSize);
PageInfo<UserDto> scorePageInfo = new PageInfo<>(collect);
scorePageInfo.setTotal(total);//总记录数
scorePageInfo.setPages(pageSum);//总页数
return ResponseResult.success(scorePageInfo);
}else {
return ResponseResult.success(new PageInfo<>(list));
}
}
优化代码如下:代码层次分明,业务思路清晰(有兴趣的话可以看LZ对七大设计原则的Java实战理解和图解)
@Override
public ResponseResult getUserList(Integer pageIndex, Integer pageSize, String search) {
//step1:
/**
* 省略
*/
//step2:根据ID获取本地数据库的用户信息
List<UserDto> list = new ArrayList<>();
/**
* 省略获取用户信息
*/
//step3:关键字筛选模糊查询
search = Optional.ofNullable(search).orElse("");//校验非空并转换空字符串
if (list.size() > 0) {
String finalSearch = search;
if (!anyMatchUser(list,finalSearch,3)) {//模糊匹配字段
if (!anyMatchUser(list,finalSearch,2)) {
if (!anyMatchUser(list,finalSearch,1)) {
list.clear();
} else {
list = filterUser(list,finalSearch,1);//过滤筛选信息
}
} else {
list = filterUser(list,finalSearch,2);
}
} else {
list = filterUser(list,finalSearch,3);
}
//step4:手动分页
int total = list.size(); //计算总记录数
List<UserDto> collect = list.stream()
.skip((pageIndex - 1) * pageSize)
.limit(pageSize)
.collect(Collectors.toList());//pageHelper无效,流操作:skip跳记录和limit限制显示记录数
int pageSum = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;//计算总页数
PageHelper.startPage(pageIndex, pageSize);
PageInfo<UserDto> scorePageInfo = new PageInfo<>(collect);
scorePageInfo.setTotal(total);//总记录数
scorePageInfo.setPages(pageSum);//总页数
return ResponseResult.success(scorePageInfo);
}else {
return ResponseResult.success(new PageInfo<>(list));
}
}
/**
* list过滤用户
* @param list
* @param finalSearch
* @return
*/
private List<UserDto> filterUser(List<UserDto> list,String finalSearch,int filterType){
if(filterType==1){
list = list.stream()
.filter(userDto -> userDto.getRole().contains(finalSearch))
.collect(Collectors.toList());
}else if(filterType==2){
list = list.stream()
.filter(userDto -> userDto.getAccount().contains(finalSearch))
.collect(Collectors.toList());
}else{
list = list.stream()
.filter(userDto -> userDto.getUsername().contains(finalSearch))
.collect(Collectors.toList());
}
return list;
}
/**
* list过滤匹配用户字段
* @param list
* @param finalSearch
* @return
*/
private boolean anyMatchUser(List<UserDto> list,String finalSearch,int filterType){
boolean flag = false;
if(filterType==1){
flag = list.stream().anyMatch(userDto -> userDto.getRole().contains(finalSearch));
}else if(filterType==2){
flag = list.stream().anyMatch(userDto -> userDto.getAccount().contains(finalSearch));
}else{
flag = list.stream().anyMatch(userDto -> userDto.getUsername().contains(finalSearch));
}
return flag;
}
结束语:挣钱是游戏,活着才是胜利,快乐才是真谛,健康才是目的。