AbstractChainWrapper.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.baomidou.mybatisplus.extension.conditions;
  17. import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
  18. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  19. import com.baomidou.mybatisplus.core.conditions.interfaces.Compare;
  20. import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
  21. import com.baomidou.mybatisplus.core.conditions.interfaces.Join;
  22. import com.baomidou.mybatisplus.core.conditions.interfaces.Nested;
  23. import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
  24. import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
  25. import java.util.Collection;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.function.BiPredicate;
  29. import java.util.function.Consumer;
  30. /**
  31. * 所有包装类都继承此抽象类,此抽象类代理了大部分生成 where 条件的方法
  32. * <li> 泛型: Children ,表示子类 </li>
  33. * <li> 泛型: Param ,表示子类所包装的具体 Wrapper 类型 </li>
  34. *
  35. * @author miemie
  36. * @since 2018-12-19
  37. */
  38. @SuppressWarnings({"unchecked"})
  39. public abstract class AbstractChainWrapper<T, R, Children extends AbstractChainWrapper<T, R, Children, Param>, Param extends AbstractWrapper<T, R, Param>>
  40. extends Wrapper<T> implements Compare<Children, R>, Func<Children, R>, Join<Children>, Nested<Param, Children> {
  41. protected final Children typedThis = (Children) this;
  42. /**
  43. * 子类所包装的具体 Wrapper 类型
  44. */
  45. protected Param wrapperChildren;
  46. /**
  47. * 必须的构造函数
  48. */
  49. public AbstractChainWrapper() {
  50. }
  51. public AbstractWrapper<T, R, Param> getWrapper() {
  52. return wrapperChildren;
  53. }
  54. public Children setEntity(T entity) {
  55. getWrapper().setEntity(entity);
  56. return typedThis;
  57. }
  58. public Children setEntityClass(Class<T> entityClass) {
  59. getWrapper().setEntityClass(entityClass);
  60. return typedThis;
  61. }
  62. @Override
  63. public <V> Children allEq(boolean condition, Map<R, V> params, boolean null2IsNull) {
  64. getWrapper().allEq(condition, params, null2IsNull);
  65. return typedThis;
  66. }
  67. @Override
  68. public <V> Children allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) {
  69. getWrapper().allEq(condition, filter, params, null2IsNull);
  70. return typedThis;
  71. }
  72. @Override
  73. public Children eq(boolean condition, R column, Object val) {
  74. getWrapper().eq(condition, column, val);
  75. return typedThis;
  76. }
  77. @Override
  78. public Children ne(boolean condition, R column, Object val) {
  79. getWrapper().ne(condition, column, val);
  80. return typedThis;
  81. }
  82. @Override
  83. public Children gt(boolean condition, R column, Object val) {
  84. getWrapper().gt(condition, column, val);
  85. return typedThis;
  86. }
  87. @Override
  88. public Children ge(boolean condition, R column, Object val) {
  89. getWrapper().ge(condition, column, val);
  90. return typedThis;
  91. }
  92. @Override
  93. public Children lt(boolean condition, R column, Object val) {
  94. getWrapper().lt(condition, column, val);
  95. return typedThis;
  96. }
  97. @Override
  98. public Children le(boolean condition, R column, Object val) {
  99. getWrapper().le(condition, column, val);
  100. return typedThis;
  101. }
  102. @Override
  103. public Children between(boolean condition, R column, Object val1, Object val2) {
  104. getWrapper().between(condition, column, val1, val2);
  105. return typedThis;
  106. }
  107. @Override
  108. public Children notBetween(boolean condition, R column, Object val1, Object val2) {
  109. getWrapper().notBetween(condition, column, val1, val2);
  110. return typedThis;
  111. }
  112. @Override
  113. public Children like(boolean condition, R column, Object val) {
  114. getWrapper().like(condition, column, val);
  115. return typedThis;
  116. }
  117. @Override
  118. public Children notLike(boolean condition, R column, Object val) {
  119. getWrapper().notLike(condition, column, val);
  120. return typedThis;
  121. }
  122. @Override
  123. public Children likeLeft(boolean condition, R column, Object val) {
  124. getWrapper().likeLeft(condition, column, val);
  125. return typedThis;
  126. }
  127. @Override
  128. public Children likeRight(boolean condition, R column, Object val) {
  129. getWrapper().likeRight(condition, column, val);
  130. return typedThis;
  131. }
  132. @Override
  133. public Children isNull(boolean condition, R column) {
  134. getWrapper().isNull(condition, column);
  135. return typedThis;
  136. }
  137. @Override
  138. public Children isNotNull(boolean condition, R column) {
  139. getWrapper().isNotNull(condition, column);
  140. return typedThis;
  141. }
  142. @Override
  143. public Children in(boolean condition, R column, Collection<?> coll) {
  144. getWrapper().in(condition, column, coll);
  145. return typedThis;
  146. }
  147. @Override
  148. public Children in(boolean condition, R column, Object... values) {
  149. getWrapper().in(condition, column, values);
  150. return typedThis;
  151. }
  152. @Override
  153. public Children notIn(boolean condition, R column, Collection<?> coll) {
  154. getWrapper().notIn(condition, column, coll);
  155. return typedThis;
  156. }
  157. @Override
  158. public Children notIn(boolean condition, R column, Object... values) {
  159. getWrapper().notIn(condition, column, values);
  160. return typedThis;
  161. }
  162. @Override
  163. public Children inSql(boolean condition, R column, String inValue) {
  164. getWrapper().inSql(condition, column, inValue);
  165. return typedThis;
  166. }
  167. @Override
  168. public Children gtSql(boolean condition, R column, String inValue) {
  169. getWrapper().gtSql(condition, column, inValue);
  170. return typedThis;
  171. }
  172. @Override
  173. public Children geSql(boolean condition, R column, String inValue) {
  174. getWrapper().geSql(condition, column, inValue);
  175. return typedThis;
  176. }
  177. @Override
  178. public Children ltSql(boolean condition, R column, String inValue) {
  179. getWrapper().ltSql(condition, column, inValue);
  180. return typedThis;
  181. }
  182. @Override
  183. public Children leSql(boolean condition, R column, String inValue) {
  184. getWrapper().leSql(condition, column, inValue);
  185. return typedThis;
  186. }
  187. @Override
  188. public Children notInSql(boolean condition, R column, String inValue) {
  189. getWrapper().notInSql(condition, column, inValue);
  190. return typedThis;
  191. }
  192. @Override
  193. public Children groupBy(boolean condition, R column, R... columns) {
  194. getWrapper().groupBy(condition, column, columns);
  195. return typedThis;
  196. }
  197. @Override
  198. public Children groupBy(boolean condition, R column) {
  199. getWrapper().groupBy(condition, column);
  200. return typedThis;
  201. }
  202. @Override
  203. public Children groupBy(boolean condition, List<R> columns) {
  204. getWrapper().groupBy(condition, columns);
  205. return typedThis;
  206. }
  207. @Override
  208. public Children orderBy(boolean condition, boolean isAsc, R column, R... columns) {
  209. getWrapper().orderBy(condition, isAsc, column, columns);
  210. return typedThis;
  211. }
  212. @Override
  213. public Children orderBy(boolean condition, boolean isAsc, R column) {
  214. getWrapper().orderBy(condition, isAsc, column);
  215. return typedThis;
  216. }
  217. @Override
  218. public Children orderBy(boolean condition, boolean isAsc, List<R> columns) {
  219. getWrapper().orderBy(condition, isAsc, columns);
  220. return typedThis;
  221. }
  222. @Override
  223. public Children having(boolean condition, String sqlHaving, Object... params) {
  224. getWrapper().having(condition, sqlHaving, params);
  225. return typedThis;
  226. }
  227. @Override
  228. public Children func(boolean condition, Consumer<Children> consumer) {
  229. if (condition) {
  230. consumer.accept(typedThis);
  231. }
  232. return typedThis;
  233. }
  234. @Override
  235. public Children or(boolean condition) {
  236. getWrapper().or(condition);
  237. return typedThis;
  238. }
  239. @Override
  240. public Children apply(boolean condition, String applySql, Object... values) {
  241. getWrapper().apply(condition, applySql, values);
  242. return typedThis;
  243. }
  244. @Override
  245. public Children last(boolean condition, String lastSql) {
  246. getWrapper().last(condition, lastSql);
  247. return typedThis;
  248. }
  249. @Override
  250. public Children comment(boolean condition, String comment) {
  251. getWrapper().comment(condition, comment);
  252. return typedThis;
  253. }
  254. @Override
  255. public Children first(boolean condition, String firstSql) {
  256. getWrapper().first(condition, firstSql);
  257. return typedThis;
  258. }
  259. @Override
  260. public Children exists(boolean condition, String existsSql, Object... values) {
  261. getWrapper().exists(condition, existsSql, values);
  262. return typedThis;
  263. }
  264. @Override
  265. public Children notExists(boolean condition, String existsSql, Object... values) {
  266. getWrapper().notExists(condition, existsSql, values);
  267. return typedThis;
  268. }
  269. @Override
  270. public Children and(boolean condition, Consumer<Param> consumer) {
  271. getWrapper().and(condition, consumer);
  272. return typedThis;
  273. }
  274. @Override
  275. public Children or(boolean condition, Consumer<Param> consumer) {
  276. getWrapper().or(condition, consumer);
  277. return typedThis;
  278. }
  279. @Override
  280. public Children nested(boolean condition, Consumer<Param> consumer) {
  281. getWrapper().nested(condition, consumer);
  282. return typedThis;
  283. }
  284. @Override
  285. public Children not(boolean condition, Consumer<Param> consumer) {
  286. getWrapper().not(condition, consumer);
  287. return typedThis;
  288. }
  289. @Override
  290. public String getSqlSegment() {
  291. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSegment");
  292. }
  293. @Override
  294. public String getSqlFirst() {
  295. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlFirst");
  296. }
  297. @Override
  298. public String getSqlSelect() {
  299. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSelect");
  300. }
  301. @Override
  302. public String getSqlSet() {
  303. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSet");
  304. }
  305. @Override
  306. public String getSqlComment() {
  307. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlComment");
  308. }
  309. @Override
  310. public String getTargetSql() {
  311. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getTargetSql");
  312. }
  313. @Override
  314. public T getEntity() {
  315. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getEntity");
  316. }
  317. @Override
  318. public MergeSegments getExpression() {
  319. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getExpression");
  320. }
  321. @Override
  322. public String getCustomSqlSegment() {
  323. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getCustomSqlSegment");
  324. }
  325. @Override
  326. public void clear() {
  327. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "clear");
  328. }
  329. @Override
  330. protected Object clone() throws CloneNotSupportedException {
  331. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "clone");
  332. }
  333. }