MyBatis-Plus多个不同实体类对应的分页IPage重构抽取成特定统一方法

利用泛型方法的方式,泛型符号T表示传入方法的参数类型

public Msg getPage(Integer currentPage, Integer everyPageSize, String type) {
    
    
    Msg msg = new Msg();
    if (type == null || type.equals("")) {
    
    
        msg.setCode(2000);
        msg.setMsg("类型type不存在");
        return msg;
    }
    try {
    
    
        switch (type) {
    
    
            case "01":
                return getPageData(new Page<>(currentPage, everyPageSize), new QueryWrapper<ReachGoal01>().orderByDesc("id"), ReachGoal01.class, reachGoal01Mapper, msg);
            case "02":
                return getPageData(new Page<>(currentPage, everyPageSize), new QueryWrapper<ReachGoal02>().orderByDesc("id"), ReachGoal02.class, reachGoal02Mapper, msg);
            case "03":
                return getPageData(new Page<>(currentPage, everyPageSize), new QueryWrapper<ReachGoal03>().orderByDesc("id"), ReachGoal03.class, reachGoal03Mapper, msg);
            default:
                msg.setCode(2000);
                msg.setMsg("类型type不存在");
                return msg;
        }
    } catch (Exception e) {
    
    
        msg.setCode(2000);
        msg.setMsg(e.getMessage());
        return msg;
    }
}
//Class<T> clazz没有用到,可以删除掉
private <T> Msg getPageData(IPage<T> iPage, QueryWrapper<T> qw, Class<T> clazz, BaseMapper<T> mapper, Msg msg) {
    
    
    List<T> records = mapper.selectPage(iPage, qw).getRecords();
    int total = mapper.selectCount(qw);
    msg.add("count", records.size());
    msg.add("list", records);
    msg.add("total", total);
    msg.setCode(1000);
    msg.setMsg("获取分页成功");
    return msg;
}

猜你喜欢

转载自blog.csdn.net/Xidian2850/article/details/130571585
今日推荐