使用MybatisPlus流式查询进行逆序列化赋值,大大减少性能消耗

业务背景:

原表中的工步和零件字段在数据库中是以序列化后的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%

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值