获取登录用户

  • 通过 LoginUserBO.getLoginUser(); 方法即可获取当前登录用户

分页和排序

  • 框架分页使用PageHelper 具体用法可以参考官方文档: PageHelper官方文档
  • 分页加排序实现方式: 在要分页的sql、前面加
  • pageNum 当前页码
  • pageSize 每页显示多少条
  • orderBy 排序规则 固定写法postListParamPO.getOrderBy(PostPO.class)
    public List<PostPO> queryPageList(PostListParamPO postListParamPO) {
        LoginUserBO loginUserBO = LoginUserBO.getLoginUser()
        //搜索条件
        LambdaQueryWrapper<PostPO> queryWrapper = Wrappers.lambdaQuery(PostPO.class)
                .like(KPStringUtil.isNotEmpty(postListParamPO.getPostCode()), PostPO::getPostCode, postListParamPO.getPostCode())
                .like(KPStringUtil.isNotEmpty(postListParamPO.getPostName()), PostPO::getPostName, postListParamPO.getPostName())
                .eq(KPStringUtil.isNotEmpty(postListParamPO.getStatus()), PostPO::getStatus, postListParamPO.getStatus());

        //分页和排序
        PageHelper.startPage(postListParamPO.getPageNum(), postListParamPO.getPageSize(), postListParamPO.getOrderBy(PostPO.class));
        return this.baseMapper.selectList(queryWrapper);
    }

分页

  • 如果不要分页 只要排序 则
  • new PageBO().getOrderBy 固定写法
    public List<DeptCustomerPO> queryList(DeptListParamPO deptListParamPO) {
        PageHelper.orderBy(new PageBO().getOrderBy(deptListParamPO.getOrderBy(), DeptPO.class));
        List<DeptCustomerPO> list = KPJsonUtil.toJavaObjectList(this.baseMapper.selectList(null), DeptCustomerPO.class);
    }

PageHelper.orderBy(new PageBO().getOrderBy(deptListParamPO.getOrderBy(), DeptPO.class));

异常处理

  • 框架提供了一些经常使用的异常类供用户使用
    异常类说明
    KPServiceException业务异常
    KPUtilException工具类异常
    KPListenerDistributeExceptionlinener分发异常
    KPHttpDistributeExceptionHTTP请求异常

线程池使用

  • 框架默认提供线程池给用户使用, 从而避免用户自行维护线程池 造成资源浪费和内存溢出的风险
  • 使用方式 @Resource(name = "kpExecutorService") private ExecutorService kpExecutorService;
  • 示例代码
  @XxlJob("httpLogArchive")
  public void handle() {
      ... 其他代码
      threadData.forEach(list -> {
          // 可以无限项线程池添加任务 但具体多少和线程同时执行 看配置 如果不配置 默认3个线程 300个队列 最大8个线程 
          kpExecutorService.submit(new Runnable() {
              @Override
              public void run() {
                  //线程池处理逻辑
              }
          });
      });
  }
完整代码示例
@Slf4j
@Component
public class HttpHistoryHandle {


  @Resource
  private HttpLogHistoryMapper httpLogHistoryMapper;

  @Resource
  private HttpLogMapper httpLogMapper;

  @Resource(name = "kpExecutorService")
  private ExecutorService kpExecutorService;

  @Autowired
  private MqProperties mqProperties;

  @XxlJob("httpLogArchive")
  public void handle() {
    LambdaQueryWrapper<HttpLogPO> wrapper = Wrappers.lambdaQuery(HttpLogPO.class)
            .lt(HttpLogPO::getCallTime, KPLocalDateTimeUtil.addDays(LocalDateTime.now(), -mqProperties.getHttpArchiveDay()))
            .orderByAsc(HttpLogPO::getCallTime);

    // 把需要归档的数据先加载到内存中
    int pageNum = 1, pageSize = 5000, pageTotal = 0;
    List<HttpLogPO> body = new ArrayList<>();
    do {
      PageHelper.startPage(pageNum++, pageSize);
      List<HttpLogPO> httpLogPOList = httpLogMapper.selectList(wrapper);
      pageTotal = httpLogPOList.size();
      if (pageTotal == 0) break;
      body.addAll(httpLogPOList);
    } while (pageTotal == pageSize);


    //进多线程开始处理归档 1000 一个子线程
    List<List<HttpLogPO>> threadData = Lists.partition(body, 1000);
    threadData.forEach(list -> {
      kpExecutorService.submit(new Runnable() {
        @Override
        public void run() {
          // 打印当前执行线程信息(验证线程名前缀)
          log.info("【http归档】线程[{}]开始处理归档任务,数据量:{}", Thread.currentThread().getName(), list.size());

          //处理历史数据
          KPCollectionUtil.insertBatch(httpLogHistoryMapper, list, HttpLogHistoryPO.class, 100);
          //删除主表
          httpLogMapper.deleteAllByIds(list.stream().map(HttpLogPO::getUuid).collect(Collectors.toList()));
          log.info("【http归档】线程[{}]:当前页数据归档完成,总处理量:{}", Thread.currentThread().getName(), list.size());
        }
      });
    });

    log.info("【http归档】日志归档任务:所有待处理数据已提交至线程池,主线程结束(异步线程将继续执行)");
  }

}