mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
feat: QueryWrapper add "not like" support
This commit is contained in:
parent
c29e58ba10
commit
269cb1354d
@ -114,6 +114,7 @@ public final class SqlConsts {
|
|||||||
public static final String LT = " < ";
|
public static final String LT = " < ";
|
||||||
public static final String LE = " <= ";
|
public static final String LE = " <= ";
|
||||||
public static final String LIKE = " LIKE ";
|
public static final String LIKE = " LIKE ";
|
||||||
|
public static final String NOT_LIKE = " NOT LIKE ";
|
||||||
public static final String EQUALS = " = ";
|
public static final String EQUALS = " = ";
|
||||||
public static final String NOT_EQUALS = " != ";
|
public static final String NOT_EQUALS = " != ";
|
||||||
public static final String IS_NULL = " IS NULL ";
|
public static final String IS_NULL = " IS NULL ";
|
||||||
@ -144,4 +145,4 @@ public final class SqlConsts {
|
|||||||
public static final String EQUALS_PLACEHOLDER = " = ? ";
|
public static final String EQUALS_PLACEHOLDER = " = ? ";
|
||||||
public static final String AND_PLACEHOLDER = BLANK + PLACEHOLDER + AND + PLACEHOLDER + BLANK;
|
public static final String AND_PLACEHOLDER = BLANK + PLACEHOLDER + AND + PLACEHOLDER + BLANK;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,6 +35,9 @@ public enum SqlOperator {
|
|||||||
// like
|
// like
|
||||||
LIKE(SqlConsts.LIKE),
|
LIKE(SqlConsts.LIKE),
|
||||||
|
|
||||||
|
// not like
|
||||||
|
NOT_LIKE(SqlConsts.NOT_LIKE),
|
||||||
|
|
||||||
// =
|
// =
|
||||||
EQUALS(SqlConsts.EQUALS),
|
EQUALS(SqlConsts.EQUALS),
|
||||||
|
|
||||||
|
|||||||
@ -224,6 +224,7 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
|
|||||||
return QueryCondition.create(this, SqlConsts.LIKE, "%" + value).when(fn);
|
return QueryCondition.create(this, SqlConsts.LIKE, "%" + value).when(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public QueryCondition likeRaw(Object value) {
|
public QueryCondition likeRaw(Object value) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return QueryCondition.createEmpty();
|
return QueryCondition.createEmpty();
|
||||||
@ -238,6 +239,59 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
|
|||||||
return QueryCondition.create(this, SqlConsts.LIKE, value).when(fn);
|
return QueryCondition.create(this, SqlConsts.LIKE, value).when(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* not like %%
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
public QueryCondition notLike(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return QueryCondition.createEmpty();
|
||||||
|
}
|
||||||
|
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value + "%");
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> QueryCondition notLike(Object value, Predicate<T> fn) {
|
||||||
|
if (value == null) {
|
||||||
|
return QueryCondition.createEmpty();
|
||||||
|
}
|
||||||
|
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value + "%").when(fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public QueryCondition notLikeLeft(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return QueryCondition.createEmpty();
|
||||||
|
}
|
||||||
|
return QueryCondition.create(this, SqlConsts.NOT_LIKE, value + "%");
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> QueryCondition notLikeLeft(Object value, Predicate<T> fn) {
|
||||||
|
if (value == null) {
|
||||||
|
return QueryCondition.createEmpty();
|
||||||
|
}
|
||||||
|
return QueryCondition.create(this, SqlConsts.NOT_LIKE, value + "%").when(fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public QueryCondition notLikeRight(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return QueryCondition.createEmpty();
|
||||||
|
}
|
||||||
|
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> QueryCondition notLikeRight(Object value, Predicate<T> fn) {
|
||||||
|
if (value == null) {
|
||||||
|
return QueryCondition.createEmpty();
|
||||||
|
}
|
||||||
|
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value).when(fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 大于 greater than
|
* 大于 greater than
|
||||||
*
|
*
|
||||||
|
|||||||
@ -109,7 +109,8 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
|
|||||||
|
|
||||||
public <T> QueryCondition when(Predicate<T> fn) {
|
public <T> QueryCondition when(Predicate<T> fn) {
|
||||||
Object val = this.value;
|
Object val = this.value;
|
||||||
if (SqlConsts.LIKE.equals(logic) && val instanceof String) {
|
if ((SqlConsts.LIKE.equals(logic) || SqlConsts.NOT_LIKE.equals(logic))
|
||||||
|
&& val instanceof String) {
|
||||||
String valStr = (String) val;
|
String valStr = (String) val;
|
||||||
if (valStr.startsWith(SqlConsts.PERCENT_SIGN)) {
|
if (valStr.startsWith(SqlConsts.PERCENT_SIGN)) {
|
||||||
valStr = valStr.substring(1);
|
valStr = valStr.substring(1);
|
||||||
|
|||||||
@ -203,6 +203,19 @@ public class AccountSqlTester {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWhereSql3() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select()
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.ID.ge(100))
|
||||||
|
.and(ACCOUNT.USER_NAME.notLike("michael"));
|
||||||
|
|
||||||
|
System.out.println(queryWrapper.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWhereCond2Sql() {
|
public void testWhereCond2Sql() {
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user