IService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (c) 2011-2019, hubin (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.service.additional.query.ChainQuery;
  22. import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
  23. import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
  24. import com.baomidou.mybatisplus.extension.service.additional.update.ChainUpdate;
  25. import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
  26. import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
  27. import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import java.io.Serializable;
  30. import java.util.Collection;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.function.Function;
  34. /**
  35. * 顶级 Service
  36. *
  37. * @author hubin
  38. * @since 2018-06-23
  39. */
  40. public interface IService<T> {
  41. /**
  42. * 插入一条记录(选择字段,策略插入)
  43. *
  44. * @param entity 实体对象
  45. */
  46. boolean save(T entity);
  47. /**
  48. * 插入(批量)
  49. *
  50. * @param entityList 实体对象集合
  51. */
  52. @Transactional(rollbackFor = Exception.class)
  53. default boolean saveBatch(Collection<T> entityList) {
  54. return saveBatch(entityList, 1000);
  55. }
  56. /**
  57. * 插入(批量)
  58. *
  59. * @param entityList 实体对象集合
  60. * @param batchSize 插入批次数量
  61. */
  62. boolean saveBatch(Collection<T> entityList, int batchSize);
  63. /**
  64. * 批量修改插入
  65. *
  66. * @param entityList 实体对象集合
  67. */
  68. @Transactional(rollbackFor = Exception.class)
  69. default boolean saveOrUpdateBatch(Collection<T> entityList) {
  70. return saveOrUpdateBatch(entityList, 1000);
  71. }
  72. /**
  73. * 批量修改插入
  74. *
  75. * @param entityList 实体对象集合
  76. * @param batchSize 每次的数量
  77. */
  78. boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
  79. /**
  80. * 根据 ID 删除
  81. *
  82. * @param id 主键ID
  83. */
  84. boolean removeById(Serializable id);
  85. /**
  86. * 根据 columnMap 条件,删除记录
  87. *
  88. * @param columnMap 表字段 map 对象
  89. */
  90. boolean removeByMap(Map<String, Object> columnMap);
  91. /**
  92. * 根据 entity 条件,删除记录
  93. *
  94. * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  95. */
  96. boolean remove(Wrapper<T> queryWrapper);
  97. /**
  98. * 删除(根据ID 批量删除)
  99. *
  100. * @param idList 主键ID列表
  101. */
  102. boolean removeByIds(Collection<? extends Serializable> idList);
  103. /**
  104. * 根据 ID 选择修改
  105. *
  106. * @param entity 实体对象
  107. */
  108. boolean updateById(T entity);
  109. /**
  110. * 根据 whereEntity 条件,更新记录
  111. *
  112. * @param entity 实体对象
  113. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  114. */
  115. boolean update(T entity, Wrapper<T> updateWrapper);
  116. /**
  117. * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
  118. *
  119. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  120. */
  121. default boolean update(Wrapper<T> updateWrapper) {
  122. return update(null, updateWrapper);
  123. }
  124. /**
  125. * 根据ID 批量更新
  126. *
  127. * @param entityList 实体对象集合
  128. */
  129. @Transactional(rollbackFor = Exception.class)
  130. default boolean updateBatchById(Collection<T> entityList) {
  131. return updateBatchById(entityList, 1000);
  132. }
  133. /**
  134. * 根据ID 批量更新
  135. *
  136. * @param entityList 实体对象集合
  137. * @param batchSize 更新批次数量
  138. */
  139. boolean updateBatchById(Collection<T> entityList, int batchSize);
  140. /**
  141. * TableId 注解存在更新记录,否插入一条记录
  142. *
  143. * @param entity 实体对象
  144. */
  145. boolean saveOrUpdate(T entity);
  146. /**
  147. * 根据 ID 查询
  148. *
  149. * @param id 主键ID
  150. */
  151. T getById(Serializable id);
  152. /**
  153. * 查询(根据ID 批量查询)
  154. *
  155. * @param idList 主键ID列表
  156. */
  157. Collection<T> listByIds(Collection<? extends Serializable> idList);
  158. /**
  159. * 查询(根据 columnMap 条件)
  160. *
  161. * @param columnMap 表字段 map 对象
  162. */
  163. Collection<T> listByMap(Map<String, Object> columnMap);
  164. /**
  165. * 根据 Wrapper,查询一条记录 <br/>
  166. * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
  167. *
  168. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  169. */
  170. default T getOne(Wrapper<T> queryWrapper) {
  171. return getOne(queryWrapper, true);
  172. }
  173. /**
  174. * 根据 Wrapper,查询一条记录
  175. *
  176. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  177. * @param throwEx 有多个 result 是否抛出异常
  178. */
  179. T getOne(Wrapper<T> queryWrapper, boolean throwEx);
  180. /**
  181. * 根据 Wrapper,查询一条记录
  182. *
  183. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  184. */
  185. Map<String, Object> getMap(Wrapper<T> queryWrapper);
  186. /**
  187. * 根据 Wrapper,查询一条记录
  188. *
  189. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  190. * @param mapper 转换函数
  191. */
  192. default <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
  193. return SqlHelper.getObject(listObjs(queryWrapper, mapper));
  194. }
  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. IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
  230. /**
  231. * 无条件翻页查询
  232. *
  233. * @param page 翻页对象
  234. * @see Wrappers#emptyWrapper()
  235. */
  236. default IPage<T> page(IPage<T> 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. IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
  289. /**
  290. * 无条件翻页查询
  291. *
  292. * @param page 翻页对象
  293. * @see Wrappers#emptyWrapper()
  294. */
  295. default IPage<Map<String, Object>> pageMaps(IPage<T> page) {
  296. return pageMaps(page, Wrappers.emptyWrapper());
  297. }
  298. /**
  299. * 获取对应 entity 的 BaseMapper
  300. *
  301. * @return BaseMapper
  302. */
  303. BaseMapper<T> getBaseMapper();
  304. /**
  305. * 链式查询 普通
  306. * 支持 {@link ChainQuery} 内的方法为结尾进行查询
  307. *
  308. * @return QueryWrapper 的包装类
  309. */
  310. default QueryChainWrapper<T> query() {
  311. return new QueryChainWrapper<>(getBaseMapper());
  312. }
  313. /**
  314. * 链式查询 lambda 式
  315. * <p>注意:不支持 Kotlin </p>
  316. *
  317. * @return LambdaQueryWrapper 的包装类
  318. */
  319. default LambdaQueryChainWrapper<T> lambdaQuery() {
  320. return new LambdaQueryChainWrapper<>(getBaseMapper());
  321. }
  322. /**
  323. * 链式更改 普通
  324. * 支持 {@link ChainUpdate} 内的方法为结尾进行数据修改
  325. *
  326. * @return UpdateWrapper 的包装类
  327. */
  328. default UpdateChainWrapper<T> update() {
  329. return new UpdateChainWrapper<>(getBaseMapper());
  330. }
  331. /**
  332. * 链式更改 lambda 式
  333. * <p>注意:不支持 Kotlin </p>
  334. *
  335. * @return LambdaUpdateWrapper 的包装类
  336. */
  337. default LambdaUpdateChainWrapper<T> lambdaUpdate() {
  338. return new LambdaUpdateChainWrapper<>(getBaseMapper());
  339. }
  340. }