package com.iailab.framework.mybatis.core.query; import cn.hutool.core.lang.Assert; import com.iailab.framework.mybatis.core.enums.SqlConstants; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import org.springframework.util.StringUtils; import java.util.Collection; /** * 拓展 MyBatis Plus QueryWrapper 类,主要增加如下功能: * * 1. 拼接条件的方法,增加 xxxIfPresent 方法,用于判断值不存在的时候,不要拼接到条件中。 * * @param 数据类型 */ public class QueryWrapperX extends QueryWrapper { public QueryWrapperX likeIfPresent(String column, String val) { if (StringUtils.hasText(val)) { return (QueryWrapperX) super.like(column, val); } return this; } public QueryWrapperX inIfPresent(String column, Collection values) { if (!CollectionUtils.isEmpty(values)) { return (QueryWrapperX) super.in(column, values); } return this; } public QueryWrapperX inIfPresent(String column, Object... values) { if (!ArrayUtils.isEmpty(values)) { return (QueryWrapperX) super.in(column, values); } return this; } public QueryWrapperX eqIfPresent(String column, Object val) { if (val != null) { return (QueryWrapperX) super.eq(column, val); } return this; } public QueryWrapperX neIfPresent(String column, Object val) { if (val != null) { return (QueryWrapperX) super.ne(column, val); } return this; } public QueryWrapperX gtIfPresent(String column, Object val) { if (val != null) { return (QueryWrapperX) super.gt(column, val); } return this; } public QueryWrapperX geIfPresent(String column, Object val) { if (val != null) { return (QueryWrapperX) super.ge(column, val); } return this; } public QueryWrapperX ltIfPresent(String column, Object val) { if (val != null) { return (QueryWrapperX) super.lt(column, val); } return this; } public QueryWrapperX leIfPresent(String column, Object val) { if (val != null) { return (QueryWrapperX) super.le(column, val); } return this; } public QueryWrapperX betweenIfPresent(String column, Object val1, Object val2) { if (val1 != null && val2 != null) { return (QueryWrapperX) super.between(column, val1, val2); } if (val1 != null) { return (QueryWrapperX) ge(column, val1); } if (val2 != null) { return (QueryWrapperX) le(column, val2); } return this; } public QueryWrapperX betweenIfPresent(String column, Object[] values) { if (values!= null && values.length != 0 && values[0] != null && values[1] != null) { return (QueryWrapperX) super.between(column, values[0], values[1]); } if (values!= null && values.length != 0 && values[0] != null) { return (QueryWrapperX) ge(column, values[0]); } if (values!= null && values.length != 0 && values[1] != null) { return (QueryWrapperX) le(column, values[1]); } return this; } // ========== 重写父类方法,方便链式调用 ========== @Override public QueryWrapperX eq(boolean condition, String column, Object val) { super.eq(condition, column, val); return this; } @Override public QueryWrapperX eq(String column, Object val) { super.eq(column, val); return this; } @Override public QueryWrapperX orderByDesc(String column) { super.orderByDesc(true, column); return this; } @Override public QueryWrapperX last(String lastSql) { super.last(lastSql); return this; } @Override public QueryWrapperX in(String column, Collection coll) { super.in(column, coll); return this; } /** * 设置只返回最后一条 * * TODO iailab:不是完美解,需要在思考下。如果使用多数据源,并且数据源是多种类型时,可能会存在问题:实现之返回一条的语法不同 * * @return this */ public QueryWrapperX limitN(int n) { Assert.notNull(SqlConstants.DB_TYPE, "获取不到数据库的类型"); switch (SqlConstants.DB_TYPE) { case ORACLE: case ORACLE_12C: super.le("ROWNUM", n); break; case SQL_SERVER: case SQL_SERVER2005: super.select("TOP " + n + " *"); // 由于 SQL Server 是通过 SELECT TOP 1 实现限制一条,所以只好使用 * 查询剩余字段 break; default: super.last("LIMIT " + n); } return this; } }