Mybatis plus 封装前后端查询的基础条件

levis
2024-06-24 / 0 评论 / 5 阅读 / 正在检测是否收录...
@Data
@ToString
@Builder
public class MallQueryDTO implements Serializable {
    public static final long serialVersionUID = 5510022355351221662L;

    public Integer pageNo;
    public Integer pageSize;
    public List<PairOfQueryDTO> filter;
    public List<PairOfSortDTO> sort;

}
@Data
@Builder
public class PairOfQueryDTO implements Serializable {

    private static final long serialVersionUID = -8952875265623011362L;
    private String key;
    private String value;
    // 查询方式 eq/gt/ge/lt/le/in/like/isNotNUll
    private String by;
}
@Data
@Builder
public class PairOfSortDTO implements Serializable {

    private static final long serialVersionUID = -3413644584659764949L;
    private String key;
    // 排序方式 "DESC/ASC"
    private String order;
}
@Slf4j
public class MallQueryWrapper<T> {

    private T entity;
    private MallQueryDTO mallQueryDTO;
    /***
     * 查询条件
     * eg   =
     * ge   >=
     * gt   >
     * le   <=
     * lt   <
     * in   in
     * like like
     * ne     !=
     */
    private static final String FILTER_EQ = "eq";
    private static final String FILTER_GE = "ge";
    private static final String FILTER_GT = "gt";
    private static final String FILTER_LE = "le";
    private static final String FILTER_LT = "lt";
    private static final String FILTER_IN = "in";
    private static final String FILTER_LIKE = "like";
    private static final String FILTER_NE = "ne";
    private static final String FILTER_ISNOTNULL = "isNotNull";


    private static final String SORT_ASC = "ASC";
    private static final String SORT_DESC = "DESC";


    private static final int DEFAULT_PAGE_SIZE = 10;
    private static final int DEFAULT_PAGE_NO = 1;

    /***
     * @param entity    数据库实体, 设置对应参数值 等同于 name = value , 对象为null时,不生效
     * @param mallQueryDTO  通用查询参数
     */
    public MallQueryWrapper(T entity, MallQueryDTO mallQueryDTO) {
        this.entity = entity;
        this.mallQueryDTO = mallQueryDTO;
    }


    public Page<T> getPageInstance() {
        if (null == mallQueryDTO) {
            mallQueryDTO = MallQueryDTO.builder().build();
        }
        log.info("QueryWrapper queryDTO:{}, entity:{}", mallQueryDTO, entity);
        // current 从0开始的
        int current = mallQueryDTO.getPageNo() == null || mallQueryDTO.getPageNo() < 1 ? DEFAULT_PAGE_NO : mallQueryDTO.getPageNo();
        int size = mallQueryDTO.getPageSize() == null || mallQueryDTO.getPageNo() < 1 ? DEFAULT_PAGE_SIZE : mallQueryDTO.getPageSize();
        return new Page<>(current, size);
    }

    public QueryWrapper<T> getWrapperInstance() {
        QueryWrapper<T> entityWrapper = new QueryWrapper<T>();
        if (Objects.nonNull(entity)) {
            entityWrapper.setEntity(entity);
        }
        // 转换查询条件
        if (!CollectionUtils.isEmpty(mallQueryDTO.getFilter())) {
            mallQueryDTO.getFilter().forEach(filter -> {
                if (FILTER_EQ.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.eq(filter.getKey(), filter.getValue());

                } else if (FILTER_GE.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.ge(filter.getKey(), filter.getValue());

                } else if (FILTER_GT.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.gt(filter.getKey(), filter.getValue());

                } else if (FILTER_LE.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.le(filter.getKey(), filter.getValue());

                } else if (FILTER_LT.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.lt(filter.getKey(), filter.getValue());

                } else if (FILTER_IN.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.in(filter.getKey(), filter.getValue());

                } else if (FILTER_LIKE.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.like(filter.getKey(), filter.getValue());

                } else if (FILTER_NE.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.ne(filter.getKey(), filter.getValue());

                } else if (FILTER_ISNOTNULL.equalsIgnoreCase(filter.getBy())) {
                    entityWrapper.isNotNull(filter.getKey());
                }
            });
        }
        // 转换排序字段
        if (!CollectionUtils.isEmpty(mallQueryDTO.getSort())) {
            mallQueryDTO.getSort().forEach(sort -> {
                if (SORT_ASC.equalsIgnoreCase(sort.getOrder())) {
                    entityWrapper.orderByAsc(sort.getKey());
                }
                if (SORT_DESC.equalsIgnoreCase(sort.getOrder())) {
                    entityWrapper.orderByDesc(sort.getKey());
                }
            });
        }
        return entityWrapper;
    }

}
// 示例一:分页查询
@Override
public PageDTO<PaidRedpackVO> list(QueryDTO queryDTO) {
    PaidRedpack pr = PaidRedpack.builder().build();
    QueryWrapper<PaidRedpack> queryWrapper = new QueryWrapper<>(pr, queryDTO);
    Page<PaidRedpack> page = paidRedpackDao.selectPage(queryWrapper.getPageInstance(), queryWrapper.getWrapperInstance());
}

0

评论 (0)

取消