ge(String column, Object params) {
return ge(true, column, params);
}
/**
*
* 等同于SQL的"field
*
* @param condition 拼接的前置条件
* @param column
* @param params
* @return
*/
public Wrapper lt(boolean condition, String column, Object params) {
if (condition) {
sql.WHERE(formatSql(String.format("%s < {0}", column), params));
}
return this;
}
/**
*
* 等同于SQL的"field
*
* @param column
* @param params
* @return
*/
public Wrapper lt(String column, Object params) {
return lt(true, column, params);
}
/**
*
* 等同于SQL的"field<=value"表达式
*
*
* @param condition 拼接的前置条件
* @param column
* @param params
* @return
*/
public Wrapper le(boolean condition, String column, Object params) {
if (condition) {
sql.WHERE(formatSql(String.format("%s <= {0}", column), params));
}
return this;
}
/**
*
* 等同于SQL的"field<=value"表达式
*
*
* @param column
* @param params
* @return
*/
public Wrapper le(String column, Object params) {
return le(true, column, params);
}
/**
*
* AND 连接后续条件
*
*
* @param condition 拼接的前置条件
* @param sqlAnd and条件语句
* @param params 参数集
* @return this
*/
public Wrapper and(boolean condition, String sqlAnd, Object... params) {
if (condition) {
sql.AND().WHERE(formatSql(sqlAnd, params));
}
return this;
}
/**
*
* AND 连接后续条件
*
*
* @param sqlAnd and条件语句
* @param params 参数集
* @return this
*/
public Wrapper and(String sqlAnd, Object... params) {
return and(true, sqlAnd, params);
}
/**
*
* 使用AND连接并换行
*
*
* eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
* (name='zhangsan' AND id=11) AND (statu=1)
*
*
* @param condition 拼接的前置条件
* @param sqlAnd AND 条件语句
* @param params 参数值
* @return this
*/
public Wrapper andNew(boolean condition, String sqlAnd, Object... params) {
if (condition) {
sql.AND_NEW().WHERE(formatSql(sqlAnd, params));
}
return this;
}
/**
*
* 使用AND连接并换行
*
*
* eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
* (name='zhangsan' AND id=11) AND (statu=1)
*
*
* @return this
*/
public Wrapper andNew() {
sql.AND_NEW();
return this;
}
/**
*
* 使用AND连接并换行
*
*
* eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
* (name='zhangsan' AND id=11) AND (statu=1)
*
*
* @param sqlAnd AND 条件语句
* @param params 参数值
* @return this
*/
public Wrapper andNew(String sqlAnd, Object... params) {
return andNew(true, sqlAnd, params);
}
/**
*
* 使用AND连接并换行
*
*
* @return this
*/
public Wrapper and() {
sql.AND();
return this;
}
/**
*
* 左嵌套方法
*
*
* @return this
*/
public Wrapper leftNest() {
leftNest(1);
return this;
}
/**
*
* 右嵌套方法
*
*
* @return this
*/
public Wrapper rightNest() {
rightNest(1);
return this;
}
/**
*
* 左嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
*
*
* @param num 嵌套数量
* @return this
*/
public Wrapper leftNest(int num) {
nest("(", num);
return this;
}
/**
*
* 右嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
*
*
* @param num 嵌套数量
* @return this
*/
public Wrapper rightNest(int num) {
nest(")", num);
return this;
}
/**
*
* 嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
*
*
* @param nest 嵌套字符串 "(" or ")"
* @param num 嵌套数量
* @return this
*/
private Wrapper nest(String nest, int num) {
if (num >= 1) {
StringBuilder builder = new StringBuilder(num);
for (int i = 0; i < num; i++) {
builder.append(nest);
}
sql.SUPP(builder.toString());
}
return this;
}
/**
*
* 使用OR连接并换行
*
*
* @return this
*/
public Wrapper or() {
sql.OR();
return this;
}
/**
*
* 添加OR条件
*
*
* @param condition 拼接的前置条件
* @param sqlOr or 条件语句
* @param params 参数集
* @return this
*/
public Wrapper or(boolean condition, String sqlOr, Object... params) {
if (condition) {
if (StringUtils.isEmpty(sql.toString())) {
AND_OR = "OR";
}
sql.OR().WHERE(formatSql(sqlOr, params));
}
return this;
}
/**
*
* 添加OR条件
*
*
* @param sqlOr or 条件语句
* @param params 参数集
* @return this
*/
public Wrapper or(String sqlOr, Object... params) {
return or(true, sqlOr, params);
}
/**
*
* 使用OR换行,并添加一个带()的新的条件
*
*
* eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
* (name='zhangsan' AND id=11) OR (statu=1)
*
*
* @return this
*/
public Wrapper orNew() {
sql.OR_NEW();
return this;
}
/**
*
* 使用OR换行,并添加一个带()的新的条件
*
*
* eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
* (name='zhangsan' AND id=11) OR (statu=1)
*
*
* @param condition 拼接的前置条件
* @param sqlOr AND 条件语句
* @param params 参数值
* @return this
*/
public Wrapper orNew(boolean condition, String sqlOr, Object... params) {
if (condition) {
if (StringUtils.isEmpty(sql.toString())) {
AND_OR = "OR";
}
sql.OR_NEW().WHERE(formatSql(sqlOr, params));
}
return this;
}
/**
*
* 使用OR换行,并添加一个带()的新的条件
*
*
* eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
* (name='zhangsan' AND id=11) OR (statu=1)
*
*
* @param sqlOr AND 条件语句
* @param params 参数值
* @return this
*/
public Wrapper orNew(String sqlOr, Object... params) {
return orNew(true, sqlOr, params);
}
/**
*
* SQL中groupBy关键字跟的条件语句
*
*
* eg: ew.where("name='zhangsan'").groupBy("id,name")
*
*
* @param condition 拼接的前置条件
* @param columns SQL 中的 Group by 语句,无需输入 Group By 关键字
* @return this
*/
public Wrapper groupBy(boolean condition, String columns) {
if (condition) {
sql.GROUP_BY(columns);
}
return this;
}
/**
*
* SQL中groupBy关键字跟的条件语句
*
*
* eg: ew.where("name='zhangsan'").groupBy("id,name")
*
*
* @param columns SQL 中的 Group by 语句,无需输入 Group By 关键字
* @return this
*/
public Wrapper groupBy(String columns) {
return groupBy(true, columns);
}
/**
*
* SQL中having关键字跟的条件语句
*
*
* eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null")
*
*
* @param condition 拼接的前置条件
* @param sqlHaving having关键字后面跟随的语句
* @param params 参数集
* @return EntityWrapper
*/
public Wrapper having(boolean condition, String sqlHaving, Object... params) {
if (condition) {
sql.HAVING(formatSql(sqlHaving, params));
}
return this;
}
/**
*
* SQL中having关键字跟的条件语句
*
*
* eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null")
*
*
* @param sqlHaving having关键字后面跟随的语句
* @param params 参数集
* @return EntityWrapper
*/
public Wrapper having(String sqlHaving, Object... params) {
return having(true, sqlHaving, params);
}
/**
*
* SQL中orderby关键字跟的条件语句
*
*
* eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null"
* ).orderBy("id,name")
*
*
* @param condition 拼接的前置条件
* @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
* @return this
*/
public Wrapper orderBy(boolean condition, String columns) {
if (condition) {
sql.ORDER_BY(columns);
}
return this;
}
/**
*
* SQL中orderby关键字跟的条件语句
*
*
* eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null"
* ).orderBy("id,name")
*
*
* @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
* @return this
*/
public Wrapper orderBy(String columns) {
return orderBy(true, columns);
}
/**
*
* SQL中orderby关键字跟的条件语句,可根据变更动态排序
*
*
* @param condition 拼接的前置条件
* @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
* @param isAsc 是否为升序
* @return this
*/
public Wrapper orderBy(boolean condition, String columns, boolean isAsc) {
if (condition && StringUtils.isNotEmpty(columns)) {
sql.ORDER_BY(columns + (isAsc ? " ASC" : " DESC"));
}
return this;
}
/**
*
* SQL中orderby关键字跟的条件语句,可根据变更动态排序
*
*
* @param condition 拼接的前置条件
* @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
* @param isAsc 是否为升序
* @return this
*/
public Wrapper orderBy(boolean condition, Collection columns, boolean isAsc) {
if (condition && CollectionUtils.isNotEmpty(columns)) {
for (String column : columns) {
orderBy(condition, column, isAsc);
}
}
return this;
}
/**
*
* SQL中orderby关键字跟的条件语句,可根据变更动态排序
*
*
* @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
* @param isAsc 是否为升序
* @return this
*/
public Wrapper orderBy(String columns, boolean isAsc) {
return orderBy(true, columns, isAsc);
}
/**
*
* 批量根据ASC排序
*
*
* @param columns 需要排序的集合
* @return this
*/
public Wrapper orderAsc(Collection columns) {
return orderBy(true, columns, true);
}
/**
*
* 批量根据DESC排序
*
*
* @param columns 需要排序的集合
* @return this
*/
public Wrapper orderDesc(Collection columns) {
return orderBy(true, columns, false);
}
/**
*
* LIKE条件语句,value中无需前后%
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 匹配值
* @return this
*/
public Wrapper like(boolean condition, String column, String value) {
if (condition) {
handerLike(column, value, SqlLike.DEFAULT, false);
}
return this;
}
/**
*
* LIKE条件语句,value中无需前后%
*
*
* @param column 字段名称
* @param value 匹配值
* @return this
*/
public Wrapper like(String column, String value) {
return like(true, column, value);
}
/**
*
* NOT LIKE条件语句,value中无需前后%
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 匹配值
* @return this
*/
public Wrapper notLike(boolean condition, String column, String value) {
if (condition) {
handerLike(column, value, SqlLike.DEFAULT, true);
}
return this;
}
/**
*
* NOT LIKE条件语句,value中无需前后%
*
*
* @param column 字段名称
* @param value 匹配值
* @return this
*/
public Wrapper notLike(String column, String value) {
return notLike(true, column, value);
}
/**
*
* 处理LIKE操作
*
*
* @param column 字段名称
* @param value like匹配值
* @param isNot 是否为NOT LIKE操作
*/
private void handerLike(String column, String value, SqlLike type, boolean isNot) {
if (StringUtils.isNotEmpty(column) && StringUtils.isNotEmpty(value)) {
StringBuilder inSql = new StringBuilder();
inSql.append(column);
if (isNot) {
inSql.append(" NOT");
}
inSql.append(" LIKE {0}");
sql.WHERE(formatSql(inSql.toString(), SqlUtils.concatLike(value, type)));
}
}
/**
*
* LIKE条件语句,value中无需前后%
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 匹配值
* @param type
* @return this
*/
public Wrapper like(boolean condition, String column, String value, SqlLike type) {
if (condition) {
handerLike(column, value, type, false);
}
return this;
}
/**
*
* LIKE条件语句,value中无需前后%
*
*
* @param column 字段名称
* @param value 匹配值
* @param type
* @return this
*/
public Wrapper like(String column, String value, SqlLike type) {
return like(true, column, value, type);
}
/**
*
* NOT LIKE条件语句,value中无需前后%
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 匹配值
* @param type
* @return this
*/
public Wrapper notLike(boolean condition, String column, String value, SqlLike type) {
if (condition) {
handerLike(column, value, type, true);
}
return this;
}
/**
*
* NOT LIKE条件语句,value中无需前后%
*
*
* @param column 字段名称
* @param value 匹配值
* @param type
* @return this
*/
public Wrapper notLike(String column, String value, SqlLike type) {
return notLike(true, column, value, type);
}
/**
*
* is not null 条件
*
*
* @param condition 拼接的前置条件
* @param columns 字段名称。多个字段以逗号分隔。
* @return this
*/
public Wrapper isNotNull(boolean condition, String columns) {
if (condition) {
sql.IS_NOT_NULL(columns);
}
return this;
}
/**
*
* is not null 条件
*
*
* @param columns 字段名称。多个字段以逗号分隔。
* @return this
*/
public Wrapper isNotNull(String columns) {
return isNotNull(true, columns);
}
/**
*
* is null 条件
*
*
* @param condition 拼接的前置条件
* @param columns 字段名称。多个字段以逗号分隔。
* @return this
*/
public Wrapper isNull(boolean condition, String columns) {
if (condition) {
sql.IS_NULL(columns);
}
return this;
}
/**
*
* is null 条件
*
*
* @param columns 字段名称。多个字段以逗号分隔。
* @return this
*/
public Wrapper isNull(String columns) {
return isNull(true, columns);
}
/**
*
* EXISTS 条件语句,目前适配mysql及oracle
*
*
* @param condition 拼接的前置条件
* @param value 匹配值
* @return this
*/
public Wrapper exists(boolean condition, String value) {
if (condition) {
sql.EXISTS(value);
}
return this;
}
/**
*
* EXISTS 条件语句,目前适配mysql及oracle
*
*
* @param value 匹配值
* @return this
*/
public Wrapper exists(String value) {
return exists(true, value);
}
/**
*
* NOT EXISTS条件语句
*
*
* @param condition 拼接的前置条件
* @param value 匹配值
* @return this
*/
public Wrapper notExists(boolean condition, String value) {
if (condition) {
sql.NOT_EXISTS(value);
}
return this;
}
/**
*
* NOT EXISTS条件语句
*
*
* @param value 匹配值
* @return this
*/
public Wrapper notExists(String value) {
return notExists(true, value);
}
/**
*
* IN 条件语句,目前适配mysql及oracle
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 逗号拼接的字符串
* @return this
*/
public Wrapper in(boolean condition, String column, String value) {
if (condition && StringUtils.isNotEmpty(value)) {
in(column, StringUtils.splitWorker(value, ",", -1, false));
}
return this;
}
/**
*
* IN 条件语句,目前适配mysql及oracle
*
*
* @param column 字段名称
* @param value 逗号拼接的字符串
* @return this
*/
public Wrapper in(String column, String value) {
return in(true, column, value);
}
/**
*
* NOT IN条件语句
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 逗号拼接的字符串
* @return this
*/
public Wrapper notIn(boolean condition, String column, String value) {
if (condition && StringUtils.isNotEmpty(value)) {
notIn(column, StringUtils.splitWorker(value, ",", -1, false));
}
return this;
}
/**
*
* NOT IN条件语句
*
*
* @param column 字段名称
* @param value 逗号拼接的字符串
* @return this
*/
public Wrapper notIn(String column, String value) {
return notIn(true, column, value);
}
/**
*
* IN 条件语句,目前适配mysql及oracle
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 匹配值 集合
* @return this
*/
public Wrapper in(boolean condition, String column, Collection> value) {
if (condition && CollectionUtils.isNotEmpty(value)) {
sql.WHERE(formatSql(inExpression(column, value, false), value.toArray()));
}
return this;
}
/**
*
* IN 条件语句,目前适配mysql及oracle
*
*
* @param column 字段名称
* @param value 匹配值 集合
* @return this
*/
public Wrapper in(String column, Collection> value) {
return in(true, column, value);
}
/**
*
* NOT IN 条件语句,目前适配mysql及oracle
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 匹配值 集合
* @return this
*/
public Wrapper notIn(boolean condition, String column, Collection> value) {
if (condition && CollectionUtils.isNotEmpty(value)) {
sql.WHERE(formatSql(inExpression(column, value, true), value.toArray()));
}
return this;
}
/**
*
* NOT IN 条件语句,目前适配mysql及oracle
*
*
* @param column 字段名称
* @param value 匹配值 集合
* @return this
*/
public Wrapper notIn(String column, Collection> value) {
return notIn(true, column, value);
}
/**
*
* IN 条件语句,目前适配mysql及oracle
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 匹配值 object数组
* @return this
*/
public Wrapper in(boolean condition, String column, Object[] value) {
if (condition && ArrayUtils.isNotEmpty(value)) {
sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), false), value));
}
return this;
}
/**
*
* IN 条件语句,目前适配mysql及oracle
*
*
* @param column 字段名称
* @param value 匹配值 object数组
* @return this
*/
public Wrapper in(String column, Object[] value) {
return in(true, column, value);
}
/**
*
* NOT IN 条件语句,目前适配mysql及oracle
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param value 匹配值 object数组
* @return this
*/
public Wrapper notIn(boolean condition, String column, Object... value) {
if (condition && ArrayUtils.isNotEmpty(value)) {
sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), true), value));
}
return this;
}
/**
*
* NOT IN 条件语句,目前适配mysql及oracle
*
*
* @param column 字段名称
* @param value 匹配值 object数组
* @return this
*/
public Wrapper notIn(String column, Object... value) {
return notIn(true, column, value);
}
/**
*
* 获取in表达式
*
*
* @param column 字段名称
* @param value 集合
* @param isNot 是否为NOT IN操作
*/
private String inExpression(String column, Collection> value, boolean isNot) {
if (StringUtils.isNotEmpty(column) && CollectionUtils.isNotEmpty(value)) {
StringBuilder inSql = new StringBuilder();
inSql.append(column);
if (isNot) {
inSql.append(" NOT");
}
inSql.append(" IN ");
inSql.append("(");
int size = value.size();
for (int i = 0; i < size; i++) {
inSql.append(String.format(PLACE_HOLDER, i));
if (i + 1 < size) {
inSql.append(",");
}
}
inSql.append(")");
return inSql.toString();
}
return null;
}
/**
*
* betwwee 条件语句
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param val1
* @param val2
* @return this
*/
public Wrapper between(boolean condition, String column, Object val1, Object val2) {
if (condition) {
sql.WHERE(formatSql(String.format("%s BETWEEN {0} AND {1}", column), val1, val2));
}
return this;
}
/**
*
* betwwee 条件语句
*
*
* @param column 字段名称
* @param val1
* @param val2
* @return this
*/
public Wrapper between(String column, Object val1, Object val2) {
return between(true, column, val1, val2);
}
/**
*
* NOT betwwee 条件语句
*
*
* @param condition 拼接的前置条件
* @param column 字段名称
* @param val1
* @param val2
* @return this
*/
public Wrapper notBetween(boolean condition, String column, Object val1, Object val2) {
if (condition) {
sql.WHERE(formatSql(String.format("%s NOT BETWEEN {0} AND {1}", column), val1, val2));
}
return this;
}
/**
*
* NOT betwwee 条件语句
*
*
* @param column 字段名称
* @param val1
* @param val2
* @return this
*/
public Wrapper notBetween(String column, Object val1, Object val2) {
return notBetween(true, column, val1, val2);
}
/**
*
* 为了兼容之前的版本,可使用where()或and()替代
*
*
* @param sqlWhere where sql部分
* @param params 参数集
* @return this
*/
public Wrapper addFilter(String sqlWhere, Object... params) {
return and(sqlWhere, params);
}
/**
*
* 根据判断条件来添加条件语句部分 使用 andIf() 替代
*
*
* eg: ew.filterIfNeed(false,"name='zhangsan'").where("name='zhangsan'")
* .filterIfNeed(true,"id={0}",22)
*
* 输出: WHERE (name='zhangsan' AND id=22)
*
*
* @param need 是否需要添加该条件
* @param sqlWhere 条件语句
* @param params 参数集
* @return this
*/
public Wrapper addFilterIfNeed(boolean need, String sqlWhere, Object... params) {
return need ? where(sqlWhere, params) : this;
}
/**
*
* 格式化SQL
*
*
* @param sqlStr SQL语句部分
* @param params 参数集
* @return this
*/
protected String formatSql(String sqlStr, Object... params) {
return formatSqlIfNeed(true, sqlStr, params);
}
/**
*
* 根据需要格式化SQL
*
* Format SQL for methods: EntityWrapper.where/and/or...("name={0}", value);
* ALL the {i} will be replaced with #{MPGENVALi}
*
* ew.where("sample_name={0}", "haha").and("sample_age >{0}
* and sample_age<{1}", 18, 30) TO
* sample_name=#{MPGENVAL1} and sample_age>#{MPGENVAL2} and
* sample_age<#{MPGENVAL3}
*
*
* @param need 是否需要格式化
* @param sqlStr SQL语句部分
* @param params 参数集
* @return this
*/
protected String formatSqlIfNeed(boolean need, String sqlStr, Object... params) {
if (!need || StringUtils.isEmpty(sqlStr)) {
return null;
}
// #200
if (ArrayUtils.isNotEmpty(params)) {
for (int i = 0; i < params.length; ++i) {
String genParamName = MP_GENERAL_PARAMNAME + paramNameSeq.incrementAndGet();
sqlStr = sqlStr.replace(String.format(PLACE_HOLDER, i),
String.format(MYBATIS_PLUS_TOKEN, getParamAlias(), genParamName));
paramNameValuePairs.put(genParamName, params[i]);
}
}
return sqlStr;
}
/**
*
* 自定义是否输出sql开头为 `WHERE` OR `AND` OR `OR`
*
*
* @param bool
* @return this
*/
public Wrapper isWhere(Boolean bool) {
this.isWhere = bool;
return this;
}
/**
*
* 手动把sql拼接到最后(有sql注入的风险,请谨慎使用)
*
*
* @param limit
* @return this
*/
public Wrapper last(String limit) {
sql.LAST(limit);
return this;
}
/**
* Fix issue 200.
*
* @return
* @since 2.0.3
*/
public Map getParamNameValuePairs() {
return paramNameValuePairs;
}
public String getParamAlias() {
return StringUtils.isEmpty(paramAlias) ? DEFAULT_PARAM_ALIAS : paramAlias;
}
/**
*
* 参数别名设置,初始化时优先设置该值、重复设置异常
*
*
* @param paramAlias 参数别名
* @return
*/
public Wrapper setParamAlias(String paramAlias) {
if (StringUtils.isNotEmpty(getSqlSegment())) {
throw new MybatisPlusException("Error: Please call this method when initializing!");
}
if (StringUtils.isNotEmpty(this.paramAlias)) {
throw new MybatisPlusException("Error: Please do not call the method repeatedly!");
}
this.paramAlias = paramAlias;
return this;
}
/**
*
* 克隆本身 fixed github issues/241
*
*/
@Override
public Wrapper clone() {
return SerializationUtils.clone(this);
}
@Override
public String getSqlSegment() {
// 子类重载
return null;
}
}