Mybatis Plus条件构造器condition动态判断优化

 

ConditionQueryWrapper
package com.common.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author exmyth
 * @version 1.0.0
 * @date 2021-01-19 17:06
 */
public class ConditionQueryWrapper<T> extends QueryWrapper<T> {

    @Override
    public LambdaConditionQueryWrapper<T> lambda() {
        return new LambdaConditionQueryWrapper(super.lambda());
    }

    @Override
    public ConditionQueryWrapper<T> between(String column, Object val1, Object val2) {
        super.between(ObjectUtils.isNotEmpty(val1)&&ObjectUtils.isNotEmpty(val2), column, val1, val2);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> eq(String column, Object val) {
        super.eq(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> le(boolean condition, String column, Object val) {
        super.le(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> lt(String column, Object val) {
        super.lt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> ge(boolean condition, String column, Object val) {
        super.ge(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> gt(String column, Object val) {
        super.gt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> like(String column, Object val) {
        super.like(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> likeLeft(boolean condition, String column, Object val) {
        super.likeLeft(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> likeRight(boolean condition, String column, Object val) {
        super.likeRight(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> ne(boolean condition, String column, Object val) {
        super.ne(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> notBetween(boolean condition, String column, Object val1, Object val2) {
        super.notBetween(ObjectUtils.isNotEmpty(val1) && ObjectUtils.isNotEmpty(val2), column, val1, val2);
        return this;
    }

    @Override
    public ConditionQueryWrapper<T> notLike(boolean condition, String column, Object val) {
        super.notLike(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }
}

 

 

LambdaConditionQueryWrapper
package com.common.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author exmyth
 * @version 1.0.0
 * @date 2021-01-19 17:11
 */
public class LambdaConditionQueryWrapper<T> extends LambdaQueryWrapper<T>{

    private final LambdaQueryWrapper<T> wrapper;

    public LambdaConditionQueryWrapper(LambdaQueryWrapper<T> wrapper) {
        this.wrapper = wrapper;
    }

    public LambdaQueryWrapper<T> wrapper(){
        return this.wrapper;
    }

    @Override
    public LambdaConditionQueryWrapper<T> between(boolean condition, SFunction<T, ?> column, Object val1, Object val2) {
        wrapper.between(ObjectUtils.isNotEmpty(val1)&&ObjectUtils.isNotEmpty(val2), column, val1, val2);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> eq(SFunction<T, ?> column, Object val) {
        wrapper.eq(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> le(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.le(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> lt(SFunction<T, ?> column, Object val) {
        wrapper.lt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> ge(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.ge(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> gt(SFunction<T, ?> column, Object val) {
        wrapper.gt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> like(SFunction<T, ?> column, Object val) {
        wrapper.like(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> likeLeft(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.likeLeft(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> likeRight(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.likeRight(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> ne(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.ne(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> notBetween(boolean condition, SFunction<T, ?> column, Object val1, Object val2) {
        wrapper.notBetween(ObjectUtils.isNotEmpty(val1) && ObjectUtils.isNotEmpty(val2), column, val1, val2);
        return this;
    }

    @Override
    public LambdaConditionQueryWrapper<T> notLike(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.notLike(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }
}

 

 

 

ConditionUpdateWrapper


package com.common.util;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author exmyth
 * @version 1.0.0
 * @date 2021-01-19 19:22
 */
public class ConditionUpdateWrapper<T> extends UpdateWrapper<T> {
    @Override
    public LambdaConditionUpdateWrapper<T> lambda() {
        return new LambdaConditionUpdateWrapper<T>(super.lambda());
    }

    @Override
    public ConditionUpdateWrapper<T> between(String column, Object val1, Object val2) {
        super.between(ObjectUtils.isNotEmpty(val1)&&ObjectUtils.isNotEmpty(val2), column, val1, val2);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> eq(String column, Object val) {
        super.eq(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> le(boolean condition, String column, Object val) {
        super.le(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> lt(String column, Object val) {
        super.lt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> ge(boolean condition, String column, Object val) {
        super.ge(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> gt(String column, Object val) {
        super.gt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> like(String column, Object val) {
        super.like(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> likeLeft(boolean condition, String column, Object val) {
        super.likeLeft(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> likeRight(boolean condition, String column, Object val) {
        super.likeRight(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> ne(boolean condition, String column, Object val) {
        super.ne(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> notBetween(boolean condition, String column, Object val1, Object val2) {
        super.notBetween(ObjectUtils.isNotEmpty(val1) && ObjectUtils.isNotEmpty(val2), column, val1, val2);
        return this;
    }

    @Override
    public ConditionUpdateWrapper<T> notLike(boolean condition, String column, Object val) {
        super.notLike(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }
}

 




LambdaConditionUpdateWrapper
package com.common.util;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author exmyth
 * @version 1.0.0
 * @date 2021-01-19 19:23
 */
public class LambdaConditionUpdateWrapper<T> extends LambdaUpdateWrapper<T> {
    private final LambdaUpdateWrapper<T> wrapper;

    public LambdaConditionUpdateWrapper(LambdaUpdateWrapper<T> wrapper) {
        this.wrapper = wrapper;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> between(boolean condition, SFunction<T, ?> column, Object val1, Object val2) {
        wrapper.between(ObjectUtils.isNotEmpty(val1)&&ObjectUtils.isNotEmpty(val2), column, val1, val2);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> eq(SFunction<T, ?> column, Object val) {
        wrapper.eq(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> le(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.le(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> lt(SFunction<T, ?> column, Object val) {
        wrapper.lt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> ge(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.ge(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> gt(SFunction<T, ?> column, Object val) {
        wrapper.gt(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> like(SFunction<T, ?> column, Object val) {
        wrapper.like(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> likeLeft(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.likeLeft(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> likeRight(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.likeRight(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> ne(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.ne(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> notBetween(boolean condition, SFunction<T, ?> column, Object val1, Object val2) {
        wrapper.notBetween(ObjectUtils.isNotEmpty(val1) && ObjectUtils.isNotEmpty(val2), column, val1, val2);
        return this;
    }

    @Override
    public LambdaConditionUpdateWrapper<T> notLike(boolean condition, SFunction<T, ?> column, Object val) {
        wrapper.notLike(ObjectUtils.isNotEmpty(val), column, val);
        return this;
    }
}

 

posted @ 2021-01-19 19:37  牧之丨  阅读(1115)  评论(0编辑  收藏  举报