业务背景:
原表中的工步和零件字段在数据库中是以序列化后的String字符串存储,当返回前端时需要转换为各自的List对象列表便于前端调用和使用。
数据结构如下:
------>
public TableDataInfo<ExcelBaseImformationVo> selectPageExcelList(PageQuery pageQuery) {
//设置新的voPage,初始化page对象
Page<ExcelBaseImformationVo> voPage = new Page<ExcelBaseImformationVo>().setRecords(new ArrayList<>());
//并未使用selectPage所以,新建的page对象并没有统计总数的功能,这里手动赋值
voPage.setTotal(excelBaseImformationMapper.selectCount(this.buildQueryWrapper()));
//MybatisPlus的流式查询,将查询到的每一条数据进行业务逻辑处理,并且可以附加条件构造器进行过滤
excelBaseImformationMapper.selectList(this.buildQueryWrapper(), resultContext -> {
excelBaseImformationVo = new ExcelBaseImformationVo();
//官方使用方法,getResultObject()获取每一条数据
ExcelBaseImformation record = resultContext.getResultObject();
excelBaseImformationVo.setExcelName(record.getExcelName());
excelBaseImformationVo.setGongxuName(record.getGongxuName());
excelBaseImformationVo.setWenjianBianhao(record.getWenjianBianhao());
//将序列化后的字符串重新转化为子列表对象
excelBaseImformationVo.setGongbu(JSONUtil.toList(JSONUtil.parseArray(record.getGongbu()), GongbuBaseImformation.class));
excelBaseImformationVo.setLingjian(JSONUtil.toList(JSONUtil.parseArray(record.getLingjian()), LingjianBaseImformation.class));
//重新添加进入voPage中
voPage.getRecords().add(excelBaseImformationVo);
});
return TableDataInfo.build(voPage);
}
public QueryWrapper<ExcelBaseImformation> buildQueryWrapper() {
QueryWrapper<ExcelBaseImformation> wrapper = new QueryWrapper<>();
return wrapper;
}
其中,
excelBaseImformationVo.setGongbu(JSONUtil.toList(JSONUtil.parseArray(record.getGongbu()), GongbuBaseImformation.class)); excelBaseImformationVo.setLingjian(JSONUtil.toList(JSONUtil.parseArray(record.getLingjian()), LingjianBaseImformation.class));
基类中的已被序列化成字符串的gongbu和lingjian字段中各自包含了多条数据,使用JSONUtil.parseArray方法,将其中字符串逆序列化转换回对象,最后将转换后的各个对象使用JSONUtil.toList重新组合成list列表赋值给Vo
经测试,比使用for循环性能消耗最多减少50%