AbstractChainWrapper.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2011-2020, baomidou (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. * https://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.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.Map;
  27. import java.util.function.BiPredicate;
  28. import java.util.function.Consumer;
  29. /**
  30. * 所有包装类都继承此抽象类,此抽象类代理了大部分生成 where 条件的方法
  31. * <li> 泛型: Children ,表示子类 </li>
  32. * <li> 泛型: Param ,表示子类所包装的具体 Wrapper 类型 </li>
  33. *
  34. * @author miemie
  35. * @since 2018-12-19
  36. */
  37. @SuppressWarnings({"serial", "unchecked", "rawtypes"})
  38. public abstract class AbstractChainWrapper<T, R, Children extends AbstractChainWrapper<T, R, Children, Param>, Param extends AbstractWrapper>
  39. extends Wrapper<T> implements Compare<Children, R>, Func<Children, R>, Join<Children>, Nested<Param, Children> {
  40. protected final Children typedThis = (Children) this;
  41. /**
  42. * 子类所包装的具体 Wrapper 类型
  43. */
  44. protected Param wrapperChildren;
  45. /**
  46. * 必须的构造函数
  47. */
  48. public AbstractChainWrapper() {
  49. }
  50. public AbstractWrapper getWrapper() {
  51. return wrapperChildren;
  52. }
  53. public Children setEntity(T entity) {
  54. getWrapper().setEntity(entity);
  55. return typedThis;
  56. }
  57. public Children setEntityClass(Class<T> entityClass) {
  58. getWrapper().setEntityClass(entityClass);
  59. return typedThis;
  60. }
  61. @Override
  62. public <V> Children allEq(boolean condition, Map<R, V> params, boolean null2IsNull) {
  63. getWrapper().allEq(condition, params, null2IsNull);
  64. return typedThis;
  65. }
  66. @Override
  67. public <V> Children allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) {
  68. getWrapper().allEq(condition, filter, params, null2IsNull);
  69. return typedThis;
  70. }
  71. @Override
  72. public Children eq(boolean condition, R column, Object val) {
  73. getWrapper().eq(condition, column, val);
  74. return typedThis;
  75. }
  76. @Override
  77. public Children ne(boolean condition, R column, Object val) {
  78. getWrapper().ne(condition, column, val);
  79. return typedThis;
  80. }
  81. @Override
  82. public Children gt(boolean condition, R column, Object val) {
  83. getWrapper().gt(condition, column, val);
  84. return typedThis;
  85. }
  86. @Override
  87. public Children ge(boolean condition, R column, Object val) {
  88. getWrapper().ge(condition, column, val);
  89. return typedThis;
  90. }
  91. @Override
  92. public Children lt(boolean condition, R column, Object val) {
  93. getWrapper().lt(condition, column, val);
  94. return typedThis;
  95. }
  96. @Override
  97. public Children le(boolean condition, R column, Object val) {
  98. getWrapper().le(condition, column, val);
  99. return typedThis;
  100. }
  101. @Override
  102. public Children between(boolean condition, R column, Object val1, Object val2) {
  103. getWrapper().between(condition, column, val1, val2);
  104. return typedThis;
  105. }
  106. @Override
  107. public Children notBetween(boolean condition, R column, Object val1, Object val2) {
  108. getWrapper().notBetween(condition, column, val1, val2);
  109. return typedThis;
  110. }
  111. @Override
  112. public Children like(boolean condition, R column, Object val) {
  113. getWrapper().like(condition, column, val);
  114. return typedThis;
  115. }
  116. @Override
  117. public Children notLike(boolean condition, R column, Object val) {
  118. getWrapper().notLike(condition, column, val);
  119. return typedThis;
  120. }
  121. @Override
  122. public Children likeLeft(boolean condition, R column, Object val) {
  123. getWrapper().likeLeft(condition, column, val);
  124. return typedThis;
  125. }
  126. @Override
  127. public Children likeRight(boolean condition, R column, Object val) {
  128. getWrapper().likeRight(condition, column, val);
  129. return typedThis;
  130. }
  131. @Override
  132. public Children isNull(boolean condition, R column) {
  133. getWrapper().isNull(condition, column);
  134. return typedThis;
  135. }
  136. @Override
  137. public Children isNotNull(boolean condition, R column) {
  138. getWrapper().isNotNull(condition, column);
  139. return typedThis;
  140. }
  141. @Override
  142. public Children in(boolean condition, R column, Collection<?> coll) {
  143. getWrapper().in(condition, column, coll);
  144. return typedThis;
  145. }
  146. @Override
  147. public Children notIn(boolean condition, R column, Collection<?> coll) {
  148. getWrapper().notIn(condition, column, coll);
  149. return typedThis;
  150. }
  151. @Override
  152. public Children inSql(boolean condition, R column, String inValue) {
  153. getWrapper().inSql(condition, column, inValue);
  154. return typedThis;
  155. }
  156. @Override
  157. public Children notInSql(boolean condition, R column, String inValue) {
  158. getWrapper().notInSql(condition, column, inValue);
  159. return typedThis;
  160. }
  161. @Override
  162. public Children groupBy(boolean condition, R... columns) {
  163. getWrapper().groupBy(condition, columns);
  164. return typedThis;
  165. }
  166. @Override
  167. public Children orderBy(boolean condition, boolean isAsc, R... columns) {
  168. getWrapper().orderBy(condition, isAsc, columns);
  169. return typedThis;
  170. }
  171. @Override
  172. public Children having(boolean condition, String sqlHaving, Object... params) {
  173. getWrapper().having(condition, sqlHaving, params);
  174. return typedThis;
  175. }
  176. @Override
  177. public Children func(boolean condition, Consumer<Children> consumer) {
  178. if (condition) {
  179. consumer.accept(typedThis);
  180. }
  181. return typedThis;
  182. }
  183. @Override
  184. public Children or(boolean condition) {
  185. getWrapper().or(condition);
  186. return typedThis;
  187. }
  188. @Override
  189. public Children apply(boolean condition, String applySql, Object... value) {
  190. getWrapper().apply(condition, applySql, value);
  191. return typedThis;
  192. }
  193. @Override
  194. public Children last(boolean condition, String lastSql) {
  195. getWrapper().last(condition, lastSql);
  196. return typedThis;
  197. }
  198. @Override
  199. public Children comment(boolean condition, String comment) {
  200. getWrapper().comment(condition, comment);
  201. return typedThis;
  202. }
  203. @Override
  204. public Children first(boolean condition, String firstSql) {
  205. getWrapper().first(condition, firstSql);
  206. return typedThis;
  207. }
  208. @Override
  209. public Children exists(boolean condition, String existsSql) {
  210. getWrapper().exists(condition, existsSql);
  211. return typedThis;
  212. }
  213. @Override
  214. public Children notExists(boolean condition, String existsSql) {
  215. getWrapper().notExists(condition, existsSql);
  216. return typedThis;
  217. }
  218. @Override
  219. public Children and(boolean condition, Consumer<Param> consumer) {
  220. getWrapper().and(condition, consumer);
  221. return typedThis;
  222. }
  223. @Override
  224. public Children or(boolean condition, Consumer<Param> consumer) {
  225. getWrapper().or(condition, consumer);
  226. return typedThis;
  227. }
  228. @Override
  229. public Children nested(boolean condition, Consumer<Param> consumer) {
  230. getWrapper().nested(condition, consumer);
  231. return typedThis;
  232. }
  233. @Override
  234. public Children not(boolean condition, Consumer<Param> consumer) {
  235. getWrapper().not(condition, consumer);
  236. return typedThis;
  237. }
  238. @Override
  239. public String getSqlSegment() {
  240. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSegment");
  241. }
  242. @Override
  243. public String getSqlFirst() {
  244. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlFirst");
  245. }
  246. @Override
  247. public String getSqlSelect() {
  248. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSelect");
  249. }
  250. @Override
  251. public String getSqlSet() {
  252. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSet");
  253. }
  254. @Override
  255. public String getSqlComment() {
  256. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlComment");
  257. }
  258. @Override
  259. public String getTargetSql() {
  260. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getTargetSql");
  261. }
  262. @Override
  263. public T getEntity() {
  264. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getEntity");
  265. }
  266. @Override
  267. public MergeSegments getExpression() {
  268. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getExpression");
  269. }
  270. @Override
  271. public String getCustomSqlSegment() {
  272. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getCustomSqlSegment");
  273. }
  274. @Override
  275. public void clear() {
  276. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "clear");
  277. }
  278. @Override
  279. protected Object clone() throws CloneNotSupportedException {
  280. throw ExceptionUtils.mpe("can not use this method for \"%s\"", "clone");
  281. }
  282. }