Wrapper.java 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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.io.Serializable;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.Map;
  23. import java.util.concurrent.atomic.AtomicInteger;
  24. import com.baomidou.mybatisplus.entity.Column;
  25. import com.baomidou.mybatisplus.entity.Columns;
  26. import com.baomidou.mybatisplus.enums.SqlLike;
  27. import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
  28. import com.baomidou.mybatisplus.toolkit.ArrayUtils;
  29. import com.baomidou.mybatisplus.toolkit.CollectionUtils;
  30. import com.baomidou.mybatisplus.toolkit.MapUtils;
  31. import com.baomidou.mybatisplus.toolkit.SqlUtils;
  32. import com.baomidou.mybatisplus.toolkit.StringUtils;
  33. /**
  34. * <p>
  35. * 条件构造抽象类,定义T-SQL语法
  36. * </p>
  37. *
  38. * @author hubin , yanghu , Dyang , Caratacus
  39. * @Date 2016-11-7
  40. */
  41. @SuppressWarnings("serial")
  42. public abstract class Wrapper<T> implements Serializable {
  43. /**
  44. * 占位符
  45. */
  46. private static final String PLACE_HOLDER = "{%s}";
  47. private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}";
  48. private static final String MP_GENERAL_PARAMNAME = "MPGENVAL";
  49. private static final String DEFAULT_PARAM_ALIAS = "ew";
  50. /**
  51. * 实现了TSQL语法的SQL实体
  52. */
  53. protected final SqlPlus sql = new SqlPlus();
  54. private final Map<String, Object> paramNameValuePairs = new HashMap<>(4);
  55. private final AtomicInteger paramNameSeq = new AtomicInteger(0);
  56. protected String paramAlias = null;
  57. /**
  58. * SQL 查询字段内容,例如:id,name,age
  59. */
  60. protected String sqlSelect = null;
  61. /**
  62. * 自定义是否输出sql为 WHERE OR AND OR OR
  63. */
  64. protected Boolean isWhere;
  65. /**
  66. * 拼接WHERE后应该是AND还是ORnull
  67. */
  68. protected String AND_OR = "AND";
  69. /**
  70. * <p>
  71. * 兼容EntityWrapper
  72. * </p>
  73. *
  74. * @return
  75. */
  76. public T getEntity() {
  77. return null;
  78. }
  79. /**
  80. * 查看where构造是否为空
  81. *
  82. * @return
  83. */
  84. public boolean isEmptyOfWhere() {
  85. return sql.isEmptyOfWhere();
  86. }
  87. /**
  88. * 查看where构造是否不为空
  89. *
  90. * @return
  91. */
  92. public boolean isNotEmptyOfWhere() {
  93. return !isEmptyOfWhere();
  94. }
  95. public String getSqlSelect() {
  96. return StringUtils.isEmpty(sqlSelect) ? null : stripSqlInjection(sqlSelect);
  97. }
  98. public Wrapper<T> setSqlSelect(String sqlSelect) {
  99. if (StringUtils.isNotEmpty(sqlSelect)) {
  100. this.sqlSelect = sqlSelect;
  101. }
  102. return this;
  103. }
  104. /**
  105. * <p>
  106. * 使用字符串数组封装sqlSelect,便于在不需要指定 AS 的情况下通过实体类自动生成的列静态字段快速组装 sqlSelect,<br/>
  107. * 减少手动录入的错误率
  108. * </p>
  109. *
  110. * @param columns 字段
  111. * @return
  112. */
  113. public Wrapper<T> setSqlSelect(String... columns) {
  114. StringBuilder builder = new StringBuilder();
  115. for (String column : columns) {
  116. if (StringUtils.isNotEmpty(column)) {
  117. if (builder.length() > 0) {
  118. builder.append(",");
  119. }
  120. builder.append(column);
  121. }
  122. }
  123. this.sqlSelect = builder.toString();
  124. return this;
  125. }
  126. /**
  127. * <p>
  128. * 使用对象封装的setsqlselect
  129. * </p>
  130. *
  131. * @param column 字段
  132. * @return
  133. */
  134. public Wrapper<T> setSqlSelect(Column... column) {
  135. if (ArrayUtils.isNotEmpty(column)) {
  136. StringBuilder builder = new StringBuilder();
  137. for (int i = 0; i < column.length; i++) {
  138. if (column[i] != null) {
  139. String col = column[i].getColumn();
  140. String as = column[i].getAs();
  141. if (StringUtils.isEmpty(col)) {
  142. continue;
  143. }
  144. builder.append(col).append(as);
  145. if (i < column.length - 1) {
  146. builder.append(",");
  147. }
  148. }
  149. }
  150. this.sqlSelect = builder.toString();
  151. }
  152. return this;
  153. }
  154. /**
  155. * <p>
  156. * 使用对象封装的setsqlselect
  157. * </p>
  158. *
  159. * @param columns 字段
  160. * @return
  161. */
  162. public Wrapper<T> setSqlSelect(Columns columns) {
  163. Column[] columnArray = columns.getColumns();
  164. if (ArrayUtils.isNotEmpty(columnArray)) {
  165. setSqlSelect(columnArray);
  166. }
  167. return this;
  168. }
  169. /**
  170. * <p>
  171. * SQL 片段 (子类实现)
  172. * </p>
  173. */
  174. public abstract String getSqlSegment();
  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. * <p>
  538. *
  539. * @return this
  540. */
  541. public Wrapper<T> and() {
  542. sql.AND();
  543. return this;
  544. }
  545. /**
  546. * <p>
  547. * 使用OR连接并换行
  548. * </p>
  549. *
  550. * @return this
  551. */
  552. public Wrapper<T> or() {
  553. sql.OR();
  554. return this;
  555. }
  556. /**
  557. * <p>
  558. * 添加OR条件
  559. * </p>
  560. *
  561. * @param condition 拼接的前置条件
  562. * @param sqlOr or 条件语句
  563. * @param params 参数集
  564. * @return this
  565. */
  566. public Wrapper<T> or(boolean condition, String sqlOr, Object... params) {
  567. if (condition) {
  568. if (StringUtils.isEmpty(sql.toString())) {
  569. AND_OR = "OR";
  570. }
  571. sql.OR().WHERE(formatSql(sqlOr, params));
  572. }
  573. return this;
  574. }
  575. /**
  576. * <p>
  577. * 添加OR条件
  578. * </p>
  579. *
  580. * @param sqlOr or 条件语句
  581. * @param params 参数集
  582. * @return this
  583. */
  584. public Wrapper<T> or(String sqlOr, Object... params) {
  585. return or(true, sqlOr, params);
  586. }
  587. /**
  588. * <p>
  589. * 使用OR换行,并添加一个带()的新的条件
  590. * </p>
  591. * <p>
  592. * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
  593. * (name='zhangsan' AND id=11) OR (statu=1)
  594. * </p>
  595. *
  596. * @return this
  597. */
  598. public Wrapper<T> orNew() {
  599. sql.OR_NEW();
  600. return this;
  601. }
  602. /**
  603. * <p>
  604. * 使用OR换行,并添加一个带()的新的条件
  605. * </p>
  606. * <p>
  607. * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
  608. * (name='zhangsan' AND id=11) OR (statu=1)
  609. * </p>
  610. *
  611. * @param condition 拼接的前置条件
  612. * @param sqlOr AND 条件语句
  613. * @param params 参数值
  614. * @return this
  615. */
  616. public Wrapper<T> orNew(boolean condition, String sqlOr, Object... params) {
  617. if (condition) {
  618. if (StringUtils.isEmpty(sql.toString())) {
  619. AND_OR = "OR";
  620. }
  621. sql.OR_NEW().WHERE(formatSql(sqlOr, params));
  622. }
  623. return this;
  624. }
  625. /**
  626. * <p>
  627. * 使用OR换行,并添加一个带()的新的条件
  628. * </p>
  629. * <p>
  630. * eg: ew.where("name='zhangsan'").and("id=11").orNew("statu=1"); 输出: WHERE
  631. * (name='zhangsan' AND id=11) OR (statu=1)
  632. * </p>
  633. *
  634. * @param sqlOr AND 条件语句
  635. * @param params 参数值
  636. * @return this
  637. */
  638. public Wrapper<T> orNew(String sqlOr, Object... params) {
  639. return orNew(true, sqlOr, params);
  640. }
  641. /**
  642. * <p>
  643. * SQL中groupBy关键字跟的条件语句
  644. * </p>
  645. * <p>
  646. * eg: ew.where("name='zhangsan'").groupBy("id,name")
  647. * </p>
  648. *
  649. * @param condition 拼接的前置条件
  650. * @param columns SQL 中的 Group by 语句,无需输入 Group By 关键字
  651. * @return this
  652. */
  653. public Wrapper<T> groupBy(boolean condition, String columns) {
  654. if (condition) {
  655. sql.GROUP_BY(columns);
  656. }
  657. return this;
  658. }
  659. /**
  660. * <p>
  661. * SQL中groupBy关键字跟的条件语句
  662. * </p>
  663. * <p>
  664. * eg: ew.where("name='zhangsan'").groupBy("id,name")
  665. * </p>
  666. *
  667. * @param columns SQL 中的 Group by 语句,无需输入 Group By 关键字
  668. * @return this
  669. */
  670. public Wrapper<T> groupBy(String columns) {
  671. return groupBy(true, columns);
  672. }
  673. /**
  674. * <p>
  675. * SQL中having关键字跟的条件语句
  676. * </p>
  677. * <p>
  678. * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null")
  679. * </p>
  680. *
  681. * @param condition 拼接的前置条件
  682. * @param sqlHaving having关键字后面跟随的语句
  683. * @param params 参数集
  684. * @return EntityWrapper<T>
  685. */
  686. public Wrapper<T> having(boolean condition, String sqlHaving, Object... params) {
  687. if (condition) {
  688. sql.HAVING(formatSql(sqlHaving, params));
  689. }
  690. return this;
  691. }
  692. /**
  693. * <p>
  694. * SQL中having关键字跟的条件语句
  695. * </p>
  696. * <p>
  697. * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null")
  698. * </p>
  699. *
  700. * @param sqlHaving having关键字后面跟随的语句
  701. * @param params 参数集
  702. * @return EntityWrapper<T>
  703. */
  704. public Wrapper<T> having(String sqlHaving, Object... params) {
  705. return having(true, sqlHaving, params);
  706. }
  707. /**
  708. * <p>
  709. * SQL中orderby关键字跟的条件语句
  710. * </p>
  711. * <p>
  712. * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null"
  713. * ).orderBy("id,name")
  714. * </p>
  715. *
  716. * @param condition 拼接的前置条件
  717. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  718. * @return this
  719. */
  720. public Wrapper<T> orderBy(boolean condition, String columns) {
  721. if (condition) {
  722. sql.ORDER_BY(columns);
  723. }
  724. return this;
  725. }
  726. /**
  727. * <p>
  728. * SQL中orderby关键字跟的条件语句
  729. * </p>
  730. * <p>
  731. * eg: ew.groupBy("id,name").having("id={0}",22).and("password is not null"
  732. * ).orderBy("id,name")
  733. * </p>
  734. *
  735. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  736. * @return this
  737. */
  738. public Wrapper<T> orderBy(String columns) {
  739. return orderBy(true, columns);
  740. }
  741. /**
  742. * <p>
  743. * SQL中orderby关键字跟的条件语句,可根据变更动态排序
  744. * </p>
  745. *
  746. * @param condition 拼接的前置条件
  747. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  748. * @param isAsc 是否为升序
  749. * @return this
  750. */
  751. public Wrapper<T> orderBy(boolean condition, String columns, boolean isAsc) {
  752. if (condition && StringUtils.isNotEmpty(columns)) {
  753. sql.ORDER_BY(columns + (isAsc ? " ASC" : " DESC"));
  754. }
  755. return this;
  756. }
  757. /**
  758. * <p>
  759. * SQL中orderby关键字跟的条件语句,可根据变更动态排序
  760. * </p>
  761. *
  762. * @param condition 拼接的前置条件
  763. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  764. * @param isAsc 是否为升序
  765. * @return this
  766. */
  767. public Wrapper<T> orderBy(boolean condition, Collection<String> columns, boolean isAsc) {
  768. if (condition && CollectionUtils.isNotEmpty(columns)) {
  769. for (String column : columns) {
  770. orderBy(condition, column, isAsc);
  771. }
  772. }
  773. return this;
  774. }
  775. /**
  776. * <p>
  777. * SQL中orderby关键字跟的条件语句,可根据变更动态排序
  778. * </p>
  779. *
  780. * @param columns SQL 中的 order by 语句,无需输入 Order By 关键字
  781. * @param isAsc 是否为升序
  782. * @return this
  783. */
  784. public Wrapper<T> orderBy(String columns, boolean isAsc) {
  785. return orderBy(true, columns, isAsc);
  786. }
  787. /**
  788. * <p>
  789. * 批量根据ASC排序
  790. * </p>
  791. *
  792. * @param columns 需要排序的集合
  793. * @return this
  794. */
  795. public Wrapper<T> orderAsc(Collection<String> columns) {
  796. return orderBy(true, columns, true);
  797. }
  798. /**
  799. * <p>
  800. * 批量根据DESC排序
  801. * </p>
  802. *
  803. * @param columns 需要排序的集合
  804. * @return this
  805. */
  806. public Wrapper<T> orderDesc(Collection<String> columns) {
  807. return orderBy(true, columns, false);
  808. }
  809. /**
  810. * <p>
  811. * LIKE条件语句,value中无需前后%
  812. * </p>
  813. *
  814. * @param condition 拼接的前置条件
  815. * @param column 字段名称
  816. * @param value 匹配值
  817. * @return this
  818. */
  819. public Wrapper<T> like(boolean condition, String column, String value) {
  820. if (condition) {
  821. handerLike(column, value, SqlLike.DEFAULT, false);
  822. }
  823. return this;
  824. }
  825. /**
  826. * <p>
  827. * LIKE条件语句,value中无需前后%
  828. * </p>
  829. *
  830. * @param column 字段名称
  831. * @param value 匹配值
  832. * @return this
  833. */
  834. public Wrapper<T> like(String column, String value) {
  835. return like(true, column, value);
  836. }
  837. /**
  838. * <p>
  839. * NOT LIKE条件语句,value中无需前后%
  840. * </p>
  841. *
  842. * @param condition 拼接的前置条件
  843. * @param column 字段名称
  844. * @param value 匹配值
  845. * @return this
  846. */
  847. public Wrapper<T> notLike(boolean condition, String column, String value) {
  848. if (condition) {
  849. handerLike(column, value, SqlLike.DEFAULT, true);
  850. }
  851. return this;
  852. }
  853. /**
  854. * <p>
  855. * NOT LIKE条件语句,value中无需前后%
  856. * </p>
  857. *
  858. * @param column 字段名称
  859. * @param value 匹配值
  860. * @return this
  861. */
  862. public Wrapper<T> notLike(String column, String value) {
  863. return notLike(true, column, value);
  864. }
  865. /**
  866. * <p>
  867. * 处理LIKE操作
  868. * </p>
  869. *
  870. * @param column 字段名称
  871. * @param value like匹配值
  872. * @param isNot 是否为NOT LIKE操作
  873. */
  874. private void handerLike(String column, String value, SqlLike type, boolean isNot) {
  875. if (StringUtils.isNotEmpty(column) && StringUtils.isNotEmpty(value)) {
  876. StringBuilder inSql = new StringBuilder();
  877. inSql.append(column);
  878. if (isNot) {
  879. inSql.append(" NOT");
  880. }
  881. inSql.append(" LIKE {0}");
  882. sql.WHERE(formatSql(inSql.toString(), SqlUtils.concatLike(value, type)));
  883. }
  884. }
  885. /**
  886. * <p>
  887. * LIKE条件语句,value中无需前后%
  888. * </p>
  889. *
  890. * @param condition 拼接的前置条件
  891. * @param column 字段名称
  892. * @param value 匹配值
  893. * @param type
  894. * @return this
  895. */
  896. public Wrapper<T> like(boolean condition, String column, String value, SqlLike type) {
  897. if (condition) {
  898. handerLike(column, value, type, false);
  899. }
  900. return this;
  901. }
  902. /**
  903. * <p>
  904. * LIKE条件语句,value中无需前后%
  905. * </p>
  906. *
  907. * @param column 字段名称
  908. * @param value 匹配值
  909. * @param type
  910. * @return this
  911. */
  912. public Wrapper<T> like(String column, String value, SqlLike type) {
  913. return like(true, column, value, type);
  914. }
  915. /**
  916. * <p>
  917. * NOT LIKE条件语句,value中无需前后%
  918. * </p>
  919. *
  920. * @param condition 拼接的前置条件
  921. * @param column 字段名称
  922. * @param value 匹配值
  923. * @param type
  924. * @return this
  925. */
  926. public Wrapper<T> notLike(boolean condition, String column, String value, SqlLike type) {
  927. if (condition) {
  928. handerLike(column, value, type, true);
  929. }
  930. return this;
  931. }
  932. /**
  933. * <p>
  934. * NOT LIKE条件语句,value中无需前后%
  935. * </p>
  936. *
  937. * @param column 字段名称
  938. * @param value 匹配值
  939. * @param type
  940. * @return this
  941. */
  942. public Wrapper<T> notLike(String column, String value, SqlLike type) {
  943. return notLike(true, column, value, type);
  944. }
  945. /**
  946. * <p>
  947. * is not null 条件
  948. * </p>
  949. *
  950. * @param condition 拼接的前置条件
  951. * @param columns 字段名称。多个字段以逗号分隔。
  952. * @return this
  953. */
  954. public Wrapper<T> isNotNull(boolean condition, String columns) {
  955. if (condition) {
  956. sql.IS_NOT_NULL(columns);
  957. }
  958. return this;
  959. }
  960. /**
  961. * <p>
  962. * is not null 条件
  963. * </p>
  964. *
  965. * @param columns 字段名称。多个字段以逗号分隔。
  966. * @return this
  967. */
  968. public Wrapper<T> isNotNull(String columns) {
  969. return isNotNull(true, columns);
  970. }
  971. /**
  972. * <p>
  973. * is null 条件
  974. * </p>
  975. *
  976. * @param condition 拼接的前置条件
  977. * @param columns 字段名称。多个字段以逗号分隔。
  978. * @return this
  979. */
  980. public Wrapper<T> isNull(boolean condition, String columns) {
  981. if (condition) {
  982. sql.IS_NULL(columns);
  983. }
  984. return this;
  985. }
  986. /**
  987. * <p>
  988. * is null 条件
  989. * </p>
  990. *
  991. * @param columns 字段名称。多个字段以逗号分隔。
  992. * @return this
  993. */
  994. public Wrapper<T> isNull(String columns) {
  995. return isNull(true, columns);
  996. }
  997. /**
  998. * <p>
  999. * EXISTS 条件语句,目前适配mysql及oracle
  1000. * </p>
  1001. *
  1002. * @param condition 拼接的前置条件
  1003. * @param value 匹配值
  1004. * @return this
  1005. */
  1006. public Wrapper<T> exists(boolean condition, String value) {
  1007. if (condition) {
  1008. sql.EXISTS(value);
  1009. }
  1010. return this;
  1011. }
  1012. /**
  1013. * <p>
  1014. * EXISTS 条件语句,目前适配mysql及oracle
  1015. * </p>
  1016. *
  1017. * @param value 匹配值
  1018. * @return this
  1019. */
  1020. public Wrapper<T> exists(String value) {
  1021. return exists(true, value);
  1022. }
  1023. /**
  1024. * <p>
  1025. * NOT EXISTS条件语句
  1026. * </p>
  1027. *
  1028. * @param condition 拼接的前置条件
  1029. * @param value 匹配值
  1030. * @return this
  1031. */
  1032. public Wrapper<T> notExists(boolean condition, String value) {
  1033. if (condition) {
  1034. sql.NOT_EXISTS(value);
  1035. }
  1036. return this;
  1037. }
  1038. /**
  1039. * <p>
  1040. * NOT EXISTS条件语句
  1041. * </p>
  1042. *
  1043. * @param value 匹配值
  1044. * @return this
  1045. */
  1046. public Wrapper<T> notExists(String value) {
  1047. return notExists(true, value);
  1048. }
  1049. /**
  1050. * <p>
  1051. * IN 条件语句,目前适配mysql及oracle
  1052. * </p>
  1053. *
  1054. * @param condition 拼接的前置条件
  1055. * @param column 字段名称
  1056. * @param value 逗号拼接的字符串
  1057. * @return this
  1058. */
  1059. public Wrapper<T> in(boolean condition, String column, String value) {
  1060. if (condition && StringUtils.isNotEmpty(value)) {
  1061. in(column, StringUtils.splitWorker(value, ",", -1, false));
  1062. }
  1063. return this;
  1064. }
  1065. /**
  1066. * <p>
  1067. * IN 条件语句,目前适配mysql及oracle
  1068. * </p>
  1069. *
  1070. * @param column 字段名称
  1071. * @param value 逗号拼接的字符串
  1072. * @return this
  1073. */
  1074. public Wrapper<T> in(String column, String value) {
  1075. return in(true, column, value);
  1076. }
  1077. /**
  1078. * <p>
  1079. * NOT IN条件语句
  1080. * </p>
  1081. *
  1082. * @param condition 拼接的前置条件
  1083. * @param column 字段名称
  1084. * @param value 逗号拼接的字符串
  1085. * @return this
  1086. */
  1087. public Wrapper<T> notIn(boolean condition, String column, String value) {
  1088. if (condition && StringUtils.isNotEmpty(value)) {
  1089. notIn(column, StringUtils.splitWorker(value, ",", -1, false));
  1090. }
  1091. return this;
  1092. }
  1093. /**
  1094. * <p>
  1095. * NOT IN条件语句
  1096. * </p>
  1097. *
  1098. * @param column 字段名称
  1099. * @param value 逗号拼接的字符串
  1100. * @return this
  1101. */
  1102. public Wrapper<T> notIn(String column, String value) {
  1103. return notIn(true, column, value);
  1104. }
  1105. /**
  1106. * <p>
  1107. * IN 条件语句,目前适配mysql及oracle
  1108. * </p>
  1109. *
  1110. * @param condition 拼接的前置条件
  1111. * @param column 字段名称
  1112. * @param value 匹配值 集合
  1113. * @return this
  1114. */
  1115. public Wrapper<T> in(boolean condition, String column, Collection<?> value) {
  1116. if (condition && CollectionUtils.isNotEmpty(value)) {
  1117. sql.WHERE(formatSql(inExpression(column, value, false), value.toArray()));
  1118. }
  1119. return this;
  1120. }
  1121. /**
  1122. * <p>
  1123. * IN 条件语句,目前适配mysql及oracle
  1124. * </p>
  1125. *
  1126. * @param column 字段名称
  1127. * @param value 匹配值 集合
  1128. * @return this
  1129. */
  1130. public Wrapper<T> in(String column, Collection<?> value) {
  1131. return in(true, column, value);
  1132. }
  1133. /**
  1134. * <p>
  1135. * NOT IN 条件语句,目前适配mysql及oracle
  1136. * </p>
  1137. *
  1138. * @param condition 拼接的前置条件
  1139. * @param column 字段名称
  1140. * @param value 匹配值 集合
  1141. * @return this
  1142. */
  1143. public Wrapper<T> notIn(boolean condition, String column, Collection<?> value) {
  1144. if (condition && CollectionUtils.isNotEmpty(value)) {
  1145. sql.WHERE(formatSql(inExpression(column, value, true), value.toArray()));
  1146. }
  1147. return this;
  1148. }
  1149. /**
  1150. * <p>
  1151. * NOT IN 条件语句,目前适配mysql及oracle
  1152. * </p>
  1153. *
  1154. * @param column 字段名称
  1155. * @param value 匹配值 集合
  1156. * @return this
  1157. */
  1158. public Wrapper<T> notIn(String column, Collection<?> value) {
  1159. return notIn(true, column, value);
  1160. }
  1161. /**
  1162. * <p>
  1163. * IN 条件语句,目前适配mysql及oracle
  1164. * </p>
  1165. *
  1166. * @param condition 拼接的前置条件
  1167. * @param column 字段名称
  1168. * @param value 匹配值 object数组
  1169. * @return this
  1170. */
  1171. public Wrapper<T> in(boolean condition, String column, Object[] value) {
  1172. if (condition && ArrayUtils.isNotEmpty(value)) {
  1173. sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), false), value));
  1174. }
  1175. return this;
  1176. }
  1177. /**
  1178. * <p>
  1179. * IN 条件语句,目前适配mysql及oracle
  1180. * </p>
  1181. *
  1182. * @param column 字段名称
  1183. * @param value 匹配值 object数组
  1184. * @return this
  1185. */
  1186. public Wrapper<T> in(String column, Object[] value) {
  1187. return in(true, column, value);
  1188. }
  1189. /**
  1190. * <p>
  1191. * NOT IN 条件语句,目前适配mysql及oracle
  1192. * </p>
  1193. *
  1194. * @param condition 拼接的前置条件
  1195. * @param column 字段名称
  1196. * @param value 匹配值 object数组
  1197. * @return this
  1198. */
  1199. public Wrapper<T> notIn(boolean condition, String column, Object... value) {
  1200. if (condition && ArrayUtils.isNotEmpty(value)) {
  1201. sql.WHERE(formatSql(inExpression(column, Arrays.asList(value), true), value));
  1202. }
  1203. return this;
  1204. }
  1205. /**
  1206. * <p>
  1207. * NOT IN 条件语句,目前适配mysql及oracle
  1208. * </p>
  1209. *
  1210. * @param column 字段名称
  1211. * @param value 匹配值 object数组
  1212. * @return this
  1213. */
  1214. public Wrapper<T> notIn(String column, Object... value) {
  1215. return notIn(true, column, value);
  1216. }
  1217. /**
  1218. * <p>
  1219. * 获取in表达式
  1220. * </p>
  1221. *
  1222. * @param column 字段名称
  1223. * @param value 集合
  1224. * @param isNot 是否为NOT IN操作
  1225. */
  1226. private String inExpression(String column, Collection<?> value, boolean isNot) {
  1227. if (StringUtils.isNotEmpty(column) && CollectionUtils.isNotEmpty(value)) {
  1228. StringBuilder inSql = new StringBuilder();
  1229. inSql.append(column);
  1230. if (isNot) {
  1231. inSql.append(" NOT");
  1232. }
  1233. inSql.append(" IN ");
  1234. inSql.append("(");
  1235. int size = value.size();
  1236. for (int i = 0; i < size; i++) {
  1237. inSql.append(String.format(PLACE_HOLDER, i));
  1238. if (i + 1 < size) {
  1239. inSql.append(",");
  1240. }
  1241. }
  1242. inSql.append(")");
  1243. return inSql.toString();
  1244. }
  1245. return null;
  1246. }
  1247. /**
  1248. * <p>
  1249. * betwwee 条件语句
  1250. * </p>
  1251. *
  1252. * @param condition 拼接的前置条件
  1253. * @param column 字段名称
  1254. * @param val1
  1255. * @param val2
  1256. * @return this
  1257. */
  1258. public Wrapper<T> between(boolean condition, String column, Object val1, Object val2) {
  1259. if (condition) {
  1260. sql.WHERE(formatSql(String.format("%s BETWEEN {0} AND {1}", column), val1, val2));
  1261. }
  1262. return this;
  1263. }
  1264. /**
  1265. * <p>
  1266. * betwwee 条件语句
  1267. * </p>
  1268. *
  1269. * @param column 字段名称
  1270. * @param val1
  1271. * @param val2
  1272. * @return this
  1273. */
  1274. public Wrapper<T> between(String column, Object val1, Object val2) {
  1275. return between(true, column, val1, val2);
  1276. }
  1277. /**
  1278. * <p>
  1279. * NOT betwwee 条件语句
  1280. * </p>
  1281. *
  1282. * @param condition 拼接的前置条件
  1283. * @param column 字段名称
  1284. * @param val1
  1285. * @param val2
  1286. * @return this
  1287. */
  1288. public Wrapper<T> notBetween(boolean condition, String column, Object val1, Object val2) {
  1289. if (condition) {
  1290. sql.WHERE(formatSql(String.format("%s NOT BETWEEN {0} AND {1}", column), val1, val2));
  1291. }
  1292. return this;
  1293. }
  1294. /**
  1295. * <p>
  1296. * NOT betwwee 条件语句
  1297. * </p>
  1298. *
  1299. * @param column 字段名称
  1300. * @param val1
  1301. * @param val2
  1302. * @return this
  1303. */
  1304. public Wrapper<T> notBetween(String column, Object val1, Object val2) {
  1305. return notBetween(true, column, val1, val2);
  1306. }
  1307. /**
  1308. * <p>
  1309. * 为了兼容之前的版本,可使用where()或and()替代
  1310. * </p>
  1311. *
  1312. * @param sqlWhere where sql部分
  1313. * @param params 参数集
  1314. * @return this
  1315. */
  1316. public Wrapper<T> addFilter(String sqlWhere, Object... params) {
  1317. return and(sqlWhere, params);
  1318. }
  1319. /**
  1320. * <p>
  1321. * 根据判断条件来添加条件语句部分 使用 andIf() 替代
  1322. * </p>
  1323. * <p>
  1324. * eg: ew.filterIfNeed(false,"name='zhangsan'").where("name='zhangsan'")
  1325. * .filterIfNeed(true,"id={0}",22)
  1326. * <p>
  1327. * 输出: WHERE (name='zhangsan' AND id=22)
  1328. * </p>
  1329. *
  1330. * @param need 是否需要添加该条件
  1331. * @param sqlWhere 条件语句
  1332. * @param params 参数集
  1333. * @return this
  1334. */
  1335. public Wrapper<T> addFilterIfNeed(boolean need, String sqlWhere, Object... params) {
  1336. return need ? where(sqlWhere, params) : this;
  1337. }
  1338. /**
  1339. * <p>
  1340. * SQL注入内容剥离
  1341. * </p>
  1342. *
  1343. * @param value 待处理内容
  1344. * @return this
  1345. */
  1346. protected String stripSqlInjection(String value) {
  1347. return value.replaceAll("('.+--)|(--)|(\\|)|(%7C)", "");
  1348. }
  1349. /**
  1350. * <p>
  1351. * 格式化SQL
  1352. * </p>
  1353. *
  1354. * @param sqlStr SQL语句部分
  1355. * @param params 参数集
  1356. * @return this
  1357. */
  1358. protected String formatSql(String sqlStr, Object... params) {
  1359. return formatSqlIfNeed(true, sqlStr, params);
  1360. }
  1361. /**
  1362. * <p>
  1363. * 根据需要格式化SQL<BR>
  1364. * <BR>
  1365. * Format SQL for methods: EntityWrapper<T>.where/and/or...("name={0}", value);
  1366. * ALL the {<b>i</b>} will be replaced with #{MPGENVAL<b>i</b>}<BR>
  1367. * <BR>
  1368. * ew.where("sample_name=<b>{0}</b>", "haha").and("sample_age &gt;<b>{0}</b>
  1369. * and sample_age&lt;<b>{1}</b>", 18, 30) <b>TO</b>
  1370. * sample_name=<b>#{MPGENVAL1}</b> and sample_age&gt;#<b>{MPGENVAL2}</b> and
  1371. * sample_age&lt;<b>#{MPGENVAL3}</b><BR>
  1372. * </p>
  1373. *
  1374. * @param need 是否需要格式化
  1375. * @param sqlStr SQL语句部分
  1376. * @param params 参数集
  1377. * @return this
  1378. */
  1379. protected String formatSqlIfNeed(boolean need, String sqlStr, Object... params) {
  1380. if (!need || StringUtils.isEmpty(sqlStr)) {
  1381. return null;
  1382. }
  1383. // #200
  1384. if (ArrayUtils.isNotEmpty(params)) {
  1385. for (int i = 0; i < params.length; ++i) {
  1386. String genParamName = MP_GENERAL_PARAMNAME + paramNameSeq.incrementAndGet();
  1387. sqlStr = sqlStr.replace(String.format(PLACE_HOLDER, i),
  1388. String.format(MYBATIS_PLUS_TOKEN, getParamAlias(), genParamName));
  1389. paramNameValuePairs.put(genParamName, params[i]);
  1390. }
  1391. }
  1392. return sqlStr;
  1393. }
  1394. /**
  1395. * <p>
  1396. * 自定义是否输出sql开头为 `WHERE` OR `AND` OR `OR`
  1397. * </p>
  1398. *
  1399. * @param bool
  1400. * @return this
  1401. */
  1402. public Wrapper<T> isWhere(Boolean bool) {
  1403. this.isWhere = bool;
  1404. return this;
  1405. }
  1406. /**
  1407. * <p>
  1408. * 手动把sql拼接到最后(有sql注入的风险,请谨慎使用)
  1409. * </p>
  1410. *
  1411. * @param limit
  1412. * @return this
  1413. */
  1414. public Wrapper<T> last(String limit) {
  1415. sql.LAST(limit);
  1416. return this;
  1417. }
  1418. /**
  1419. * Fix issue 200.
  1420. *
  1421. * @return
  1422. * @since 2.0.3
  1423. */
  1424. public Map<String, Object> getParamNameValuePairs() {
  1425. return paramNameValuePairs;
  1426. }
  1427. public String getParamAlias() {
  1428. return StringUtils.isEmpty(paramAlias) ? DEFAULT_PARAM_ALIAS : paramAlias;
  1429. }
  1430. /**
  1431. * <p>
  1432. * 参数别名设置,初始化时优先设置该值、重复设置异常
  1433. * </p>
  1434. *
  1435. * @param paramAlias 参数别名
  1436. * @return
  1437. */
  1438. public Wrapper<T> setParamAlias(String paramAlias) {
  1439. if (StringUtils.isNotEmpty(getSqlSegment())) {
  1440. throw new MybatisPlusException("Error: Please call this method when initializing!");
  1441. }
  1442. if (StringUtils.isNotEmpty(this.paramAlias)) {
  1443. throw new MybatisPlusException("Error: Please do not call the method repeatedly!");
  1444. }
  1445. this.paramAlias = paramAlias;
  1446. return this;
  1447. }
  1448. }