IService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.service;
  17. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  18. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  19. import com.baomidou.mybatisplus.core.metadata.IPage;
  20. import com.baomidou.mybatisplus.core.toolkit.Assert;
  21. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  22. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  23. import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
  24. import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
  25. import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
  26. import com.baomidou.mybatisplus.extension.conditions.update.ChainUpdate;
  27. import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
  28. import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
  29. import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper;
  30. import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper;
  31. import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
  32. import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
  33. import org.springframework.transaction.annotation.Transactional;
  34. import java.io.Serializable;
  35. import java.util.Collection;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.Objects;
  39. import java.util.function.Function;
  40. import java.util.stream.Collectors;
  41. /**
  42. * 顶级 Service
  43. *
  44. * @author hubin
  45. * @since 2018-06-23
  46. */
  47. public interface IService<T> {
  48. /**
  49. * 默认批次提交数量
  50. */
  51. int DEFAULT_BATCH_SIZE = 1000;
  52. /**
  53. * 插入一条记录(选择字段,策略插入)
  54. *
  55. * @param entity 实体对象
  56. */
  57. default boolean save(T entity) {
  58. return SqlHelper.retBool(getBaseMapper().insert(entity));
  59. }
  60. /**
  61. * 插入(批量)
  62. *
  63. * @param entityList 实体对象集合
  64. */
  65. @Transactional(rollbackFor = Exception.class)
  66. default boolean saveBatch(Collection<T> entityList) {
  67. return saveBatch(entityList, DEFAULT_BATCH_SIZE);
  68. }
  69. /**
  70. * 插入(批量)
  71. *
  72. * @param entityList 实体对象集合
  73. * @param batchSize 插入批次数量
  74. */
  75. boolean saveBatch(Collection<T> entityList, int batchSize);
  76. /**
  77. * 批量修改插入
  78. *
  79. * @param entityList 实体对象集合
  80. */
  81. @Transactional(rollbackFor = Exception.class)
  82. default boolean saveOrUpdateBatch(Collection<T> entityList) {
  83. return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);
  84. }
  85. /**
  86. * 批量修改插入
  87. *
  88. * @param entityList 实体对象集合
  89. * @param batchSize 每次的数量
  90. */
  91. boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
  92. /**
  93. * 根据 ID 删除
  94. *
  95. * @param id 主键ID
  96. */
  97. default boolean removeById(Serializable id) {
  98. return SqlHelper.retBool(getBaseMapper().deleteById(id));
  99. }
  100. /**
  101. * 根据 columnMap 条件,删除记录
  102. *
  103. * @param columnMap 表字段 map 对象
  104. */
  105. default boolean removeByMap(Map<String, Object> columnMap) {
  106. Assert.notEmpty(columnMap, "error: columnMap must not be empty");
  107. return SqlHelper.retBool(getBaseMapper().deleteByMap(columnMap));
  108. }
  109. /**
  110. * 根据 entity 条件,删除记录
  111. *
  112. * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  113. */
  114. default boolean remove(Wrapper<T> queryWrapper) {
  115. return SqlHelper.retBool(getBaseMapper().delete(queryWrapper));
  116. }
  117. /**
  118. * 删除(根据ID 批量删除)
  119. *
  120. * @param idList 主键ID列表
  121. */
  122. default boolean removeByIds(Collection<? extends Serializable> idList) {
  123. if (CollectionUtils.isEmpty(idList)) {
  124. return false;
  125. }
  126. return SqlHelper.retBool(getBaseMapper().deleteBatchIds(idList));
  127. }
  128. /**
  129. * 根据 ID 选择修改
  130. *
  131. * @param entity 实体对象
  132. */
  133. default boolean updateById(T entity) {
  134. return SqlHelper.retBool(getBaseMapper().updateById(entity));
  135. }
  136. /**
  137. * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
  138. *
  139. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  140. */
  141. default boolean update(Wrapper<T> updateWrapper) {
  142. return update(null, updateWrapper);
  143. }
  144. /**
  145. * 根据 whereEntity 条件,更新记录
  146. *
  147. * @param entity 实体对象
  148. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  149. */
  150. default boolean update(T entity, Wrapper<T> updateWrapper) {
  151. return SqlHelper.retBool(getBaseMapper().update(entity, updateWrapper));
  152. }
  153. /**
  154. * 根据ID 批量更新
  155. *
  156. * @param entityList 实体对象集合
  157. */
  158. @Transactional(rollbackFor = Exception.class)
  159. default boolean updateBatchById(Collection<T> entityList) {
  160. return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
  161. }
  162. /**
  163. * 根据ID 批量更新
  164. *
  165. * @param entityList 实体对象集合
  166. * @param batchSize 更新批次数量
  167. */
  168. boolean updateBatchById(Collection<T> entityList, int batchSize);
  169. /**
  170. * TableId 注解存在更新记录,否插入一条记录
  171. *
  172. * @param entity 实体对象
  173. */
  174. boolean saveOrUpdate(T entity);
  175. /**
  176. * 根据 ID 查询
  177. *
  178. * @param id 主键ID
  179. */
  180. default T getById(Serializable id) {
  181. return getBaseMapper().selectById(id);
  182. }
  183. /**
  184. * 查询(根据ID 批量查询)
  185. *
  186. * @param idList 主键ID列表
  187. */
  188. default List<T> listByIds(Collection<? extends Serializable> idList) {
  189. return getBaseMapper().selectBatchIds(idList);
  190. }
  191. /**
  192. * 查询(根据 columnMap 条件)
  193. *
  194. * @param columnMap 表字段 map 对象
  195. */
  196. default List<T> listByMap(Map<String, Object> columnMap) {
  197. return getBaseMapper().selectByMap(columnMap);
  198. }
  199. /**
  200. * 根据 Wrapper,查询一条记录 <br/>
  201. * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
  202. *
  203. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  204. */
  205. default T getOne(Wrapper<T> queryWrapper) {
  206. return getOne(queryWrapper, true);
  207. }
  208. /**
  209. * 根据 Wrapper,查询一条记录
  210. *
  211. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  212. * @param throwEx 有多个 result 是否抛出异常
  213. */
  214. T getOne(Wrapper<T> queryWrapper, boolean throwEx);
  215. /**
  216. * 根据 Wrapper,查询一条记录
  217. *
  218. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  219. */
  220. Map<String, Object> getMap(Wrapper<T> queryWrapper);
  221. /**
  222. * 根据 Wrapper,查询一条记录
  223. *
  224. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  225. * @param mapper 转换函数
  226. */
  227. <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
  228. /**
  229. * 查询总记录数
  230. *
  231. * @see Wrappers#emptyWrapper()
  232. */
  233. default int count() {
  234. return count(Wrappers.emptyWrapper());
  235. }
  236. /**
  237. * 根据 Wrapper 条件,查询总记录数
  238. *
  239. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  240. */
  241. default int count(Wrapper<T> queryWrapper) {
  242. return SqlHelper.retCount(getBaseMapper().selectCount(queryWrapper));
  243. }
  244. /**
  245. * 查询列表
  246. *
  247. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  248. */
  249. default List<T> list(Wrapper<T> queryWrapper) {
  250. return getBaseMapper().selectList(queryWrapper);
  251. }
  252. /**
  253. * 查询所有
  254. *
  255. * @see Wrappers#emptyWrapper()
  256. */
  257. default List<T> list() {
  258. return list(Wrappers.emptyWrapper());
  259. }
  260. /**
  261. * 翻页查询
  262. *
  263. * @param page 翻页对象
  264. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  265. */
  266. default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
  267. return getBaseMapper().selectPage(page, queryWrapper);
  268. }
  269. /**
  270. * 无条件翻页查询
  271. *
  272. * @param page 翻页对象
  273. * @see Wrappers#emptyWrapper()
  274. */
  275. default <E extends IPage<T>> E page(E page) {
  276. return page(page, Wrappers.emptyWrapper());
  277. }
  278. /**
  279. * 查询列表
  280. *
  281. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  282. */
  283. default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
  284. return getBaseMapper().selectMaps(queryWrapper);
  285. }
  286. /**
  287. * 查询所有列表
  288. *
  289. * @see Wrappers#emptyWrapper()
  290. */
  291. default List<Map<String, Object>> listMaps() {
  292. return listMaps(Wrappers.emptyWrapper());
  293. }
  294. /**
  295. * 查询全部记录
  296. */
  297. default List<Object> listObjs() {
  298. return listObjs(Function.identity());
  299. }
  300. /**
  301. * 查询全部记录
  302. *
  303. * @param mapper 转换函数
  304. */
  305. default <V> List<V> listObjs(Function<? super Object, V> mapper) {
  306. return listObjs(Wrappers.emptyWrapper(), mapper);
  307. }
  308. /**
  309. * 根据 Wrapper 条件,查询全部记录
  310. *
  311. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  312. */
  313. default List<Object> listObjs(Wrapper<T> queryWrapper) {
  314. return listObjs(queryWrapper, Function.identity());
  315. }
  316. /**
  317. * 根据 Wrapper 条件,查询全部记录
  318. *
  319. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  320. * @param mapper 转换函数
  321. */
  322. default <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
  323. return getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
  324. }
  325. /**
  326. * 翻页查询
  327. *
  328. * @param page 翻页对象
  329. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  330. */
  331. default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
  332. return getBaseMapper().selectMapsPage(page, queryWrapper);
  333. }
  334. /**
  335. * 无条件翻页查询
  336. *
  337. * @param page 翻页对象
  338. * @see Wrappers#emptyWrapper()
  339. */
  340. default <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
  341. return pageMaps(page, Wrappers.emptyWrapper());
  342. }
  343. /**
  344. * 获取对应 entity 的 BaseMapper
  345. *
  346. * @return BaseMapper
  347. */
  348. BaseMapper<T> getBaseMapper();
  349. /**
  350. * 获取 entity 的 class
  351. *
  352. * @return {@link Class<T>}
  353. */
  354. Class<T> getEntityClass();
  355. /**
  356. * 以下的方法使用介绍:
  357. *
  358. * 一. 名称介绍
  359. * 1. 方法名带有 query 的为对数据的查询操作, 方法名带有 update 的为对数据的修改操作
  360. * 2. 方法名带有 lambda 的为内部方法入参 column 支持函数式的
  361. * 二. 支持介绍
  362. *
  363. * 1. 方法名带有 query 的支持以 {@link ChainQuery} 内部的方法名结尾进行数据查询操作
  364. * 2. 方法名带有 update 的支持以 {@link ChainUpdate} 内部的方法名为结尾进行数据修改操作
  365. *
  366. * 三. 使用示例,只用不带 lambda 的方法各展示一个例子,其他类推
  367. * 1. 根据条件获取一条数据: `query().eq("column", value).one()`
  368. * 2. 根据条件删除一条数据: `update().eq("column", value).remove()`
  369. *
  370. */
  371. /**
  372. * 链式查询 普通
  373. *
  374. * @return QueryWrapper 的包装类
  375. */
  376. default QueryChainWrapper<T> query() {
  377. return ChainWrappers.queryChain(getBaseMapper());
  378. }
  379. /**
  380. * 链式查询 lambda 式
  381. * <p>注意:不支持 Kotlin </p>
  382. *
  383. * @return LambdaQueryWrapper 的包装类
  384. */
  385. default LambdaQueryChainWrapper<T> lambdaQuery() {
  386. return ChainWrappers.lambdaQueryChain(getBaseMapper());
  387. }
  388. /**
  389. * 链式查询 lambda 式
  390. * kotlin 使用
  391. *
  392. * @return KtQueryWrapper 的包装类
  393. */
  394. default KtQueryChainWrapper<T> ktQuery() {
  395. return ChainWrappers.ktQueryChain(getBaseMapper(), getEntityClass());
  396. }
  397. /**
  398. * 链式查询 lambda 式
  399. * kotlin 使用
  400. *
  401. * @return KtQueryWrapper 的包装类
  402. */
  403. default KtUpdateChainWrapper<T> ktUpdate() {
  404. return ChainWrappers.ktUpdateChain(getBaseMapper(), getEntityClass());
  405. }
  406. /**
  407. * 链式更改 普通
  408. *
  409. * @return UpdateWrapper 的包装类
  410. */
  411. default UpdateChainWrapper<T> update() {
  412. return ChainWrappers.updateChain(getBaseMapper());
  413. }
  414. /**
  415. * 链式更改 lambda 式
  416. * <p>注意:不支持 Kotlin </p>
  417. *
  418. * @return LambdaUpdateWrapper 的包装类
  419. */
  420. default LambdaUpdateChainWrapper<T> lambdaUpdate() {
  421. return ChainWrappers.lambdaUpdateChain(getBaseMapper());
  422. }
  423. /**
  424. * <p>
  425. * 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
  426. * 此次修改主要是减少了此项业务代码的代码量(存在性验证之后的saveOrUpdate操作)
  427. * </p>
  428. *
  429. * @param entity 实体对象
  430. */
  431. default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
  432. return update(entity, updateWrapper) || saveOrUpdate(entity);
  433. }
  434. }