Wrapper.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. /*
  2. * Copyright (c) 2011-2014, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.mapper;
  17. import java.util.Arrays;
  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.concurrent.atomic.AtomicInteger;
  23. import com.baomidou.mybatisplus.core.conditions.SqlPlus;
  24. import com.baomidou.mybatisplus.core.enums.SqlLike;
  25. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  26. import com.baomidou.mybatisplus.core.metadata.Column;
  27. import com.baomidou.mybatisplus.core.metadata.Columns;
  28. import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
  29. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  30. import com.baomidou.mybatisplus.core.toolkit.MapUtils;
  31. import com.baomidou.mybatisplus.core.toolkit.SerializationUtils;
  32. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  33. import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
  34. /**
  35. * <p>
  36. * 条件构造抽象类,定义T-SQL语法
  37. * </p>
  38. *
  39. * @author hubin , yanghu , Dyang , Caratacus, hcl
  40. * @Date 2016-11-7
  41. */
  42. @Deprecated
  43. public class Wrapper<T> extends com.baomidou.mybatisplus.core.conditions.Wrapper<T> {
  44. /**
  45. * 占位符
  46. */
  47. private static final String PLACE_HOLDER = "{%s}";
  48. private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}";
  49. private static final String MP_GENERAL_PARAMNAME = "MPGENVAL";
  50. private static final String DEFAULT_PARAM_ALIAS = "ew";
  51. /**
  52. * 实现了TSQL语法的SQL实体
  53. */
  54. protected final SqlPlus sql = new SqlPlus();
  55. private final Map<String, Object> paramNameValuePairs = new HashMap<>();
  56. private final AtomicInteger paramNameSeq = new AtomicInteger(0);
  57. protected String paramAlias = null;
  58. /**
  59. * SQL 查询字段内容,例如:id,name,age
  60. */
  61. protected String sqlSelect = null;
  62. /**
  63. * 自定义是否输出sql为 WHERE OR AND OR OR
  64. */
  65. protected Boolean isWhere;
  66. /**
  67. * 拼接WHERE后应该是AND还是ORnull
  68. */
  69. protected String AND_OR = "AND";
  70. public static Wrapper getInstance() {
  71. return new Wrapper();
  72. }
  73. /**
  74. * <p>
  75. * 兼容EntityWrapper
  76. * </p>
  77. *
  78. * @return
  79. */
  80. @Override
  81. public T getEntity() {
  82. return null;
  83. }
  84. /**
  85. * 查看where构造是否为空
  86. *
  87. * @return
  88. */
  89. public boolean isEmptyOfWhere() {
  90. return sql.isEmptyOfWhere();
  91. }
  92. /**
  93. * 查看where构造是否不为空
  94. *
  95. * @return
  96. */
  97. public boolean isNotEmptyOfWhere() {
  98. return !isEmptyOfWhere();
  99. }
  100. @Override
  101. public String getSqlSelect() {
  102. return StringUtils.isEmpty(sqlSelect) ? null : SqlUtils.stripSqlInjection(sqlSelect);
  103. }
  104. public Wrapper<T> setSqlSelect(String sqlSelect) {
  105. if (StringUtils.isNotEmpty(sqlSelect)) {
  106. this.sqlSelect = sqlSelect;
  107. }
  108. return this;
  109. }
  110. /**
  111. * <p>
  112. * 使用字符串数组封装sqlSelect,便于在不需要指定 AS 的情况下通过实体类自动生成的列静态字段快速组装 sqlSelect,<br/>
  113. * 减少手动录入的错误率
  114. * </p>
  115. *
  116. * @param columns 字段
  117. * @return
  118. */
  119. public Wrapper<T> setSqlSelect(String... columns) {
  120. StringBuilder builder = new StringBuilder();
  121. for (String column : columns) {
  122. if (StringUtils.isNotEmpty(column)) {
  123. if (builder.length() > 0) {
  124. builder.append(",");
  125. }
  126. builder.append(column);
  127. }
  128. }
  129. this.sqlSelect = builder.toString();
  130. return this;
  131. }
  132. /**
  133. * <p>
  134. * 使用对象封装的setsqlselect
  135. * </p>
  136. *
  137. * @param column 字段
  138. * @return
  139. */
  140. public Wrapper<T> setSqlSelect(Column... column) {
  141. if (ArrayUtils.isNotEmpty(column)) {
  142. StringBuilder builder = new StringBuilder();
  143. for (int i = 0; i < column.length; i++) {
  144. if (column[i] != null) {
  145. String col = column[i].getColumn();
  146. String as = column[i].getAs();
  147. if (StringUtils.isEmpty(col)) {
  148. continue;
  149. }
  150. builder.append(col).append(as);
  151. if (i < column.length - 1) {
  152. builder.append(",");
  153. }
  154. }
  155. }
  156. this.sqlSelect = builder.toString();
  157. }
  158. return this;
  159. }
  160. /**
  161. * <p>
  162. * 使用对象封装的setsqlselect
  163. * </p>
  164. *
  165. * @param columns 字段
  166. * @return
  167. */
  168. public Wrapper<T> setSqlSelect(Columns columns) {
  169. Column[] columnArray = columns.getColumns();
  170. if (ArrayUtils.isNotEmpty(columnArray)) {
  171. setSqlSelect(columnArray);
  172. }
  173. return this;
  174. }
  175. @Override
  176. public String toString() {
  177. StringBuilder sb = new StringBuilder("Wrapper<T>:");
  178. String sqlSegment = getSqlSegment();
  179. sb.append(replacePlaceholder(sqlSegment));
  180. Object entity = getEntity();
  181. if (entity != null) {
  182. sb.append("\n");
  183. sb.append("entity=").append(entity.toString());
  184. }
  185. return sb.toString();
  186. }
  187. /**
  188. * <p>
  189. * 替换占位符
  190. * </p>
  191. *
  192. * @param sqlSegment
  193. * @return
  194. */
  195. private String replacePlaceholder(String sqlSegment) {
  196. if (StringUtils.isEmpty(sqlSegment)) {
  197. return StringUtils.EMPTY;
  198. }
  199. return sqlSegment.replaceAll("#\\{" + getParamAlias() + ".paramNameValuePairs.MPGENVAL[0-9]+}", "\\?");
  200. }
  201. /**
  202. * <p>
  203. * 原生占位符sql
  204. * </p>
  205. *
  206. * @return
  207. */
  208. public String originalSql() {
  209. return replacePlaceholder(getSqlSegment());
  210. }
  211. /**
  212. * <p>
  213. * SQL中WHERE关键字跟的条件语句
  214. * </p>
  215. * <p>
  216. * eg: ew.where("name='zhangsan'").where(id!=null, "id={0}", id);
  217. * <p>
  218. * 输出:<br>
  219. * 如果id=123: WHERE (NAME='zhangsan' AND id=123)<br>
  220. * 如果id=null: WHERE (NAME='zhangsan')
  221. * </p>
  222. *
  223. * @param condition 拼接的前置条件
  224. * @param sqlWhere where语句
  225. * @param params 参数集
  226. * @return this
  227. */
  228. public Wrapper<T> where(boolean condition, String sqlWhere, Object... params) {
  229. if (condition) {
  230. sql.WHERE(formatSql(sqlWhere, params));
  231. }
  232. return this;
  233. }
  234. /**
  235. * <p>
  236. * SQL中WHERE关键字跟的条件语句
  237. * </p>
  238. * <p>
  239. * eg: ew.where("name='zhangsan'").where("id={0}","123");
  240. * <p>
  241. * 输出: WHERE (NAME='zhangsan' AND id=123)
  242. * </p>
  243. *
  244. * @param sqlWhere where语句
  245. * @param params 参数集
  246. * @return this
  247. */
  248. public Wrapper<T> where(String sqlWhere, Object... params) {
  249. return where(true, sqlWhere, params);
  250. }
  251. /**
  252. * <p>
  253. * 等同于SQL的"field=value"表达式
  254. * </p>
  255. *
  256. * @param condition 拼接的前置条件
  257. * @param column
  258. * @param params
  259. * @return
  260. */
  261. public Wrapper<T> eq(boolean condition, String column, Object params) {
  262. if (condition) {
  263. sql.WHERE(formatSql(String.format("%s = {0}", column), params));
  264. }
  265. return this;
  266. }
  267. /**
  268. * <p>
  269. * 等同于SQL的"field=value"表达式
  270. * </p>
  271. *
  272. * @param column
  273. * @param params
  274. * @return
  275. */
  276. public Wrapper<T> eq(String column, Object params) {
  277. return eq(true, column, params);
  278. }
  279. /**
  280. * <p>
  281. * 等同于SQL的"field <> value"表达式
  282. * </p>
  283. *
  284. * @param condition 拼接的前置条件
  285. * @param column
  286. * @param params
  287. * @return
  288. */
  289. public Wrapper<T> ne(boolean condition, String column, Object params) {
  290. if (condition) {
  291. sql.WHERE(formatSql(String.format("%s <> {0}", column), params));
  292. }
  293. return this;
  294. }
  295. /**
  296. * <p>
  297. * 等同于SQL的"field <> value"表达式
  298. * </p>
  299. *
  300. * @param column
  301. * @param params
  302. * @return
  303. */
  304. public Wrapper<T> ne(String column, Object params) {
  305. return ne(true, column, params);
  306. }
  307. /**
  308. * <p>
  309. * 等同于SQL的"field=value"表达式
  310. * </p>
  311. *
  312. * @param condition 拼接的前置条件
  313. * @param params
  314. * @return
  315. */
  316. @SuppressWarnings({"rawtypes", "unchecked"})
  317. public Wrapper<T> allEq(boolean condition, Map<String, Object> params) {
  318. if (condition && MapUtils.isNotEmpty(params)) {
  319. Iterator iterator = params.entrySet().iterator();
  320. while (iterator.hasNext()) {
  321. Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();
  322. Object value = entry.getValue();
  323. if (StringUtils.checkValNotNull(value)) {
  324. sql.WHERE(formatSql(String.format("%s = {0}", entry.getKey()), entry.getValue()));
  325. }
  326. }
  327. }
  328. return this;
  329. }
  330. /**
  331. * <p>
  332. * 等同于SQL的"field=value"表达式
  333. * </p>
  334. *
  335. * @param params
  336. * @return
  337. */
  338. @SuppressWarnings({"rawtypes", "unchecked"})
  339. public Wrapper<T> allEq(Map<String, Object> params) {
  340. return allEq(true, params);
  341. }
  342. /**
  343. * <p>
  344. * 等同于SQL的"field>value"表达式
  345. * </p>
  346. *
  347. * @param condition 拼接的前置条件
  348. * @param column
  349. * @param params
  350. * @return
  351. */
  352. public Wrapper<T> gt(boolean condition, String column, Object params) {
  353. if (condition) {
  354. sql.WHERE(formatSql(String.format("%s > {0}", column), params));
  355. }
  356. return this;
  357. }
  358. /**
  359. * <p>
  360. * 等同于SQL的"field>value"表达式
  361. * </p>
  362. *
  363. * @param column
  364. * @param params
  365. * @return
  366. */
  367. public Wrapper<T> gt(String column, Object params) {
  368. return gt(true, column, params);
  369. }
  370. /**
  371. * <p>
  372. * 等同于SQL的"field>=value"表达式
  373. * </p>
  374. *
  375. * @param condition 拼接的前置条件
  376. * @param column
  377. * @param params
  378. * @return
  379. */
  380. public Wrapper<T> ge(boolean condition, String column, Object params) {
  381. if (condition) {
  382. sql.WHERE(formatSql(String.format("%s >= {0}", column), params));
  383. }
  384. return this;
  385. }
  386. /**
  387. * <p>
  388. * 等同于SQL的"field>=value"表达式
  389. * </p>
  390. *
  391. * @param column
  392. * @param params
  393. * @return
  394. */
  395. public Wrapper<T> ge(String column, Object params) {
  396. return ge(true, column, params);
  397. }
  398. /**
  399. * <p>
  400. * 等同于SQL的"field<value"表达式
  401. * </p>
  402. *
  403. * @param condition 拼接的前置条件
  404. * @param column
  405. * @param params
  406. * @return
  407. */
  408. public Wrapper<T> lt(boolean condition, String column, Object params) {
  409. if (condition) {
  410. sql.WHERE(formatSql(String.format("%s < {0}", column), params));
  411. }
  412. return this;
  413. }
  414. /**
  415. * <p>
  416. * 等同于SQL的"field<value"表达式
  417. * </p>
  418. *
  419. * @param column
  420. * @param params
  421. * @return
  422. */
  423. public Wrapper<T> lt(String column, Object params) {
  424. return lt(true, column, params);
  425. }
  426. /**
  427. * <p>
  428. * 等同于SQL的"field<=value"表达式
  429. * </p>
  430. *
  431. * @param condition 拼接的前置条件
  432. * @param column
  433. * @param params
  434. * @return
  435. */
  436. public Wrapper<T> le(boolean condition, String column, Object params) {
  437. if (condition) {
  438. sql.WHERE(formatSql(String.format("%s <= {0}", column), params));
  439. }
  440. return this;
  441. }
  442. /**
  443. * <p>
  444. * 等同于SQL的"field<=value"表达式
  445. * </p>
  446. *
  447. * @param column
  448. * @param params
  449. * @return
  450. */
  451. public Wrapper<T> le(String column, Object params) {
  452. return le(true, column, params);
  453. }
  454. /**
  455. * <p>
  456. * AND 连接后续条件
  457. * </p>
  458. *
  459. * @param condition 拼接的前置条件
  460. * @param sqlAnd and条件语句
  461. * @param params 参数集
  462. * @return this
  463. */
  464. public Wrapper<T> and(boolean condition, String sqlAnd, Object... params) {
  465. if (condition) {
  466. sql.AND().WHERE(formatSql(sqlAnd, params));
  467. }
  468. return this;
  469. }
  470. /**
  471. * <p>
  472. * AND 连接后续条件
  473. * </p>
  474. *
  475. * @param sqlAnd and条件语句
  476. * @param params 参数集
  477. * @return this
  478. */
  479. public Wrapper<T> and(String sqlAnd, Object... params) {
  480. return and(true, sqlAnd, params);
  481. }
  482. /**
  483. * <p>
  484. * 使用AND连接并换行
  485. * </p>
  486. * <p>
  487. * eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
  488. * (name='zhangsan' AND id=11) AND (statu=1)
  489. * </p>
  490. *
  491. * @param condition 拼接的前置条件
  492. * @param sqlAnd AND 条件语句
  493. * @param params 参数值
  494. * @return this
  495. */
  496. public Wrapper<T> andNew(boolean condition, String sqlAnd, Object... params) {
  497. if (condition) {
  498. sql.AND_NEW().WHERE(formatSql(sqlAnd, params));
  499. }
  500. return this;
  501. }
  502. /**
  503. * <p>
  504. * 使用AND连接并换行
  505. * </p>
  506. * <p>
  507. * eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
  508. * (name='zhangsan' AND id=11) AND (statu=1)
  509. * </p>
  510. *
  511. * @return this
  512. */
  513. public Wrapper<T> andNew() {
  514. sql.AND_NEW();
  515. return this;
  516. }
  517. /**
  518. * <p>
  519. * 使用AND连接并换行
  520. * </p>
  521. * <p>
  522. * eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE
  523. * (name='zhangsan' AND id=11) AND (statu=1)
  524. * </p>
  525. *
  526. * @param sqlAnd AND 条件语句
  527. * @param params 参数值
  528. * @return this
  529. */
  530. public Wrapper<T> andNew(String sqlAnd, Object... params) {
  531. return andNew(true, sqlAnd, params);
  532. }
  533. /**
  534. * <p>
  535. * 使用AND连接并换行
  536. * </p>
  537. *
  538. * @return this
  539. */
  540. public Wrapper<T> and() {
  541. sql.AND();
  542. return this;
  543. }
  544. /**
  545. * <p>
  546. * 左嵌套方法
  547. * </p>
  548. *
  549. * @return this
  550. */
  551. public Wrapper<T> leftNest() {
  552. leftNest(1);
  553. return this;
  554. }
  555. /**
  556. * <p>
  557. * 右嵌套方法
  558. * </p>
  559. *
  560. * @return this
  561. */
  562. public Wrapper<T> rightNest() {
  563. rightNest(1);
  564. return this;
  565. }
  566. /**
  567. * <p>
  568. * 左嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
  569. * </p>
  570. *
  571. * @param num 嵌套数量
  572. * @return this
  573. */
  574. public Wrapper<T> leftNest(int num) {
  575. nest("(", num);
  576. return this;
  577. }
  578. /**
  579. * <p>
  580. * 右嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
  581. * </p>
  582. *
  583. * @param num 嵌套数量
  584. * @return this
  585. */
  586. public Wrapper<T> rightNest(int num) {
  587. nest(")", num);
  588. return this;
  589. }
  590. /**
  591. * <p>
  592. * 嵌套方法,一般认为填补mp一个条件中多重嵌套解决方案
  593. * </p>
  594. *
  595. * @param nest 嵌套字符串 "(" or ")"
  596. * @param num 嵌套数量
  597. * @return this
  598. */
  599. private Wrapper<T> nest(String nest, int num) {
  600. if (num >= 1) {
  601. StringBuilder builder = new StringBuilder(num);
  602. for (int i = 0; i < num; i++) {
  603. builder.append(nest);
  604. }
  605. sql.SUPP(builder.toString());
  606. }
  607. return this;
  608. }
  609. /**
  610. * <p>
  611. * 使用OR连接并换行
  612. * </p>
  613. *
  614. * @return this
  615. */
  616. public Wrapper<T> or() {
  617. sql.OR();
  618. return this;
  619. }
  620. /**
  621. * <p>
  622. * 添加OR条件
  623. * </p>
  624. *
  625. * @param condition 拼接的前置条件
  626. * @param sqlOr or 条件语句
  627. * @param params 参数集
  628. * @return this
  629. */
  630. public Wrapper<T> or(boolean condition, String sqlOr, Object... params) {
  631. if (condition) {
  632. if (StringUtils.isEmpty(sql.toString())) {
  633. AND_OR = "OR";
  634. }
  635. sql.OR().WHERE(formatSql(sqlOr, params));
  636. }
  637. return this;
  638. }
  639. /**
  640. * <p>
  641. * 添加OR条件
  642. * </p>
  643. *
  644. * @param sqlOr or 条件语句
  645. * @param params 参数集
  646. * @return this
  647. */
  648. public Wrapper<T> or(String sqlOr, Object... params) {
  649. return or(true, sqlOr, params);
  650. }
  651. /**
  652. * <p>
  653. * 使用OR换行,并添加一个带()的新的条件
  654. * </p>
  655. * <p>
  656. * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
  657. * (name='zhangsan' AND id=11) OR (statu=1)
  658. * </p>
  659. *
  660. * @return this
  661. */
  662. public Wrapper<T> orNew() {
  663. sql.OR_NEW();
  664. return this;
  665. }
  666. /**
  667. * <p>
  668. * 使用OR换行,并添加一个带()的新的条件
  669. * </p>
  670. * <p>
  671. * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
  672. * (name='zhangsan' AND id=11) OR (statu=1)
  673. * </p>
  674. *
  675. * @param condition 拼接的前置条件
  676. * @param sqlOr AND 条件语句
  677. * @param params 参数值
  678. * @return this
  679. */
  680. public Wrapper<T> orNew(boolean condition, String sqlOr, Object... params) {
  681. if (condition) {
  682. if (StringUtils.isEmpty(sql.toString())) {
  683. AND_OR = "OR";
  684. }
  685. sql.OR_NEW().WHERE(formatSql(sqlOr, params));
  686. }
  687. return this;
  688. }
  689. /**
  690. * <p>
  691. * 使用OR换行,并添加一个带()的新的条件
  692. * </p>
  693. * <p>
  694. * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
  695. * (name='zhangsan' AND id=11) OR (statu=1)
  696. * </p>
  697. *
  698. * @param sqlOr AND 条件语句
  699. * @param params 参数值
  700. * @return this
  701. */
  702. public Wrapper<T> orNew(String sqlOr, Object... params) {
  703. return orNew(true, sqlOr, params);
  704. }
  705. /**
  706. * <p>
  707. * SQL中groupBy关键字跟的条件语句
  708. * </p>
  709. * <p>
  710. * eg: ew.where("name='zhangsan'").groupBy("id,name")
  711. * </p>
  712. *
  713. * @param condition 拼接的前置条件
  714. * @param columns SQL 中的 Group by 语句,无需输入 Group By 关键字
  715. * @return this
  716. */
  717. public Wrapper<T> groupBy(boolean condition, String columns) {
  718. if (condition) {
  719. sql.GROUP_BY(columns);
  720. }
  721. return this;
  722. }
  723. /**
  724. * <p>
  725. * SQL中groupBy关键字跟的条件语句
  726. * </p>
  727. * <p>
  728. * eg: ew.where("name='zhangsan'").groupBy("id,name")
  729. * </p>
  730. *
  731. * @param columns SQL 中的 Group by 语句,无需输入 Group By 关键字
  732. * @return this
  733. */
  734. public Wrapper<T> groupBy(String columns) {
  735. return groupBy(true, columns);
  736. }
  737. /**
  738. * <p>
  739. * SQL中having关键字跟的条件语句
  740. * </p>
  741. * <p>
  742. * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null")
  743. * </p>
  744. *
  745. * @param condition 拼接的前置条件
  746. * @param sqlHaving having关键字后面跟随的语句
  747. * @param params 参数集
  748. * @return EntityWrapper<T>
  749. */
  750. public Wrapper<T> having(boolean condition, String sqlHaving, Object... params) {
  751. if (condition) {
  752. sql.HAVING(formatSql(sqlHaving, params));
  753. }
  754. return this;
  755. }
  756. /**
  757. * <p>
  758. * SQL中having关键字跟的条件语句
  759. * </p>
  760. * <p>
  761. * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null")
  762. * </p>
  763. *
  764. * @param sqlHaving having关键字后面跟随的语句
  765. * @param params 参数集
  766. * @return EntityWrapper<T>
  767. */
  768. public Wrapper<T> having(String sqlHaving, Object... params) {
  769. return having(true, sqlHaving, params);
  770. }
  771. /**
  772. * <p>
  773. * SQL中orderby关键字跟的条件语句
  774. * </p>
  775. * <p>
  776. * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null"
  777. * ).orderBy("id,name")
  778. * </p>
  779. *
  780. * @param condition 拼接的前置条件
  781. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  782. * @return this
  783. */
  784. public Wrapper<T> orderBy(boolean condition, String columns) {
  785. if (condition) {
  786. sql.ORDER_BY(columns);
  787. }
  788. return this;
  789. }
  790. /**
  791. * <p>
  792. * SQL中orderby关键字跟的条件语句
  793. * </p>
  794. * <p>
  795. * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null"
  796. * ).orderBy("id,name")
  797. * </p>
  798. *
  799. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  800. * @return this
  801. */
  802. public Wrapper<T> orderBy(String columns) {
  803. return orderBy(true, columns);
  804. }
  805. /**
  806. * <p>
  807. * SQL中orderby关键字跟的条件语句,可根据变更动态排序
  808. * </p>
  809. *
  810. * @param condition 拼接的前置条件
  811. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  812. * @param isAsc 是否为升序
  813. * @return this
  814. */
  815. public Wrapper<T> orderBy(boolean condition, String columns, boolean isAsc) {
  816. if (condition && StringUtils.isNotEmpty(columns)) {
  817. sql.ORDER_BY(columns + (isAsc ? " ASC" : " DESC"));
  818. }
  819. return this;
  820. }
  821. /**
  822. * <p>
  823. * SQL中orderby关键字跟的条件语句,可根据变更动态排序
  824. * </p>
  825. *
  826. * @param condition 拼接的前置条件
  827. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  828. * @param isAsc 是否为升序
  829. * @return this
  830. */
  831. public Wrapper<T> orderBy(boolean condition, Collection<String> columns, boolean isAsc) {
  832. if (condition && CollectionUtils.isNotEmpty(columns)) {
  833. for (String column : columns) {
  834. orderBy(condition, column, isAsc);
  835. }
  836. }
  837. return this;
  838. }
  839. /**
  840. * <p>
  841. * SQL中orderby关键字跟的条件语句,可根据变更动态排序
  842. * </p>
  843. *
  844. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  845. * @param isAsc 是否为升序
  846. * @return this
  847. */
  848. public Wrapper<T> orderBy(String columns, boolean isAsc) {
  849. return orderBy(true, columns, isAsc);
  850. }
  851. /**
  852. * <p>
  853. * 批量根据ASC排序
  854. * </p>
  855. *
  856. * @param columns 需要排序的集合
  857. * @return this
  858. */
  859. public Wrapper<T> orderAsc(Collection<String> columns) {
  860. return orderBy(true, columns, true);
  861. }
  862. /**
  863. * <p>
  864. * 批量根据DESC排序
  865. * </p>
  866. *
  867. * @param columns 需要排序的集合
  868. * @return this
  869. */
  870. public Wrapper<T> orderDesc(Collection<String> columns) {
  871. return orderBy(true, columns, false);
  872. }
  873. /**
  874. * <p>
  875. * LIKE条件语句,value中无需前后%
  876. * </p>
  877. *
  878. * @param condition 拼接的前置条件
  879. * @param column 字段名称
  880. * @param value 匹配值
  881. * @return this
  882. */
  883. public Wrapper<T> like(boolean condition, String column, String value) {
  884. if (condition) {
  885. handerLike(column, value, SqlLike.DEFAULT, false);
  886. }
  887. return this;
  888. }
  889. /**
  890. * <p>
  891. * LIKE条件语句,value中无需前后%
  892. * </p>
  893. *
  894. * @param column 字段名称
  895. * @param value 匹配值
  896. * @return this
  897. */
  898. public Wrapper<T> like(String column, String value) {
  899. return like(true, column, value);
  900. }
  901. /**
  902. * <p>
  903. * NOT LIKE条件语句,value中无需前后%
  904. * </p>
  905. *
  906. * @param condition 拼接的前置条件
  907. * @param column 字段名称
  908. * @param value 匹配值
  909. * @return this
  910. */
  911. public Wrapper<T> notLike(boolean condition, String column, String value) {
  912. if (condition) {
  913. handerLike(column, value, SqlLike.DEFAULT, true);
  914. }
  915. return this;
  916. }
  917. /**
  918. * <p>
  919. * NOT LIKE条件语句,value中无需前后%
  920. * </p>
  921. *
  922. * @param column 字段名称
  923. * @param value 匹配值
  924. * @return this
  925. */
  926. public Wrapper<T> notLike(String column, String value) {
  927. return notLike(true, column, value);
  928. }
  929. /**
  930. * <p>
  931. * 处理LIKE操作
  932. * </p>
  933. *
  934. * @param column 字段名称
  935. * @param value like匹配值
  936. * @param isNot 是否为NOT LIKE操作
  937. */
  938. private void handerLike(String column, String value, SqlLike type, boolean isNot) {
  939. if (StringUtils.isNotEmpty(column) && StringUtils.isNotEmpty(value)) {
  940. StringBuilder inSql = new StringBuilder();
  941. inSql.append(column);
  942. if (isNot) {
  943. inSql.append(" NOT");
  944. }
  945. inSql.append(" LIKE {0}");
  946. sql.WHERE(formatSql(inSql.toString(), SqlUtils.concatLike(value, type)));
  947. }
  948. }
  949. /**
  950. * <p>
  951. * LIKE条件语句,value中无需前后%
  952. * </p>
  953. *
  954. * @param condition 拼接的前置条件
  955. * @param column 字段名称
  956. * @param value 匹配值
  957. * @param type
  958. * @return this
  959. */
  960. public Wrapper<T> like(boolean condition, String column, String value, SqlLike type) {
  961. if (condition) {
  962. handerLike(column, value, type, false);
  963. }
  964. return this;
  965. }
  966. /**
  967. * <p>
  968. * LIKE条件语句,value中无需前后%
  969. * </p>
  970. *
  971. * @param column 字段名称
  972. * @param value 匹配值
  973. * @param type
  974. * @return this
  975. */
  976. public Wrapper<T> like(String column, String value, SqlLike type) {
  977. return like(true, column, value, type);
  978. }
  979. /**
  980. * <p>
  981. * NOT LIKE条件语句,value中无需前后%
  982. * </p>
  983. *
  984. * @param condition 拼接的前置条件
  985. * @param column 字段名称
  986. * @param value 匹配值
  987. * @param type
  988. * @return this
  989. */
  990. public Wrapper<T> notLike(boolean condition, String column, String value, SqlLike type) {
  991. if (condition) {
  992. handerLike(column, value, type, true);
  993. }
  994. return this;
  995. }
  996. /**
  997. * <p>
  998. * NOT LIKE条件语句,value中无需前后%
  999. * </p>
  1000. *
  1001. * @param column 字段名称
  1002. * @param value 匹配值
  1003. * @param type
  1004. * @return this
  1005. */
  1006. public Wrapper<T> notLike(String column, String value, SqlLike type) {
  1007. return notLike(true, column, value, type);
  1008. }
  1009. /**
  1010. * <p>
  1011. * is not null 条件
  1012. * </p>
  1013. *
  1014. * @param condition 拼接的前置条件
  1015. * @param columns 字段名称。多个字段以逗号分隔。
  1016. * @return this
  1017. */
  1018. public Wrapper<T> isNotNull(boolean condition, String columns) {
  1019. if (condition) {
  1020. sql.IS_NOT_NULL(columns);
  1021. }
  1022. return this;
  1023. }
  1024. /**
  1025. * <p>
  1026. * is not null 条件
  1027. * </p>
  1028. *
  1029. * @param columns 字段名称。多个字段以逗号分隔。
  1030. * @return this
  1031. */
  1032. public Wrapper<T> isNotNull(String columns) {
  1033. return isNotNull(true, columns);
  1034. }
  1035. /**
  1036. * <p>
  1037. * is null 条件
  1038. * </p>
  1039. *
  1040. * @param condition 拼接的前置条件
  1041. * @param columns 字段名称。多个字段以逗号分隔。
  1042. * @return this
  1043. */
  1044. public Wrapper<T> isNull(boolean condition, String columns) {
  1045. if (condition) {
  1046. sql.IS_NULL(columns);
  1047. }
  1048. return this;
  1049. }
  1050. /**
  1051. * <p>
  1052. * is null 条件
  1053. * </p>
  1054. *
  1055. * @param columns 字段名称。多个字段以逗号分隔。
  1056. * @return this
  1057. */
  1058. public Wrapper<T> isNull(String columns) {
  1059. return isNull(true, columns);
  1060. }
  1061. /**
  1062. * <p>
  1063. * EXISTS 条件语句,目前适配mysql及oracle
  1064. * </p>
  1065. *
  1066. * @param condition 拼接的前置条件
  1067. * @param value 匹配值
  1068. * @return this
  1069. */
  1070. public Wrapper<T> exists(boolean condition, String value) {
  1071. if (condition) {
  1072. sql.EXISTS(value);
  1073. }
  1074. return this;
  1075. }
  1076. /**
  1077. * <p>
  1078. * EXISTS 条件语句,目前适配mysql及oracle
  1079. * </p>
  1080. *
  1081. * @param value 匹配值
  1082. * @return this
  1083. */
  1084. public Wrapper<T> exists(String value) {
  1085. return exists(true, value);
  1086. }
  1087. /**
  1088. * <p>
  1089. * NOT EXISTS条件语句
  1090. * </p>
  1091. *
  1092. * @param condition 拼接的前置条件
  1093. * @param value 匹配值
  1094. * @return this
  1095. */
  1096. public Wrapper<T> notExists(boolean condition, String value) {
  1097. if (condition) {
  1098. sql.NOT_EXISTS(value);
  1099. }
  1100. return this;
  1101. }
  1102. /**
  1103. * <p>
  1104. * NOT EXISTS条件语句
  1105. * </p>
  1106. *
  1107. * @param value 匹配值
  1108. * @return this
  1109. */
  1110. public Wrapper<T> notExists(String value) {
  1111. return notExists(true, value);
  1112. }
  1113. /**
  1114. * <p>
  1115. * IN 条件语句,目前适配mysql及oracle
  1116. * </p>
  1117. *
  1118. * @param condition 拼接的前置条件
  1119. * @param column 字段名称
  1120. * @param value 逗号拼接的字符串
  1121. * @return this
  1122. */
  1123. public Wrapper<T> in(boolean condition, String column, String value) {
  1124. if (condition && StringUtils.isNotEmpty(value)) {
  1125. in(column, StringUtils.splitWorker(value, ",", -1, false));
  1126. }
  1127. return this;
  1128. }
  1129. /**
  1130. * <p>
  1131. * IN 条件语句,目前适配mysql及oracle
  1132. * </p>
  1133. *
  1134. * @param column 字段名称
  1135. * @param value 逗号拼接的字符串
  1136. * @return this
  1137. */
  1138. public Wrapper<T> in(String column, String value) {
  1139. return in(true, column, value);
  1140. }
  1141. /**
  1142. * <p>
  1143. * NOT IN条件语句
  1144. * </p>
  1145. *
  1146. * @param condition 拼接的前置条件
  1147. * @param column 字段名称
  1148. * @param value 逗号拼接的字符串
  1149. * @return this
  1150. */
  1151. public Wrapper<T> notIn(boolean condition, String column, String value) {
  1152. if (condition && StringUtils.isNotEmpty(value)) {
  1153. notIn(column, StringUtils.splitWorker(value, ",", -1, false));
  1154. }
  1155. return this;
  1156. }
  1157. /**
  1158. * <p>
  1159. * NOT IN条件语句
  1160. * </p>
  1161. *
  1162. * @param column 字段名称
  1163. * @param value 逗号拼接的字符串
  1164. * @return this
  1165. */
  1166. public Wrapper<T> notIn(String column, String value) {
  1167. return notIn(true, column, value);
  1168. }
  1169. /**
  1170. * <p>
  1171. * IN 条件语句,目前适配mysql及oracle
  1172. * </p>
  1173. *
  1174. * @param condition 拼接的前置条件
  1175. * @param column 字段名称
  1176. * @param value 匹配值 集合
  1177. * @return this
  1178. */
  1179. public Wrapper<T> in(boolean condition, String column, Collection<?> value) {
  1180. if (condition && CollectionUtils.isNotEmpty(value)) {
  1181. sql.WHERE(formatSql(inExpression(column, value, false), value.toArray()));
  1182. }
  1183. return this;
  1184. }
  1185. /**
  1186. * <p>
  1187. * IN 条件语句,目前适配mysql及oracle
  1188. * </p>
  1189. *
  1190. * @param column 字段名称
  1191. * @param value 匹配值 集合
  1192. * @return this
  1193. */
  1194. public Wrapper<T> in(String column, Collection<?> value) {
  1195. return in(true, column, value);
  1196. }
  1197. /**
  1198. * <p>
  1199. * NOT IN 条件语句,目前适配mysql及oracle
  1200. * </p>
  1201. *
  1202. * @param condition 拼接的前置条件
  1203. * @param column 字段名称
  1204. * @param value 匹配值 集合
  1205. * @return this
  1206. */
  1207. public Wrapper<T> notIn(boolean condition, String column, Collection<?> value) {
  1208. if (condition && CollectionUtils.isNotEmpty(value)) {
  1209. sql.WHERE(formatSql(inExpression(column, value, true), value.toArray()));
  1210. }
  1211. return this;
  1212. }
  1213. /**
  1214. * <p>
  1215. * NOT IN 条件语句,目前适配mysql及oracle
  1216. * </p>
  1217. *
  1218. * @param column 字段名称
  1219. * @param value 匹配值 集合
  1220. * @return this
  1221. */
  1222. public Wrapper<T> notIn(String column, Collection<?> value) {
  1223. return notIn(true, column, value);
  1224. }
  1225. /**
  1226. * <p>
  1227. * IN 条件语句,目前适配mysql及oracle
  1228. * </p>
  1229. *
  1230. * @param condition 拼接的前置条件
  1231. * @param column 字段名称
  1232. * @param value 匹配值 object数组
  1233. * @return this
  1234. */
  1235. public Wrapper<T> in(boolean condition, String column, Object[] value) {
  1236. if (condition && ArrayUtils.isNotEmpty(value)) {
  1237. sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), false), value));
  1238. }
  1239. return this;
  1240. }
  1241. /**
  1242. * <p>
  1243. * IN 条件语句,目前适配mysql及oracle
  1244. * </p>
  1245. *
  1246. * @param column 字段名称
  1247. * @param value 匹配值 object数组
  1248. * @return this
  1249. */
  1250. public Wrapper<T> in(String column, Object[] value) {
  1251. return in(true, column, value);
  1252. }
  1253. /**
  1254. * <p>
  1255. * NOT IN 条件语句,目前适配mysql及oracle
  1256. * </p>
  1257. *
  1258. * @param condition 拼接的前置条件
  1259. * @param column 字段名称
  1260. * @param value 匹配值 object数组
  1261. * @return this
  1262. */
  1263. public Wrapper<T> notIn(boolean condition, String column, Object... value) {
  1264. if (condition && ArrayUtils.isNotEmpty(value)) {
  1265. sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), true), value));
  1266. }
  1267. return this;
  1268. }
  1269. /**
  1270. * <p>
  1271. * NOT IN 条件语句,目前适配mysql及oracle
  1272. * </p>
  1273. *
  1274. * @param column 字段名称
  1275. * @param value 匹配值 object数组
  1276. * @return this
  1277. */
  1278. public Wrapper<T> notIn(String column, Object... value) {
  1279. return notIn(true, column, value);
  1280. }
  1281. /**
  1282. * <p>
  1283. * 获取in表达式
  1284. * </p>
  1285. *
  1286. * @param column 字段名称
  1287. * @param value 集合
  1288. * @param isNot 是否为NOT IN操作
  1289. */
  1290. private String inExpression(String column, Collection<?> value, boolean isNot) {
  1291. if (StringUtils.isNotEmpty(column) && CollectionUtils.isNotEmpty(value)) {
  1292. StringBuilder inSql = new StringBuilder();
  1293. inSql.append(column);
  1294. if (isNot) {
  1295. inSql.append(" NOT");
  1296. }
  1297. inSql.append(" IN ");
  1298. inSql.append("(");
  1299. int size = value.size();
  1300. for (int i = 0; i < size; i++) {
  1301. inSql.append(String.format(PLACE_HOLDER, i));
  1302. if (i + 1 < size) {
  1303. inSql.append(",");
  1304. }
  1305. }
  1306. inSql.append(")");
  1307. return inSql.toString();
  1308. }
  1309. return null;
  1310. }
  1311. /**
  1312. * <p>
  1313. * betwwee 条件语句
  1314. * </p>
  1315. *
  1316. * @param condition 拼接的前置条件
  1317. * @param column 字段名称
  1318. * @param val1
  1319. * @param val2
  1320. * @return this
  1321. */
  1322. public Wrapper<T> between(boolean condition, String column, Object val1, Object val2) {
  1323. if (condition) {
  1324. sql.WHERE(formatSql(String.format("%s BETWEEN {0} AND {1}", column), val1, val2));
  1325. }
  1326. return this;
  1327. }
  1328. /**
  1329. * <p>
  1330. * betwwee 条件语句
  1331. * </p>
  1332. *
  1333. * @param column 字段名称
  1334. * @param val1
  1335. * @param val2
  1336. * @return this
  1337. */
  1338. public Wrapper<T> between(String column, Object val1, Object val2) {
  1339. return between(true, column, val1, val2);
  1340. }
  1341. /**
  1342. * <p>
  1343. * NOT betwwee 条件语句
  1344. * </p>
  1345. *
  1346. * @param condition 拼接的前置条件
  1347. * @param column 字段名称
  1348. * @param val1
  1349. * @param val2
  1350. * @return this
  1351. */
  1352. public Wrapper<T> notBetween(boolean condition, String column, Object val1, Object val2) {
  1353. if (condition) {
  1354. sql.WHERE(formatSql(String.format("%s NOT BETWEEN {0} AND {1}", column), val1, val2));
  1355. }
  1356. return this;
  1357. }
  1358. /**
  1359. * <p>
  1360. * NOT betwwee 条件语句
  1361. * </p>
  1362. *
  1363. * @param column 字段名称
  1364. * @param val1
  1365. * @param val2
  1366. * @return this
  1367. */
  1368. public Wrapper<T> notBetween(String column, Object val1, Object val2) {
  1369. return notBetween(true, column, val1, val2);
  1370. }
  1371. /**
  1372. * <p>
  1373. * 为了兼容之前的版本,可使用where()或and()替代
  1374. * </p>
  1375. *
  1376. * @param sqlWhere where sql部分
  1377. * @param params 参数集
  1378. * @return this
  1379. */
  1380. public Wrapper<T> addFilter(String sqlWhere, Object... params) {
  1381. return and(sqlWhere, params);
  1382. }
  1383. /**
  1384. * <p>
  1385. * 根据判断条件来添加条件语句部分 使用 andIf() 替代
  1386. * </p>
  1387. * <p>
  1388. * eg: ew.filterIfNeed(false,"name='zhangsan'").where("name='zhangsan'")
  1389. * .filterIfNeed(true,"id={0}",22)
  1390. * <p>
  1391. * 输出: WHERE (name='zhangsan' AND id=22)
  1392. * </p>
  1393. *
  1394. * @param need 是否需要添加该条件
  1395. * @param sqlWhere 条件语句
  1396. * @param params 参数集
  1397. * @return this
  1398. */
  1399. public Wrapper<T> addFilterIfNeed(boolean need, String sqlWhere, Object... params) {
  1400. return need ? where(sqlWhere, params) : this;
  1401. }
  1402. /**
  1403. * <p>
  1404. * 格式化SQL
  1405. * </p>
  1406. *
  1407. * @param sqlStr SQL语句部分
  1408. * @param params 参数集
  1409. * @return this
  1410. */
  1411. protected String formatSql(String sqlStr, Object... params) {
  1412. return formatSqlIfNeed(true, sqlStr, params);
  1413. }
  1414. /**
  1415. * <p>
  1416. * 根据需要格式化SQL<BR>
  1417. * <BR>
  1418. * Format SQL for methods: EntityWrapper<T>.where/and/or...("name={0}", value);
  1419. * ALL the {<b>i</b>} will be replaced with #{MPGENVAL<b>i</b>}<BR>
  1420. * <BR>
  1421. * ew.where("sample_name=<b>{0}</b>", "haha").and("sample_age &gt;<b>{0}</b>
  1422. * and sample_age&lt;<b>{1}</b>", 18, 30) <b>TO</b>
  1423. * sample_name=<b>#{MPGENVAL1}</b> and sample_age&gt;#<b>{MPGENVAL2}</b> and
  1424. * sample_age&lt;<b>#{MPGENVAL3}</b><BR>
  1425. * </p>
  1426. *
  1427. * @param need 是否需要格式化
  1428. * @param sqlStr SQL语句部分
  1429. * @param params 参数集
  1430. * @return this
  1431. */
  1432. protected String formatSqlIfNeed(boolean need, String sqlStr, Object... params) {
  1433. if (!need || StringUtils.isEmpty(sqlStr)) {
  1434. return null;
  1435. }
  1436. // #200
  1437. if (ArrayUtils.isNotEmpty(params)) {
  1438. for (int i = 0; i < params.length; ++i) {
  1439. String genParamName = MP_GENERAL_PARAMNAME + paramNameSeq.incrementAndGet();
  1440. sqlStr = sqlStr.replace(String.format(PLACE_HOLDER, i),
  1441. String.format(MYBATIS_PLUS_TOKEN, getParamAlias(), genParamName));
  1442. paramNameValuePairs.put(genParamName, params[i]);
  1443. }
  1444. }
  1445. return sqlStr;
  1446. }
  1447. /**
  1448. * <p>
  1449. * 自定义是否输出sql开头为 `WHERE` OR `AND` OR `OR`
  1450. * </p>
  1451. *
  1452. * @param bool
  1453. * @return this
  1454. */
  1455. public Wrapper<T> isWhere(Boolean bool) {
  1456. this.isWhere = bool;
  1457. return this;
  1458. }
  1459. /**
  1460. * <p>
  1461. * 手动把sql拼接到最后(有sql注入的风险,请谨慎使用)
  1462. * </p>
  1463. *
  1464. * @param limit
  1465. * @return this
  1466. */
  1467. public Wrapper<T> last(String limit) {
  1468. sql.LAST(limit);
  1469. return this;
  1470. }
  1471. /**
  1472. * Fix issue 200.
  1473. *
  1474. * @return
  1475. * @since 2.0.3
  1476. */
  1477. public Map<String, Object> getParamNameValuePairs() {
  1478. return paramNameValuePairs;
  1479. }
  1480. public String getParamAlias() {
  1481. return StringUtils.isEmpty(paramAlias) ? DEFAULT_PARAM_ALIAS : paramAlias;
  1482. }
  1483. /**
  1484. * <p>
  1485. * 参数别名设置,初始化时优先设置该值、重复设置异常
  1486. * </p>
  1487. *
  1488. * @param paramAlias 参数别名
  1489. * @return
  1490. */
  1491. public Wrapper<T> setParamAlias(String paramAlias) {
  1492. if (StringUtils.isNotEmpty(getSqlSegment())) {
  1493. throw new MybatisPlusException("Error: Please call this method when initializing!");
  1494. }
  1495. if (StringUtils.isNotEmpty(this.paramAlias)) {
  1496. throw new MybatisPlusException("Error: Please do not call the method repeatedly!");
  1497. }
  1498. this.paramAlias = paramAlias;
  1499. return this;
  1500. }
  1501. /**
  1502. * <p>
  1503. * 克隆本身 fixed github issues/241
  1504. * </p>
  1505. */
  1506. @Override
  1507. public Wrapper<T> clone() {
  1508. return SerializationUtils.clone(this);
  1509. }
  1510. @Override
  1511. public String getSqlSegment() {
  1512. // 子类重载
  1513. return null;
  1514. }
  1515. }