有时候一个页面可能要查询十条二十条甚至更多的信息,就需要写很多mapper,这时候如果像之前一样写mapper的话,效率会很低。有下面几条优化方案
1.通过数据库的存储函数,存储过程和视图提升性能
2.优化sql语句。将多个sql语句合成一个;减少使用IN,左连接,右连接,子查询
3.多线程,线程池。使用CompletableFuture
我使用了第三种方法进行优化
CompletableFuture<Integer> nums1 = CompletableFuture.supplyAsync(()->{
// 2.2 开始查询第一个属性 数量
return repMapper.getNum1(beginCreateTime, endCreateTime, username);
});
CompletableFuture<Integer> nums2 = CompletableFuture.supplyAsync(()->{
// 2.3 开始查询第二个属性 数量
return repMapper.getNum2(beginCreateTime, endCreateTime, username);
});
CompletableFuture通过supplyAsync()方法调用异步线程,将每一个查询结果都进行返回。
// join等待所有线程全部执行完成
CompletableFuture
.allOf(nums1,
nums2)
.join();
allOf里存放所有线程,join等待所有线程全部执行完成后再进行下面操作
如有错误,请多包涵