mybatis与stream使用小记

1-mybatis解决limit语句限制

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery异常解决方法

例子1:mybatis-plus 1层子查询嵌套,mysql不支持LIMIT/IN/ANY/SOME

// 查询最近100条数据
List<SendNsqMessageDO> allfeeYopProtocolDTOs = sendNsqMessageMapper.selectList(
new QueryWrapper<SendNsqMessageDO>()
.inSql("id","select id from send_nsq_message order by created_at desc limit 100")
);

解决办法:2层子查询

// 查询最近100条数据
List<SendNsqMessageDO> allfeeYopProtocolDTOs = sendNsqMessageMapper.selectList(
new QueryWrapper<SendNsqMessageDO>()
.inSql("id","select t.id from (select * from send_nsq_message order by created_at desc limit 100) as t")
);

 

2-stream快速实现求和

目的:结合mybatis查询结果,快速求和

 

 

普通解决办法:求出列的List,然后遍历相加,求和

 

 

简化版:

int oweQuotaSum = alldebtRecords.stream().mapToInt(t->Math.toIntExact(t.getOweQuota())).sum();
int oweQuotaSnapshotSum = alldebtRecords.stream().mapToInt(t->Math.toIntExact(t.getOweQuotaSnapshot())).sum();
int recoveryQuotaSum = alldebtRecords.stream().mapToInt(t->Math.toIntExact(t.getRecoveryQuota())).sum();

 

posted @ 2020-08-26 19:10  爱寂寞撒的谎言  阅读(1628)  评论(0编辑  收藏  举报