123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629 |
- /*
- * Copyright (c) 2011-2014, hubin (jobob@qq.com).
- * <p>
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.baomidou.mybatisplus.mapper;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.concurrent.atomic.AtomicInteger;
- import com.baomidou.mybatisplus.core.conditions.SqlPlus;
- import com.baomidou.mybatisplus.core.enums.SqlLike;
- import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
- import com.baomidou.mybatisplus.core.metadata.Column;
- import com.baomidou.mybatisplus.core.metadata.Columns;
- import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
- import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
- import com.baomidou.mybatisplus.core.toolkit.MapUtils;
- import com.baomidou.mybatisplus.core.toolkit.SerializationUtils;
- import com.baomidou.mybatisplus.core.toolkit.StringUtils;
- import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
- /**
- * <p>
- * 条件构造抽象类,定义T-SQL语法
- * </p>
- *
- * @author hubin , yanghu , Dyang , Caratacus, hcl
- * @Date 2016-11-7
- */
- @Deprecated
- public class Wrapper<T> extends com.baomidou.mybatisplus.core.conditions.Wrapper<T> {
- /**
- * 占位符
- */
- private static final String PLACE_HOLDER = "{%s}";
- private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}";
- private static final String MP_GENERAL_PARAMNAME = "MPGENVAL";
- private static final String DEFAULT_PARAM_ALIAS = "ew";
- /**
- * 实现了TSQL语法的SQL实体
- */
- protected final SqlPlus sql = new SqlPlus();
- private final Map<String, Object> paramNameValuePairs = new HashMap<>();
- private final AtomicInteger paramNameSeq = new AtomicInteger(0);
- protected String paramAlias = null;
- /**
- * SQL 查询字段内容,例如:id,name,age
- */
- protected String sqlSelect = null;
- /**
- * 自定义是否输出sql为 WHERE OR AND OR OR
- */
- protected Boolean isWhere;
- /**
- * 拼接WHERE后应该是AND还是ORnull
- */
- protected String AND_OR = "AND";
- public static Wrapper getInstance() {
- return new Wrapper();
- }
- /**
- * <p>
- * 兼容EntityWrapper
- * </p>
- *
- * @return
- */
- @Override
- public T getEntity() {
- return null;
- }
- /**
- * 查看where构造是否为空
- *
- * @return
- */
- public boolean isEmptyOfWhere() {
- return sql.isEmptyOfWhere();
- }
- /**
- * 查看where构造是否不为空
- *
- * @return
- */
- public boolean isNotEmptyOfWhere() {
- return !isEmptyOfWhere();
- }
- @Override
- public String getSqlSelect() {
- return StringUtils.isEmpty(sqlSelect) ? null : SqlUtils.stripSqlInjection(sqlSelect);
- }
- public Wrapper<T> setSqlSelect(String sqlSelect) {
- if (StringUtils.isNotEmpty(sqlSelect)) {
- this.sqlSelect = sqlSelect;
- }
- return this;
- }
- /**
- * <p>
- * 使用字符串数组封装sqlSelect,便于在不需要指定 AS 的情况下通过实体类自动生成的列静态字段快速组装 sqlSelect,<br/>
- * 减少手动录入的错误率
- * </p>
- *
- * @param columns 字段
- * @return
- */
- public Wrapper<T> setSqlSelect(String... columns) {
- StringBuilder builder = new StringBuilder();
- for (String column : columns) {
- if (StringUtils.isNotEmpty(column)) {
- if (builder.length() > 0) {
- builder.append(",");
- }
- builder.append(column);
- }
- }
- this.sqlSelect = builder.toString();
- return this;
- }
- /**
- * <p>
- * 使用对象封装的setsqlselect
- * </p>
- *
- * @param column 字段
- * @return
- */
- public Wrapper<T> setSqlSelect(Column... column) {
- if (ArrayUtils.isNotEmpty(column)) {
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < column.length; i++) {
- if (column[i] != null) {
- String col = column[i].getColumn();
- String as = column[i].getAs();
- if (StringUtils.isEmpty(col)) {
- continue;
- }
- builder.append(col).append(as);
- if (i < column.length - 1) {
- builder.append(",");
- }
- }
- }
- this.sqlSelect = builder.toString();
- }
- return this;
- }
- /**
- * <p>
- * 使用对象封装的setsqlselect
- * </p>
- *
- * @param columns 字段
- * @return
- */
- public Wrapper<T> setSqlSelect(Columns columns) {
- Column[] columnArray = columns.getColumns();
- if (ArrayUtils.isNotEmpty(columnArray)) {
- setSqlSelect(columnArray);
- }
- return this;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder("Wrapper<T>:");
- String sqlSegment = getSqlSegment();
- sb.append(replacePlaceholder(sqlSegment));
- Object entity = getEntity();
- if (entity != null) {
- sb.append("\n");
- sb.append("entity=").append(entity.toString());
- }
- return sb.toString();
- }
- /**
- * <p>
- * 替换占位符
- * </p>
- *
- * @param sqlSegment
- * @return
- */
- private String replacePlaceholder(String sqlSegment) {
- if (StringUtils.isEmpty(sqlSegment)) {
- return StringUtils.EMPTY;
- }
- return sqlSegment.replaceAll("#\\{" + getParamAlias() + ".paramNameValuePairs.MPGENVAL[0-9]+}", "\\?");
- }
- /**
- * <p>
- * 原生占位符sql
- * </p>
- *
- * @return
- */
- public String originalSql() {
- return replacePlaceholder(getSqlSegment());
- }
- /**
- * <p>
- * SQL中WHERE关键字跟的条件语句
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").where(id!=null, "id={0}", id);
- * <p>
- * 输出:<br>
- * 如果id=123: WHERE (NAME='zhangsan' AND id=123)<br>
- * 如果id=null: WHERE (NAME='zhangsan')
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param sqlWhere where语句
- * @param params 参数集
- * @return this
- */
- public Wrapper<T> where(boolean condition, String sqlWhere, Object... params) {
- if (condition) {
- sql.WHERE(formatSql(sqlWhere, params));
- }
- return this;
- }
- /**
- * <p>
- * SQL中WHERE关键字跟的条件语句
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").where("id={0}","123");
- * <p>
- * 输出: WHERE (NAME='zhangsan' AND id=123)
- * </p>
- *
- * @param sqlWhere where语句
- * @param params 参数集
- * @return this
- */
- public Wrapper<T> where(String sqlWhere, Object... params) {
- return where(true, sqlWhere, params);
- }
- /**
- * <p>
- * 等同于SQL的"field=value"表达式
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> eq(boolean condition, String column, Object params) {
- if (condition) {
- sql.WHERE(formatSql(String.format("%s = {0}", column), params));
- }
- return this;
- }
- /**
- * <p>
- * 等同于SQL的"field=value"表达式
- * </p>
- *
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> eq(String column, Object params) {
- return eq(true, column, params);
- }
- /**
- * <p>
- * 等同于SQL的"field <> value"表达式
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> ne(boolean condition, String column, Object params) {
- if (condition) {
- sql.WHERE(formatSql(String.format("%s <> {0}", column), params));
- }
- return this;
- }
- /**
- * <p>
- * 等同于SQL的"field <> value"表达式
- * </p>
- *
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> ne(String column, Object params) {
- return ne(true, column, params);
- }
- /**
- * <p>
- * 等同于SQL的"field=value"表达式
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param params
- * @return
- */
- @SuppressWarnings({"rawtypes", "unchecked"})
- public Wrapper<T> allEq(boolean condition, Map<String, Object> params) {
- if (condition && MapUtils.isNotEmpty(params)) {
- Iterator iterator = params.entrySet().iterator();
- while (iterator.hasNext()) {
- Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();
- Object value = entry.getValue();
- if (StringUtils.checkValNotNull(value)) {
- sql.WHERE(formatSql(String.format("%s = {0}", entry.getKey()), entry.getValue()));
- }
- }
- }
- return this;
- }
- /**
- * <p>
- * 等同于SQL的"field=value"表达式
- * </p>
- *
- * @param params
- * @return
- */
- @SuppressWarnings({"rawtypes", "unchecked"})
- public Wrapper<T> allEq(Map<String, Object> params) {
- return allEq(true, params);
- }
- /**
- * <p>
- * 等同于SQL的"field>value"表达式
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> gt(boolean condition, String column, Object params) {
- if (condition) {
- sql.WHERE(formatSql(String.format("%s > {0}", column), params));
- }
- return this;
- }
- /**
- * <p>
- * 等同于SQL的"field>value"表达式
- * </p>
- *
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> gt(String column, Object params) {
- return gt(true, column, params);
- }
- /**
- * <p>
- * 等同于SQL的"field>=value"表达式
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> ge(boolean condition, String column, Object params) {
- if (condition) {
- sql.WHERE(formatSql(String.format("%s >= {0}", column), params));
- }
- return this;
- }
- /**
- * <p>
- * 等同于SQL的"field>=value"表达式
- * </p>
- *
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> ge(String column, Object params) {
- return ge(true, column, params);
- }
- /**
- * <p>
- * 等同于SQL的"field<value"表达式
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> lt(boolean condition, String column, Object params) {
- if (condition) {
- sql.WHERE(formatSql(String.format("%s < {0}", column), params));
- }
- return this;
- }
- /**
- * <p>
- * 等同于SQL的"field<value"表达式
- * </p>
- *
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> lt(String column, Object params) {
- return lt(true, column, params);
- }
- /**
- * <p>
- * 等同于SQL的"field<=value"表达式
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> le(boolean condition, String column, Object params) {
- if (condition) {
- sql.WHERE(formatSql(String.format("%s <= {0}", column), params));
- }
- return this;
- }
- /**
- * <p>
- * 等同于SQL的"field<=value"表达式
- * </p>
- *
- * @param column
- * @param params
- * @return
- */
- public Wrapper<T> le(String column, Object params) {
- return le(true, column, params);
- }
- /**
- * <p>
- * AND 连接后续条件
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param sqlAnd and条件语句
- * @param params 参数集
- * @return this
- */
- public Wrapper<T> and(boolean condition, String sqlAnd, Object... params) {
- if (condition) {
- sql.AND().WHERE(formatSql(sqlAnd, params));
- }
- return this;
- }
- /**
- * <p>
- * AND 连接后续条件
- * </p>
- *
- * @param sqlAnd and条件语句
- * @param params 参数集
- * @return this
- */
- public Wrapper<T> and(String sqlAnd, Object... params) {
- return and(true, sqlAnd, params);
- }
- /**
- * <p>
- * 使用AND连接并换行
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
- * (name='zhangsan' AND id=11) AND (statu=1)
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param sqlAnd AND 条件语句
- * @param params 参数值
- * @return this
- */
- public Wrapper<T> andNew(boolean condition, String sqlAnd, Object... params) {
- if (condition) {
- sql.AND_NEW().WHERE(formatSql(sqlAnd, params));
- }
- return this;
- }
- /**
- * <p>
- * 使用AND连接并换行
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
- * (name='zhangsan' AND id=11) AND (statu=1)
- * </p>
- *
- * @return this
- */
- public Wrapper<T> andNew() {
- sql.AND_NEW();
- return this;
- }
- /**
- * <p>
- * 使用AND连接并换行
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
- * (name='zhangsan' AND id=11) AND (statu=1)
- * </p>
- *
- * @param sqlAnd AND 条件语句
- * @param params 参数值
- * @return this
- */
- public Wrapper<T> andNew(String sqlAnd, Object... params) {
- return andNew(true, sqlAnd, params);
- }
- /**
- * <p>
- * 使用AND连接并换行
- * </p>
- *
- * @return this
- */
- public Wrapper<T> and() {
- sql.AND();
- return this;
- }
- /**
- * <p>
- * 左嵌套方法
- * </p>
- *
- * @return this
- */
- public Wrapper<T> leftNest() {
- leftNest(1);
- return this;
- }
- /**
- * <p>
- * 右嵌套方法
- * </p>
- *
- * @return this
- */
- public Wrapper<T> rightNest() {
- rightNest(1);
- return this;
- }
- /**
- * <p>
- * 左嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
- * </p>
- *
- * @param num 嵌套数量
- * @return this
- */
- public Wrapper<T> leftNest(int num) {
- nest("(", num);
- return this;
- }
- /**
- * <p>
- * 右嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
- * </p>
- *
- * @param num 嵌套数量
- * @return this
- */
- public Wrapper<T> rightNest(int num) {
- nest(")", num);
- return this;
- }
- /**
- * <p>
- * 嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
- * </p>
- *
- * @param nest 嵌套字符串 "(" or ")"
- * @param num 嵌套数量
- * @return this
- */
- private Wrapper<T> 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;
- }
- /**
- * <p>
- * 使用OR连接并换行
- * </p>
- *
- * @return this
- */
- public Wrapper<T> or() {
- sql.OR();
- return this;
- }
- /**
- * <p>
- * 添加OR条件
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param sqlOr or 条件语句
- * @param params 参数集
- * @return this
- */
- public Wrapper<T> 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;
- }
- /**
- * <p>
- * 添加OR条件
- * </p>
- *
- * @param sqlOr or 条件语句
- * @param params 参数集
- * @return this
- */
- public Wrapper<T> or(String sqlOr, Object... params) {
- return or(true, sqlOr, params);
- }
- /**
- * <p>
- * 使用OR换行,并添加一个带()的新的条件
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
- * (name='zhangsan' AND id=11) OR (statu=1)
- * </p>
- *
- * @return this
- */
- public Wrapper<T> orNew() {
- sql.OR_NEW();
- return this;
- }
- /**
- * <p>
- * 使用OR换行,并添加一个带()的新的条件
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
- * (name='zhangsan' AND id=11) OR (statu=1)
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param sqlOr AND 条件语句
- * @param params 参数值
- * @return this
- */
- public Wrapper<T> 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;
- }
- /**
- * <p>
- * 使用OR换行,并添加一个带()的新的条件
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
- * (name='zhangsan' AND id=11) OR (statu=1)
- * </p>
- *
- * @param sqlOr AND 条件语句
- * @param params 参数值
- * @return this
- */
- public Wrapper<T> orNew(String sqlOr, Object... params) {
- return orNew(true, sqlOr, params);
- }
- /**
- * <p>
- * SQL中groupBy关键字跟的条件语句
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").groupBy("id,name")
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param columns SQL 中的 Group by 语句,无需输入 Group By 关键字
- * @return this
- */
- public Wrapper<T> groupBy(boolean condition, String columns) {
- if (condition) {
- sql.GROUP_BY(columns);
- }
- return this;
- }
- /**
- * <p>
- * SQL中groupBy关键字跟的条件语句
- * </p>
- * <p>
- * eg: ew.where("name='zhangsan'").groupBy("id,name")
- * </p>
- *
- * @param columns SQL 中的 Group by 语句,无需输入 Group By 关键字
- * @return this
- */
- public Wrapper<T> groupBy(String columns) {
- return groupBy(true, columns);
- }
- /**
- * <p>
- * SQL中having关键字跟的条件语句
- * </p>
- * <p>
- * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null")
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param sqlHaving having关键字后面跟随的语句
- * @param params 参数集
- * @return EntityWrapper<T>
- */
- public Wrapper<T> having(boolean condition, String sqlHaving, Object... params) {
- if (condition) {
- sql.HAVING(formatSql(sqlHaving, params));
- }
- return this;
- }
- /**
- * <p>
- * SQL中having关键字跟的条件语句
- * </p>
- * <p>
- * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null")
- * </p>
- *
- * @param sqlHaving having关键字后面跟随的语句
- * @param params 参数集
- * @return EntityWrapper<T>
- */
- public Wrapper<T> having(String sqlHaving, Object... params) {
- return having(true, sqlHaving, params);
- }
- /**
- * <p>
- * SQL中orderby关键字跟的条件语句
- * </p>
- * <p>
- * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null"
- * ).orderBy("id,name")
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
- * @return this
- */
- public Wrapper<T> orderBy(boolean condition, String columns) {
- if (condition) {
- sql.ORDER_BY(columns);
- }
- return this;
- }
- /**
- * <p>
- * SQL中orderby关键字跟的条件语句
- * </p>
- * <p>
- * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null"
- * ).orderBy("id,name")
- * </p>
- *
- * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
- * @return this
- */
- public Wrapper<T> orderBy(String columns) {
- return orderBy(true, columns);
- }
- /**
- * <p>
- * SQL中orderby关键字跟的条件语句,可根据变更动态排序
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
- * @param isAsc 是否为升序
- * @return this
- */
- public Wrapper<T> orderBy(boolean condition, String columns, boolean isAsc) {
- if (condition && StringUtils.isNotEmpty(columns)) {
- sql.ORDER_BY(columns + (isAsc ? " ASC" : " DESC"));
- }
- return this;
- }
- /**
- * <p>
- * SQL中orderby关键字跟的条件语句,可根据变更动态排序
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
- * @param isAsc 是否为升序
- * @return this
- */
- public Wrapper<T> orderBy(boolean condition, Collection<String> columns, boolean isAsc) {
- if (condition && CollectionUtils.isNotEmpty(columns)) {
- for (String column : columns) {
- orderBy(condition, column, isAsc);
- }
- }
- return this;
- }
- /**
- * <p>
- * SQL中orderby关键字跟的条件语句,可根据变更动态排序
- * </p>
- *
- * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
- * @param isAsc 是否为升序
- * @return this
- */
- public Wrapper<T> orderBy(String columns, boolean isAsc) {
- return orderBy(true, columns, isAsc);
- }
- /**
- * <p>
- * 批量根据ASC排序
- * </p>
- *
- * @param columns 需要排序的集合
- * @return this
- */
- public Wrapper<T> orderAsc(Collection<String> columns) {
- return orderBy(true, columns, true);
- }
- /**
- * <p>
- * 批量根据DESC排序
- * </p>
- *
- * @param columns 需要排序的集合
- * @return this
- */
- public Wrapper<T> orderDesc(Collection<String> columns) {
- return orderBy(true, columns, false);
- }
- /**
- * <p>
- * LIKE条件语句,value中无需前后%
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 匹配值
- * @return this
- */
- public Wrapper<T> like(boolean condition, String column, String value) {
- if (condition) {
- handerLike(column, value, SqlLike.DEFAULT, false);
- }
- return this;
- }
- /**
- * <p>
- * LIKE条件语句,value中无需前后%
- * </p>
- *
- * @param column 字段名称
- * @param value 匹配值
- * @return this
- */
- public Wrapper<T> like(String column, String value) {
- return like(true, column, value);
- }
- /**
- * <p>
- * NOT LIKE条件语句,value中无需前后%
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 匹配值
- * @return this
- */
- public Wrapper<T> notLike(boolean condition, String column, String value) {
- if (condition) {
- handerLike(column, value, SqlLike.DEFAULT, true);
- }
- return this;
- }
- /**
- * <p>
- * NOT LIKE条件语句,value中无需前后%
- * </p>
- *
- * @param column 字段名称
- * @param value 匹配值
- * @return this
- */
- public Wrapper<T> notLike(String column, String value) {
- return notLike(true, column, value);
- }
- /**
- * <p>
- * 处理LIKE操作
- * </p>
- *
- * @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)));
- }
- }
- /**
- * <p>
- * LIKE条件语句,value中无需前后%
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 匹配值
- * @param type
- * @return this
- */
- public Wrapper<T> like(boolean condition, String column, String value, SqlLike type) {
- if (condition) {
- handerLike(column, value, type, false);
- }
- return this;
- }
- /**
- * <p>
- * LIKE条件语句,value中无需前后%
- * </p>
- *
- * @param column 字段名称
- * @param value 匹配值
- * @param type
- * @return this
- */
- public Wrapper<T> like(String column, String value, SqlLike type) {
- return like(true, column, value, type);
- }
- /**
- * <p>
- * NOT LIKE条件语句,value中无需前后%
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 匹配值
- * @param type
- * @return this
- */
- public Wrapper<T> notLike(boolean condition, String column, String value, SqlLike type) {
- if (condition) {
- handerLike(column, value, type, true);
- }
- return this;
- }
- /**
- * <p>
- * NOT LIKE条件语句,value中无需前后%
- * </p>
- *
- * @param column 字段名称
- * @param value 匹配值
- * @param type
- * @return this
- */
- public Wrapper<T> notLike(String column, String value, SqlLike type) {
- return notLike(true, column, value, type);
- }
- /**
- * <p>
- * is not null 条件
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param columns 字段名称。多个字段以逗号分隔。
- * @return this
- */
- public Wrapper<T> isNotNull(boolean condition, String columns) {
- if (condition) {
- sql.IS_NOT_NULL(columns);
- }
- return this;
- }
- /**
- * <p>
- * is not null 条件
- * </p>
- *
- * @param columns 字段名称。多个字段以逗号分隔。
- * @return this
- */
- public Wrapper<T> isNotNull(String columns) {
- return isNotNull(true, columns);
- }
- /**
- * <p>
- * is null 条件
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param columns 字段名称。多个字段以逗号分隔。
- * @return this
- */
- public Wrapper<T> isNull(boolean condition, String columns) {
- if (condition) {
- sql.IS_NULL(columns);
- }
- return this;
- }
- /**
- * <p>
- * is null 条件
- * </p>
- *
- * @param columns 字段名称。多个字段以逗号分隔。
- * @return this
- */
- public Wrapper<T> isNull(String columns) {
- return isNull(true, columns);
- }
- /**
- * <p>
- * EXISTS 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param value 匹配值
- * @return this
- */
- public Wrapper<T> exists(boolean condition, String value) {
- if (condition) {
- sql.EXISTS(value);
- }
- return this;
- }
- /**
- * <p>
- * EXISTS 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param value 匹配值
- * @return this
- */
- public Wrapper<T> exists(String value) {
- return exists(true, value);
- }
- /**
- * <p>
- * NOT EXISTS条件语句
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param value 匹配值
- * @return this
- */
- public Wrapper<T> notExists(boolean condition, String value) {
- if (condition) {
- sql.NOT_EXISTS(value);
- }
- return this;
- }
- /**
- * <p>
- * NOT EXISTS条件语句
- * </p>
- *
- * @param value 匹配值
- * @return this
- */
- public Wrapper<T> notExists(String value) {
- return notExists(true, value);
- }
- /**
- * <p>
- * IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 逗号拼接的字符串
- * @return this
- */
- public Wrapper<T> in(boolean condition, String column, String value) {
- if (condition && StringUtils.isNotEmpty(value)) {
- in(column, StringUtils.splitWorker(value, ",", -1, false));
- }
- return this;
- }
- /**
- * <p>
- * IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param column 字段名称
- * @param value 逗号拼接的字符串
- * @return this
- */
- public Wrapper<T> in(String column, String value) {
- return in(true, column, value);
- }
- /**
- * <p>
- * NOT IN条件语句
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 逗号拼接的字符串
- * @return this
- */
- public Wrapper<T> notIn(boolean condition, String column, String value) {
- if (condition && StringUtils.isNotEmpty(value)) {
- notIn(column, StringUtils.splitWorker(value, ",", -1, false));
- }
- return this;
- }
- /**
- * <p>
- * NOT IN条件语句
- * </p>
- *
- * @param column 字段名称
- * @param value 逗号拼接的字符串
- * @return this
- */
- public Wrapper<T> notIn(String column, String value) {
- return notIn(true, column, value);
- }
- /**
- * <p>
- * IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 匹配值 集合
- * @return this
- */
- public Wrapper<T> in(boolean condition, String column, Collection<?> value) {
- if (condition && CollectionUtils.isNotEmpty(value)) {
- sql.WHERE(formatSql(inExpression(column, value, false), value.toArray()));
- }
- return this;
- }
- /**
- * <p>
- * IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param column 字段名称
- * @param value 匹配值 集合
- * @return this
- */
- public Wrapper<T> in(String column, Collection<?> value) {
- return in(true, column, value);
- }
- /**
- * <p>
- * NOT IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 匹配值 集合
- * @return this
- */
- public Wrapper<T> notIn(boolean condition, String column, Collection<?> value) {
- if (condition && CollectionUtils.isNotEmpty(value)) {
- sql.WHERE(formatSql(inExpression(column, value, true), value.toArray()));
- }
- return this;
- }
- /**
- * <p>
- * NOT IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param column 字段名称
- * @param value 匹配值 集合
- * @return this
- */
- public Wrapper<T> notIn(String column, Collection<?> value) {
- return notIn(true, column, value);
- }
- /**
- * <p>
- * IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 匹配值 object数组
- * @return this
- */
- public Wrapper<T> in(boolean condition, String column, Object[] value) {
- if (condition && ArrayUtils.isNotEmpty(value)) {
- sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), false), value));
- }
- return this;
- }
- /**
- * <p>
- * IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param column 字段名称
- * @param value 匹配值 object数组
- * @return this
- */
- public Wrapper<T> in(String column, Object[] value) {
- return in(true, column, value);
- }
- /**
- * <p>
- * NOT IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param value 匹配值 object数组
- * @return this
- */
- public Wrapper<T> notIn(boolean condition, String column, Object... value) {
- if (condition && ArrayUtils.isNotEmpty(value)) {
- sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), true), value));
- }
- return this;
- }
- /**
- * <p>
- * NOT IN 条件语句,目前适配mysql及oracle
- * </p>
- *
- * @param column 字段名称
- * @param value 匹配值 object数组
- * @return this
- */
- public Wrapper<T> notIn(String column, Object... value) {
- return notIn(true, column, value);
- }
- /**
- * <p>
- * 获取in表达式
- * </p>
- *
- * @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;
- }
- /**
- * <p>
- * betwwee 条件语句
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param val1
- * @param val2
- * @return this
- */
- public Wrapper<T> 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;
- }
- /**
- * <p>
- * betwwee 条件语句
- * </p>
- *
- * @param column 字段名称
- * @param val1
- * @param val2
- * @return this
- */
- public Wrapper<T> between(String column, Object val1, Object val2) {
- return between(true, column, val1, val2);
- }
- /**
- * <p>
- * NOT betwwee 条件语句
- * </p>
- *
- * @param condition 拼接的前置条件
- * @param column 字段名称
- * @param val1
- * @param val2
- * @return this
- */
- public Wrapper<T> 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;
- }
- /**
- * <p>
- * NOT betwwee 条件语句
- * </p>
- *
- * @param column 字段名称
- * @param val1
- * @param val2
- * @return this
- */
- public Wrapper<T> notBetween(String column, Object val1, Object val2) {
- return notBetween(true, column, val1, val2);
- }
- /**
- * <p>
- * 为了兼容之前的版本,可使用where()或and()替代
- * </p>
- *
- * @param sqlWhere where sql部分
- * @param params 参数集
- * @return this
- */
- public Wrapper<T> addFilter(String sqlWhere, Object... params) {
- return and(sqlWhere, params);
- }
- /**
- * <p>
- * 根据判断条件来添加条件语句部分 使用 andIf() 替代
- * </p>
- * <p>
- * eg: ew.filterIfNeed(false,"name='zhangsan'").where("name='zhangsan'")
- * .filterIfNeed(true,"id={0}",22)
- * <p>
- * 输出: WHERE (name='zhangsan' AND id=22)
- * </p>
- *
- * @param need 是否需要添加该条件
- * @param sqlWhere 条件语句
- * @param params 参数集
- * @return this
- */
- public Wrapper<T> addFilterIfNeed(boolean need, String sqlWhere, Object... params) {
- return need ? where(sqlWhere, params) : this;
- }
- /**
- * <p>
- * 格式化SQL
- * </p>
- *
- * @param sqlStr SQL语句部分
- * @param params 参数集
- * @return this
- */
- protected String formatSql(String sqlStr, Object... params) {
- return formatSqlIfNeed(true, sqlStr, params);
- }
- /**
- * <p>
- * 根据需要格式化SQL<BR>
- * <BR>
- * Format SQL for methods: EntityWrapper<T>.where/and/or...("name={0}", value);
- * ALL the {<b>i</b>} will be replaced with #{MPGENVAL<b>i</b>}<BR>
- * <BR>
- * ew.where("sample_name=<b>{0}</b>", "haha").and("sample_age ><b>{0}</b>
- * and sample_age<<b>{1}</b>", 18, 30) <b>TO</b>
- * sample_name=<b>#{MPGENVAL1}</b> and sample_age>#<b>{MPGENVAL2}</b> and
- * sample_age<<b>#{MPGENVAL3}</b><BR>
- * </p>
- *
- * @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;
- }
- /**
- * <p>
- * 自定义是否输出sql开头为 `WHERE` OR `AND` OR `OR`
- * </p>
- *
- * @param bool
- * @return this
- */
- public Wrapper<T> isWhere(Boolean bool) {
- this.isWhere = bool;
- return this;
- }
- /**
- * <p>
- * 手动把sql拼接到最后(有sql注入的风险,请谨慎使用)
- * </p>
- *
- * @param limit
- * @return this
- */
- public Wrapper<T> last(String limit) {
- sql.LAST(limit);
- return this;
- }
- /**
- * Fix issue 200.
- *
- * @return
- * @since 2.0.3
- */
- public Map<String, Object> getParamNameValuePairs() {
- return paramNameValuePairs;
- }
- public String getParamAlias() {
- return StringUtils.isEmpty(paramAlias) ? DEFAULT_PARAM_ALIAS : paramAlias;
- }
- /**
- * <p>
- * 参数别名设置,初始化时优先设置该值、重复设置异常
- * </p>
- *
- * @param paramAlias 参数别名
- * @return
- */
- public Wrapper<T> 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;
- }
- /**
- * <p>
- * 克隆本身 fixed github issues/241
- * </p>
- */
- @Override
- public Wrapper<T> clone() {
- return SerializationUtils.clone(this);
- }
- @Override
- public String getSqlSegment() {
- // 子类重载
- return null;
- }
- }
|