package com.iailab.framework.mybatis.core.query; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import com.iailab.framework.common.util.collection.ArrayUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.MPJWrappers; import com.github.yulichang.wrapper.MPJLambdaWrapper; import org.springframework.util.StringUtils; import java.util.Collection; import java.util.function.Consumer; /** * 拓展 MyBatis Plus Join QueryWrapper 类,主要增加如下功能: *

* 1. 拼接条件的方法,增加 xxxIfPresent 方法,用于判断值不存在的时候,不要拼接到条件中。 * * @param 数据类型 */ public class MPJLambdaWrapperX extends MPJLambdaWrapper { public MPJLambdaWrapperX likeIfPresent(SFunction column, String val) { MPJWrappers.lambdaJoin().like(column, val); if (StringUtils.hasText(val)) { return (MPJLambdaWrapperX) super.like(column, val); } return this; } public MPJLambdaWrapperX inIfPresent(SFunction column, Collection values) { if (ObjectUtil.isAllNotEmpty(values) && !ArrayUtil.isEmpty(values)) { return (MPJLambdaWrapperX) super.in(column, values); } return this; } public MPJLambdaWrapperX inIfPresent(SFunction column, Object... values) { if (ObjectUtil.isAllNotEmpty(values) && !ArrayUtil.isEmpty(values)) { return (MPJLambdaWrapperX) super.in(column, values); } return this; } public MPJLambdaWrapperX eqIfPresent(SFunction column, Object val) { if (ObjectUtil.isNotEmpty(val)) { return (MPJLambdaWrapperX) super.eq(column, val); } return this; } public MPJLambdaWrapperX neIfPresent(SFunction column, Object val) { if (ObjectUtil.isNotEmpty(val)) { return (MPJLambdaWrapperX) super.ne(column, val); } return this; } public MPJLambdaWrapperX gtIfPresent(SFunction column, Object val) { if (val != null) { return (MPJLambdaWrapperX) super.gt(column, val); } return this; } public MPJLambdaWrapperX geIfPresent(SFunction column, Object val) { if (val != null) { return (MPJLambdaWrapperX) super.ge(column, val); } return this; } public MPJLambdaWrapperX ltIfPresent(SFunction column, Object val) { if (val != null) { return (MPJLambdaWrapperX) super.lt(column, val); } return this; } public MPJLambdaWrapperX leIfPresent(SFunction column, Object val) { if (val != null) { return (MPJLambdaWrapperX) super.le(column, val); } return this; } public MPJLambdaWrapperX betweenIfPresent(SFunction column, Object val1, Object val2) { if (val1 != null && val2 != null) { return (MPJLambdaWrapperX) super.between(column, val1, val2); } if (val1 != null) { return (MPJLambdaWrapperX) ge(column, val1); } if (val2 != null) { return (MPJLambdaWrapperX) le(column, val2); } return this; } public MPJLambdaWrapperX betweenIfPresent(SFunction column, Object[] values) { Object val1 = ArrayUtils.get(values, 0); Object val2 = ArrayUtils.get(values, 1); return betweenIfPresent(column, val1, val2); } // ========== 重写父类方法,方便链式调用 ========== @Override public MPJLambdaWrapperX eq(boolean condition, SFunction column, Object val) { super.eq(condition, column, val); return this; } @Override public MPJLambdaWrapperX eq(SFunction column, Object val) { super.eq(column, val); return this; } @Override public MPJLambdaWrapperX orderByDesc(SFunction column) { //noinspection unchecked super.orderByDesc(true, column); return this; } @Override public MPJLambdaWrapperX last(String lastSql) { super.last(lastSql); return this; } @Override public MPJLambdaWrapperX in(SFunction column, Collection coll) { super.in(column, coll); return this; } @Override public MPJLambdaWrapperX selectAll(Class clazz) { super.selectAll(clazz); return this; } @Override public MPJLambdaWrapperX selectAll(Class clazz, String prefix) { super.selectAll(clazz, prefix); return this; } @Override public MPJLambdaWrapperX selectAs(SFunction column, String alias) { super.selectAs(column, alias); return this; } @Override public MPJLambdaWrapperX selectAs(String column, SFunction alias) { super.selectAs(column, alias); return this; } @Override public MPJLambdaWrapperX selectAs(SFunction column, SFunction alias) { super.selectAs(column, alias); return this; } @Override public MPJLambdaWrapperX selectAs(String index, SFunction column, SFunction alias) { super.selectAs(index, column, alias); return this; } @Override public MPJLambdaWrapperX selectAsClass(Class source, Class tag) { super.selectAsClass(source, tag); return this; } @Override public MPJLambdaWrapperX selectSub(Class clazz, Consumer> consumer, SFunction alias) { super.selectSub(clazz, consumer, alias); return this; } @Override public MPJLambdaWrapperX selectSub(Class clazz, String st, Consumer> consumer, SFunction alias) { super.selectSub(clazz, st, consumer, alias); return this; } @Override public MPJLambdaWrapperX selectCount(SFunction column) { super.selectCount(column); return this; } @Override public MPJLambdaWrapperX selectCount(Object column, String alias) { super.selectCount(column, alias); return this; } @Override public MPJLambdaWrapperX selectCount(Object column, SFunction alias) { super.selectCount(column, alias); return this; } @Override public MPJLambdaWrapperX selectCount(SFunction column, String alias) { super.selectCount(column, alias); return this; } @Override public MPJLambdaWrapperX selectCount(SFunction column, SFunction alias) { super.selectCount(column, alias); return this; } @Override public MPJLambdaWrapperX selectSum(SFunction column) { super.selectSum(column); return this; } @Override public MPJLambdaWrapperX selectSum(SFunction column, String alias) { super.selectSum(column, alias); return this; } @Override public MPJLambdaWrapperX selectSum(SFunction column, SFunction alias) { super.selectSum(column, alias); return this; } @Override public MPJLambdaWrapperX selectMax(SFunction column) { super.selectMax(column); return this; } @Override public MPJLambdaWrapperX selectMax(SFunction column, String alias) { super.selectMax(column, alias); return this; } @Override public MPJLambdaWrapperX selectMax(SFunction column, SFunction alias) { super.selectMax(column, alias); return this; } @Override public MPJLambdaWrapperX selectMin(SFunction column) { super.selectMin(column); return this; } @Override public MPJLambdaWrapperX selectMin(SFunction column, String alias) { super.selectMin(column, alias); return this; } @Override public MPJLambdaWrapperX selectMin(SFunction column, SFunction alias) { super.selectMin(column, alias); return this; } @Override public MPJLambdaWrapperX selectAvg(SFunction column) { super.selectAvg(column); return this; } @Override public MPJLambdaWrapperX selectAvg(SFunction column, String alias) { super.selectAvg(column, alias); return this; } @Override public MPJLambdaWrapperX selectAvg(SFunction column, SFunction alias) { super.selectAvg(column, alias); return this; } @Override public MPJLambdaWrapperX selectLen(SFunction column) { super.selectLen(column); return this; } @Override public MPJLambdaWrapperX selectLen(SFunction column, String alias) { super.selectLen(column, alias); return this; } @Override public MPJLambdaWrapperX selectLen(SFunction column, SFunction alias) { super.selectLen(column, alias); return this; } }