场景-总体思路
- 微服务跨库,通过接口调用
- 因分表分库无法通过
xml
里的sql进行联表管理查询 - 字段中文模糊查询,而业务表里存的是中文名称在表中对应的id,故需要在另一个数据库进行,通过模块查询出关联表的id,列表
List<String> idList
Mybatis
内置分页拦截去掉,改用java8
内存分页- 查出所有数据,通过
java8
流式过滤出数据
java8内存分页
/**
* @description:java8内存分页
*
* @param records 待分页的数据
* @param pageNum 当前页码
* @param pageSize 每页显示的条数
* @return 分页之后的数据
*/
public static <T> List<T> pagination(List<T> records, int pageNum, int pageSize) {
if (CollectionUtils.isEmpty(records)) {
return Collections.emptyList();
}
int totalCount = records.size();
int remainder = totalCount % pageSize;
int pageCount = (remainder > 0) ? totalCount/pageSize + 1 : totalCount/pageSize;
int zero = 0;
if (remainder == zero) {
return records.stream()
.skip((pageNum - 1) * pageSize)
.limit(pageSize)
.collect(Collectors.toList());
} else {
if (pageNum == pageCount) {
return records.stream()
.skip((pageNum - 1) * pageSize)
.limit(totalCount)
.collect(Collectors.toList());
} else {
return records.stream()
.skip((pageNum - 1) * pageSize)
.limit(pageSize)
.collect(Collectors.toList());
}
}
}
方法skip()
的参数n的四种情况:
- 当n<0时,抛IllegalArgumentException异常;
- 当n=0时,相当没有跳过任何元素,原封不动、完璧归赵
- 当0<n<length时,跳过n个元素后,返回含有剩下的元素的流;
- 当n>=length时,跳过所有元素,返回空流。
方法limit()
的参数n的四种情况: - 当n<0时,抛IllegalArgumentException异常;
- 当n=0时,不取元素,返回空流;
- 当0<n<length时,取前n个元素,返回新的流;
- 当n>=length时,取所有元素。
java8 过滤
private List<User> getUsers(List<UserVo> userVoList, String type, List<User> UserList) {
List<String> userId = userVoList.stream()
.filter(userVo -> StringUtils.isNotBlank(type) && userVo.getUserName().contains(type))
.map(UserVo::getId)
.collect(Collectors.toList());
int size = UserList.size();
List<User> users = null;
if (size > 0 && UserList != null){
users = UserList.stream()
.filter(user -> userId.stream().anyMatch(id -> id.equals(userId.getUserName())))
.collect(Collectors.toList());
}
return users;
}