IService.java 15 KB

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