有时候会进行大数据量查询后的建树操作,如果直接使用sql语句会大大延缓业务效率,因此需要优化,本文章介绍了使用java8流操作以及hutool树工具栏的建树操作。
流是支持各种方法的对象序列(一系列对象,列入:LIst对象)
代码如下(示例):
(操作对象).distinct().collect(Collectors.toList());
代码如下(示例):
//一批500条
int batchSize = 500;
//赋值
List<String> finalSysDistrictParentCodeStringList = sysDistrictParentCodeStringList;
//对finalSysDistrictParentCodeStringList进行分批处理
List<List<String>> batches = IntStream.range(0, (finalSysDistrictParentCodeStringList.size() + batchSize - 1) / batchSize)
.mapToObj(i -> finalSysDistrictParentCodeStringList.subList(i * batchSize, Math.min(finalSysDistrictParentCodeStringList.size(), (i + 1) * batchSize)))
.collect(Collectors.toList());
代码如下(示例):
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
//设置id字段
treeNodeConfig.setIdKey("code");
//设置父级id字段
treeNodeConfig.setParentIdKey("parentCode");
List<Tree<String>> trees = TreeUtil.build(result, "0", treeNodeConfig, ((object, treeNode) -> {
//对树节点进行id赋值
treeNode.setId(object.getCode());//id
//对树节点进行父级id赋值
treeNode.setParentId(object.getParentCode());//父id
//对树节点进行名称赋值
treeNode.putExtra("name", object.getName());
//对树节点进行层级赋值
treeNode.putExtra("level", object.getLevels());
}));
log.info("建树后:{}",trees.size());
代码如下(示例):
//实体类
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import java.util.Map;
import java.util.Set;
/**
* 全国区域Demo
*/
@Data
@ApiModel(value = "全国区域")
public class SysDistrictDemo extends BasePageQuery {
/**
* 编码
*/
private String code;
/**
* 名称
*/
private String name;
/**
* 父级编号
*/
private String parentCode;
/**
* 层级
*/
private Integer levels;
}
//整体函数
public R getTreeByLikeInfoDemo(SysDistrictDemo sysDistrictDemo) {
QueryWrapper<SysDistrict> wrapper = new QueryWrapper<>();
wrapper.like("name",sysDistrictDemo.getName());
List<String> sysDistrictParentCodeStringList = "根据条件模糊查询";
log.info("去重前:{}",sysDistrictParentCodeStringList.size());
//至此 取得所有数据 去重
List<SysDistrictTree> result = new ArrayList<>();
sysDistrictParentCodeStringList = sysDistrictParentCodeStringList.stream().distinct().collect(Collectors.toList());
log.info("去重后:{}",sysDistrictParentCodeStringList.size());
int batchSize = 500;
List<String> finalSysDistrictParentCodeStringList = sysDistrictParentCodeStringList;
List<List<String>> batches = IntStream.range(0, (finalSysDistrictParentCodeStringList.size() + batchSize - 1) / batchSize)
.mapToObj(i -> finalSysDistrictParentCodeStringList.subList(i * batchSize, Math.min(finalSysDistrictParentCodeStringList.size(), (i + 1) * batchSize)))
.collect(Collectors.toList());
batches.forEach(codes->{
result = "使用sql中in的查询操作,例如:select a.id from a in #{codes}"
});
log.info("取值后:{}",result.size());
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
//设置id字段
treeNodeConfig.setIdKey("code");
//设置父级id字段
treeNodeConfig.setParentIdKey("parentCode");
List<Tree<String>> trees = TreeUtil.build(result, "0", treeNodeConfig, ((object, treeNode) -> {
//对树节点进行id赋值
treeNode.setId(object.getCode());//id
//对树节点进行父级id赋值
treeNode.setParentId(object.getParentCode());//父id
//对树节点进行名称赋值
treeNode.putExtra("name", object.getName());
//对树节点进行层级赋值
treeNode.putExtra("level", object.getLevels());
}));
log.info("建树后:{}",trees.size());
return R.ok(trees);
}
自此完成了优化,本人是50s优化至7s,欢迎讨论,勿喷,怕怕