diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConsts.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConsts.java index ad3a7f83..cea4050f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConsts.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConsts.java @@ -114,6 +114,7 @@ public final class SqlConsts { public static final String LT = " < "; public static final String LE = " <= "; public static final String LIKE = " LIKE "; + public static final String NOT_LIKE = " NOT LIKE "; public static final String EQUALS = " = "; public static final String NOT_EQUALS = " != "; 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 AND_PLACEHOLDER = BLANK + PLACEHOLDER + AND + PLACEHOLDER + BLANK; -} \ No newline at end of file +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlOperator.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlOperator.java index fb114f65..bea594f0 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlOperator.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlOperator.java @@ -35,6 +35,9 @@ public enum SqlOperator { // like LIKE(SqlConsts.LIKE), + // not like + NOT_LIKE(SqlConsts.NOT_LIKE), + // = EQUALS(SqlConsts.EQUALS), diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java index e624714c..3341829e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java @@ -224,6 +224,7 @@ public class QueryColumn implements CloneSupport { return QueryCondition.create(this, SqlConsts.LIKE, "%" + value).when(fn); } + public QueryCondition likeRaw(Object value) { if (value == null) { return QueryCondition.createEmpty(); @@ -238,6 +239,59 @@ public class QueryColumn implements CloneSupport { 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 QueryCondition notLike(Object value, Predicate 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 QueryCondition notLikeLeft(Object value, Predicate 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 QueryCondition notLikeRight(Object value, Predicate fn) { + if (value == null) { + return QueryCondition.createEmpty(); + } + return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value).when(fn); + } + + + /** * 大于 greater than * diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java index 119f53c2..2fd57ddd 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java @@ -109,7 +109,8 @@ public class QueryCondition implements CloneSupport { public QueryCondition when(Predicate fn) { 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; if (valStr.startsWith(SqlConsts.PERCENT_SIGN)) { valStr = valStr.substring(1); diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java index f78b056f..edef7443 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java @@ -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 public void testWhereCond2Sql() { boolean flag = false;