Wrapper.java 39 KB

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