IService.java 12 KB

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