A list,

In the previous section, we mentioned that when CRUD operations are performed, they can be obtained by passing an UpdateWrapper or QueryWrapper as a condition for updating or querying the operations. In this section, we will show you how to use these two types of wrappers.

The parent classes of QueryWrapper(LambdaQueryWrapper) and UpdateWrapper(LambdaUpdateWrapper) are used to generate SQL where conditions, The Entity attribute is also used to generate SQL WHERE conditions

Second, the AbstractWrapper

Unified description of parameters:

  • Boolean condition: indicates whether the condition is added to the final GENERATED SQL.

    For example, query.eq(stringutils.isNotblank (name), Entity::getName, name), this eq is not generated in the WHERE condition when name is empty;

  • Null2IsNull: Whether to add a parameter to the WHERE condition when the value of Map in the parameter is null:

    Such as:

    Default: allEq({id:1,name:” Lao Wang “,age:null})–>id = 1 and name = ‘Lao Wang’ and age is null

    Null2IsNull == false: allEq({id:1,name:” Lao Wang “,age:null}, false)– >id = 1 and name = ‘Lao Wang’

Used to generate a WHERE condition:

conditions function instructions
allEq allEq(Map<R, V> params)

allEq(Map<R, V> params, boolean null2IsNull)

allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
All =, the parameters in the map, generate where, and join;
eq eq(R column, Object val)

eq(boolean condition, R column, Object val)
Equal to =
ne ne(R column, Object val)

ne(boolean condition, R column, Object val)
Is not the same as < >
gt gt(R column, Object val)

gt(boolean condition, R column, Object val)
More than >
ge ge(R column, Object val)

ge(boolean condition, R column, Object val)
Greater than or equal to greater than or equal to
lt lt(R column, Object val)

lt(boolean condition, R column, Object val)
< <
le le(R column, Object val)

le(boolean condition, R column, Object val)
Less than or equal to <=
between between(R column, Object val1, Object val2)

between(boolean condition, R column, Object val1, Object val2)
BETWEEN value 1 AND value 2
notBetween notBetween(R column, Object val1, Object val2)

notBetween(boolean condition, R column, Object val1, Object val2)
NOT BETWEEN value 1 AND value 2
like like(R column, Object val)

like(boolean condition, R column, Object val)
LIKE ‘% % values’
notLike notLike(R column, Object val)

notLike(boolean condition, R column, Object val)
notLike
likeLeft likeLeft(R column, Object val) likeLeft(boolean condition, R column, Object val) LIKE ‘% values’
likeRight likeRight(R column, Object val) likeRight(boolean condition, R column, Object val) LIKE ‘% values’
isNull isNull(R column)

isNull(boolean condition, R column)
Field IS NULL
isNotNull isNotNull(R column)

isNotNull(boolean condition, R column)
The field IS NOT NULL
in in(R column, Collection<? > value)

in(boolean condition, R column,Collection< ? >value

in(R column, Object… values)

in(boolean condition, R column, Object… values)
IN (value.get(0), value.get(1),…)

In (” age “, {1, 2, 3})--->The age in (1, 2, 3)

in(“age”, 1, 2, 3)--->The age in (1, 2, 3)
notIn notIn(R column, Collection value) notIn(boolean condition, R column, Collection value)

notIn(R column, Object… values)

notIn(boolean condition, R column, Object… values)
NOT IN (value.get(0), value.get(1),…)
inSql inSql(R column, String inValue)

inSql(boolean condition, R column, String inValue)
Field IN (SQL statement)

Example: inSql (” age “, “6”) — – > age in (6)

InSql (” select id from table where id < 3″)– > inSql(” select id from table where id < 3″)– > inSql(” select id from table where id < 3″)
notInSql notInSql(R column, String inValue)

notInSql(boolean condition, R column, String inValue)
Field NOT IN (SQL statement)
groupBy groupBy(R… columns)

groupBy(boolean condition, R… columns)
GROUP BY
orderByAsc orderByAsc(R… columns)

orderByAsc(boolean condition, R… columns)
ORDER BY field… ASC
orderByDesc orderByDesc(R… columns)

orderByDesc(boolean condition, R… columns)
Sort: ORDER BY field,… DESC
orderBy orderBy(boolean condition, boolean isAsc, R… columns) Sort: ORDER BY field
having having(String sqlHaving, Object… params)

having(boolean condition, String sqlHaving, Object… params)
HAVING (SQL Statement)

having(“sum(age) > 10”)->having sum(age) > 10
func func(Consumer consumer)

func(boolean condition, Consumer consumer)
The func method (mainly convenient in the presence of if… Else call different methods can keep the chain)func(i -> if(true) {i.eq("id", 1)} else {i.ne("id", 1)})
or or() or(boolean condition)or(Consumer consumer)

or(boolean condition, Consumer consumer)
Joining together the OR

Eq (” id “, 1) or (.) eq (” name “, “wang”) – > id = 1 or name = ‘Lao wang’
and and(Consumer consumer)

and(boolean condition, Consumer consumer)
AND nested

And (I -> i.e (“name”, “li “).ne(“status”,” alive “))--->And (name = ‘liba’ and status <> ‘liba ‘)
nested nested(Consumer consumer)

nested(boolean condition, Consumer consumer)
Normal nesting does not take AND OR OR

Nested (I – > appropriate precautions q (” name “, “li bai”). Ne (” status “, “live”))--->(name = ‘liba’ and status <> ‘liba ‘)
apply apply(String applySql, Object… params)

apply(boolean condition, String applySql, Object… params)
Stitching SQL

Example: apply(“id = 1”)– >id = 1

Example: apply (” date_format (dateColumn, ‘Y – m – % d % %) =’ 2008-08-08 ‘”) – > date_format (dateColumn,’ Y – m – % d % %) = ‘2008-08-08’)”

Ex. : apply(“date_format(dateColumn,’%Y-%m-%d’) = {0}”, “2008-08-08″)—>date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08′”)
last last(String lastSql)

last(boolean condition, String lastSql)
Ignore optimization rules and concatenate directly to the end of the SQL

The call can be invoked only once. If the last call is used for multiple calls, SQL injection may occur. Exercise caution when using the call

last(“limit 1”)
exists exists(String existsSql)

exists(boolean condition, String existsSql)
Joining together the EXISTS

Example: exists(“select id from table where age = 1”)– >exists (select id from table where age = 1)
notExists notExists(String notExistsSql)

notExists(boolean condition, String notExistsSql)
NOT EXISTS (SQL statement)

Example: notExists(“select id from table where age = 1”)– >not exists (select ID from table where age = 1)

Third, QueryWrapper

Select Sets the query fields

select(String... sqlSelect)
select(Predicate<TableFieldInfo> predicate)
select(Class<T> entityClass, Predicate<TableFieldInfo> predicate)
Copy the code

The second and third usages can be used to exclude fields in the case of multiple fields, for example:

queryWrapper.select(info -> info.getProperty().startsWith("test"));
Copy the code
queryWrapper.select(User.class, info->! info.getColumn() .equals("email") && !info.getColumn().equals("create_time"));
Copy the code

Four, UpdateWrapper

  1. SET the SET field
set(String column, Object val)
set(boolean condition, String column, Object val)
Copy the code
  • Ex. :Set ("name", "Lao Li Tou ")
  • Ex. :set("name", "")–> The database field value changesAn empty string
  • Ex. :set("name", null)–> The database field value changesnull

2. SET the SET SQL

setSql(String sql)
Copy the code

Example :setSql(“name = ‘old’ header ‘”)