MybatisPlus QueryWrapper对象封装操作类AbstractWrapper查询条件封装

﹥>﹥吖頭↗ 提交于 2020-12-12 12:46:38

MybatisPlus QueryWrapper对象封装操作类

先把代码拿出来遛一遛

public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>> {
   
   

    /**
     * 查询字段
     */
    private String sqlSelect;

    public QueryWrapper() {
   
   
        this(null);
    }

    public QueryWrapper(T entity) {
   
   
        this.entity = entity;
        this.initEntityClass();
        this.initNeed();
    }

    public QueryWrapper(T entity, String... columns) {
   
   
        this.entity = entity;
        this.select(columns);
        this.initEntityClass();
        this.initNeed();
    }

    /**
     * 非对外公开的构造方法,只用于生产嵌套 sql
     *
     * @param entityClass 本不应该需要的
     */
    private QueryWrapper(T entity, Class<T> entityClass, AtomicInteger paramNameSeq,
                         Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
   
   
        this.entity = entity;
        this.entityClass = entityClass;
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
    }

    public QueryWrapper<T> select(String... columns) {
   
   
        if (ArrayUtils.isNotEmpty(columns)) {
   
   
            this.sqlSelect = String.join(StringPool.COMMA, columns);
        }
        return typedThis;
    }

    public QueryWrapper<T> select(Predicate<TableFieldInfo> predicate) {
   
   
        return select(entityClass, predicate);
    }

    /**
     * <p>
     * 过滤查询的字段信息(主键除外!)
     * </p>
     * <p>
     * 例1: 只要 java 字段名以 "test" 开头的              -> select(i -> i.getProperty().startsWith("test"))
     * 例2: 只要 java 字段属性是 CharSequence 类型的       -> select(TableFieldInfo::isCharSequence)
     * 例3: 只要 java 字段没有填充策略的                   -> select(i -> i.getFieldFill == FieldFill.DEFAULT)
     * 例4: 要全部字段                                   -> select(i -> true)
     * 例5: 只要主键字段                                 -> select(i -> false)
     * </p>
     *
     * @param predicate 过滤方式
     * @return this
     */
    public QueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
   
   
        this.entityClass = entityClass;
        this.sqlSelect = TableInfoHelper.getTableInfo(getCheckEntityClass()).chooseSelect(predicate);
        return typedThis;
    }

    /**
     * <p>
     * 返回一个支持 lambda 函数写法的 wrapper
     * </p>
     */
    public LambdaQueryWrapper<T> lambda() {
   
   
        return new LambdaQueryWrapper<>(entity, entityClass, sqlSelect, paramNameSeq, paramNameValuePairs, expression);
    }

    @Override
    public String getSqlSelect() {
   
   
        return sqlSelect;
    }

    @Override
    protected String columnToString(String column) {
   
   
        return column;
    }

    /**
     * <p>
     * 用于生成嵌套 sql
     * 故 sqlSelect 不向下传递
     * </p>
     */
    @Override
    protected QueryWrapper<T> instance(AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
   
   
        return new QueryWrapper<>(entity, entityClass, paramNameSeq, paramNameValuePairs, new MergeSegments());
    }
}

public abstract class AbstractWrapper封装了很多类似下面的条件方法

   @Override
    public <V> This allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) {
   
   
        if (condition && CollectionUtils.isNotEmpty(params)) {
   
   
            params.forEach((k, v) -> {
   
   
                if (filter.test(k, v)) {
   
   
                    if (StringUtils.checkValNotNull(v)) {
   
   
                        eq(k, v);
                    } else {
   
   
                        if (null2IsNull) {
   
   
                            isNull(k);
                        }
                    }
                }
            });
        }
        return typedThis;
    }

    @Override
    public This eq(boolean condition, R column, Object val) {
   
   
        return addCondition(condition, column, EQ, val);
    }

    @Override
    public This ne(boolean condition, R column, Object val) {
   
   
        return addCondition(condition, column, NE, val);
    }

    @Override
    public This gt(boolean condition, R column, Object val) {
   
   
        return addCondition(condition, column, GT, val);
    }

    @Override
    public This ge(boolean condition, R column, Object val) {
   
   
        return addCondition(condition, column, GE, val);
    }

    @Override
    public This lt(boolean condition, R column, Object val) {
   
   
        return addCondition(condition, column, LT, val);
    }

    @Override
    public This le(boolean condition, R column, Object val) {
   
   
        return addCondition(condition, column, LE, val);
    }

    @Override
    public This like(boolean condition, R column, Object val) {
   
   
        return doIt(condition, () -> columnToString(column), LIKE, () -> formatSql("{0}", StringPool.PERCENT + val + StringPool.PERCENT));
    }

    @Override
    public This notLike(boolean condition, R column, Object val) {
   
   
        return not(condition).like(condition, column, val);
    }

    @Override
    public This likeLeft(boolean condition, R column, Object val) {
   
   
        return doIt(condition, () -> columnToString(column), LIKE, () -> formatSql("{0}", StringPool.PERCENT + val));
    }

    @Override
    public This likeRight(boolean condition, R column, Object val) {
   
   
        return doIt(condition, () -> columnToString(column), LIKE, () -> formatSql("{0}", val + StringPool.PERCENT));
    }

    @Override
    public This between(boolean condition, R column, Object val1, Object val2) {
   
   
        return doIt(condition, () -> columnToString(column), BETWEEN, () -> formatSql("{0}", val1), AND,
            () -> formatSql("{0}", val2));
    }

    @Override
    public This notBetween(boolean condition, R column, Object val1, Object val2) {
   
   
        return not(condition).between(condition, column, val1, val2);
    }

    @Override
    public This and(boolean condition, Function<This, This> func) {
   
   
        return and(condition).addNestedCondition(condition, func);
    }

    @Override
    public This or(boolean condition, Function<This, This> func) {
   
   
        return or(condition).addNestedCondition(condition, func);
    }

    @Override
    public This nested(boolean condition, Function<This, This> func) {
   
   
        return addNestedCondition(condition, func);
    }

    @Override
    public This or(boolean condition) {
   
   
        return doIt(condition, OR);
    }

    @Override
    public This apply(boolean condition, String applySql, Object... value) {
   
   
        return doIt(condition, APPLY, () -> formatSql(applySql, value));
    }

    @Override
    public This last(boolean condition, String lastSql) {
   
   
        if (condition) {
   
   
            this.lastSql = StringPool.SPACE + lastSql;
        }
        return typedThis;
    }

    @Override
    public This exists(boolean condition, String existsSql) {
   
   
        return doIt(condition, EXISTS, () -> String.format("(%s)", existsSql));
    }

    @Override
    public This notExists(boolean condition, String notExistsSql) {
   
   
        return not(condition).exists(condition, notExistsSql);
    }

    @Override
    public This isNull(boolean condition, R column) {
   
   
        return doIt(condition, () -> columnToString(column), IS_NULL);
    }

    @Override
    public This isNotNull(boolean condition, R column) {
   
   
        return doIt(condition, () -> columnToString(column), IS_NOT_NULL);
    }

    @Override
    public This in(boolean condition, R column, Collection<?> value) {
   
   
        if (CollectionUtils.isEmpty(value)) {
   
   
            return typedThis;
        }
        return doIt(condition, () -> columnToString(column), IN, inExpression(value));
    }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!