IService.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Copyright (c) 2011-2023, 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.*;
  36. import java.util.function.Function;
  37. import java.util.stream.Collectors;
  38. /**
  39. * 顶级 Service
  40. *
  41. * @author hubin
  42. * @since 2018-06-23
  43. */
  44. public interface IService<T> {
  45. /**
  46. * 默认批次提交数量
  47. */
  48. int DEFAULT_BATCH_SIZE = 1000;
  49. /**
  50. * 插入一条记录(选择字段,策略插入)
  51. *
  52. * @param entity 实体对象
  53. */
  54. default boolean save(T entity) {
  55. return SqlHelper.retBool(getBaseMapper().insert(entity));
  56. }
  57. /**
  58. * 插入(批量)
  59. *
  60. * @param entityList 实体对象集合
  61. */
  62. @Transactional(rollbackFor = Exception.class)
  63. default boolean saveBatch(Collection<T> entityList) {
  64. return saveBatch(entityList, DEFAULT_BATCH_SIZE);
  65. }
  66. /**
  67. * 插入(批量)
  68. *
  69. * @param entityList 实体对象集合
  70. * @param batchSize 插入批次数量
  71. */
  72. boolean saveBatch(Collection<T> entityList, int batchSize);
  73. /**
  74. * 批量修改插入
  75. *
  76. * @param entityList 实体对象集合
  77. */
  78. @Transactional(rollbackFor = Exception.class)
  79. default boolean saveOrUpdateBatch(Collection<T> entityList) {
  80. return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);
  81. }
  82. /**
  83. * 批量修改插入
  84. *
  85. * @param entityList 实体对象集合
  86. * @param batchSize 每次的数量
  87. */
  88. boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
  89. /**
  90. * 根据 ID 删除
  91. *
  92. * @param id 主键ID
  93. */
  94. default boolean removeById(Serializable id) {
  95. return SqlHelper.retBool(getBaseMapper().deleteById(id));
  96. }
  97. /**
  98. * 根据 ID 删除
  99. *
  100. * @param id 主键(类型必须与实体类型字段保持一致)
  101. * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
  102. * @return 删除结果
  103. * @since 3.5.0
  104. */
  105. default boolean removeById(Serializable id, boolean useFill) {
  106. throw new UnsupportedOperationException("不支持的方法!");
  107. }
  108. /**
  109. * 根据实体(ID)删除
  110. *
  111. * @param entity 实体
  112. * @since 3.4.4
  113. */
  114. default boolean removeById(T entity) {
  115. return SqlHelper.retBool(getBaseMapper().deleteById(entity));
  116. }
  117. /**
  118. * 根据 columnMap 条件,删除记录
  119. *
  120. * @param columnMap 表字段 map 对象
  121. */
  122. default boolean removeByMap(Map<String, Object> columnMap) {
  123. Assert.notEmpty(columnMap, "error: columnMap must not be empty");
  124. return SqlHelper.retBool(getBaseMapper().deleteByMap(columnMap));
  125. }
  126. /**
  127. * 根据 entity 条件,删除记录
  128. *
  129. * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  130. */
  131. default boolean remove(Wrapper<T> queryWrapper) {
  132. return SqlHelper.retBool(getBaseMapper().delete(queryWrapper));
  133. }
  134. /**
  135. * 删除(根据ID 批量删除)
  136. *
  137. * @param list 主键ID或实体列表
  138. */
  139. default boolean removeByIds(Collection<?> list) {
  140. if (CollectionUtils.isEmpty(list)) {
  141. return false;
  142. }
  143. return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
  144. }
  145. /**
  146. * 批量删除
  147. *
  148. * @param list 主键ID或实体列表
  149. * @param useFill 是否填充(为true的情况,会将入参转换实体进行delete删除)
  150. * @return 删除结果
  151. * @since 3.5.0
  152. */
  153. @Transactional(rollbackFor = Exception.class)
  154. default boolean removeByIds(Collection<?> list, boolean useFill) {
  155. if (CollectionUtils.isEmpty(list)) {
  156. return false;
  157. }
  158. if (useFill) {
  159. return removeBatchByIds(list, true);
  160. }
  161. return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
  162. }
  163. /**
  164. * 批量删除(jdbc批量提交)
  165. *
  166. * @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
  167. * @return 删除结果
  168. * @since 3.5.0
  169. */
  170. @Transactional(rollbackFor = Exception.class)
  171. default boolean removeBatchByIds(Collection<?> list) {
  172. return removeBatchByIds(list, DEFAULT_BATCH_SIZE);
  173. }
  174. /**
  175. * 批量删除(jdbc批量提交)
  176. *
  177. * @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
  178. * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
  179. * @return 删除结果
  180. * @since 3.5.0
  181. */
  182. @Transactional(rollbackFor = Exception.class)
  183. default boolean removeBatchByIds(Collection<?> list, boolean useFill) {
  184. return removeBatchByIds(list, DEFAULT_BATCH_SIZE, useFill);
  185. }
  186. /**
  187. * 批量删除(jdbc批量提交)
  188. *
  189. * @param list 主键ID或实体列表
  190. * @param batchSize 批次大小
  191. * @return 删除结果
  192. * @see #removeBatchByIds(Collection, boolean)
  193. * @since 3.5.0
  194. * @deprecated 3.5.7
  195. */
  196. @Deprecated
  197. default boolean removeBatchByIds(Collection<?> list, int batchSize) {
  198. throw new UnsupportedOperationException("不支持的方法!");
  199. }
  200. /**
  201. * 批量删除(jdbc批量提交)
  202. *
  203. * @param list 主键ID或实体列表
  204. * @param batchSize 批次大小
  205. * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
  206. * @return 删除结果
  207. * @see #removeBatchByIds(Collection, boolean)
  208. * @since 3.5.0
  209. * @deprecated 3.5.7
  210. */
  211. @Deprecated
  212. default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
  213. throw new UnsupportedOperationException("不支持的方法!");
  214. }
  215. /**
  216. * 根据 ID 选择修改
  217. *
  218. * @param entity 实体对象
  219. */
  220. default boolean updateById(T entity) {
  221. return SqlHelper.retBool(getBaseMapper().updateById(entity));
  222. }
  223. /**
  224. * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
  225. * <p>此方法无法进行自动填充,如需自动填充请使用{@link #update(Object, Wrapper)}</p>
  226. *
  227. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  228. */
  229. default boolean update(Wrapper<T> updateWrapper) {
  230. return update(null, updateWrapper);
  231. }
  232. /**
  233. * 根据 whereEntity 条件,更新记录
  234. *
  235. * @param entity 实体对象(当entity为空时无法进行自动填充)
  236. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  237. */
  238. default boolean update(T entity, Wrapper<T> updateWrapper) {
  239. return SqlHelper.retBool(getBaseMapper().update(entity, updateWrapper));
  240. }
  241. /**
  242. * 根据ID 批量更新
  243. *
  244. * @param entityList 实体对象集合
  245. */
  246. @Transactional(rollbackFor = Exception.class)
  247. default boolean updateBatchById(Collection<T> entityList) {
  248. return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
  249. }
  250. /**
  251. * 根据ID 批量更新
  252. *
  253. * @param entityList 实体对象集合
  254. * @param batchSize 更新批次数量
  255. */
  256. boolean updateBatchById(Collection<T> entityList, int batchSize);
  257. /**
  258. * TableId 注解存在更新记录,否插入一条记录
  259. *
  260. * @param entity 实体对象
  261. */
  262. boolean saveOrUpdate(T entity);
  263. /**
  264. * 根据 ID 查询
  265. *
  266. * @param id 主键ID
  267. */
  268. default T getById(Serializable id) {
  269. return getBaseMapper().selectById(id);
  270. }
  271. /**
  272. * 根据 ID 查询,返回一个Option对象
  273. *
  274. * @param id 主键ID
  275. * @return {@link Optional}
  276. */
  277. default Optional<T> getOptById(Serializable id) {
  278. return Optional.ofNullable(getBaseMapper().selectById(id));
  279. }
  280. /**
  281. * 查询(根据ID 批量查询)
  282. *
  283. * @param idList 主键ID列表
  284. */
  285. default List<T> listByIds(Collection<? extends Serializable> idList) {
  286. return getBaseMapper().selectBatchIds(idList);
  287. }
  288. /**
  289. * 查询(根据 columnMap 条件)
  290. *
  291. * @param columnMap 表字段 map 对象
  292. */
  293. default List<T> listByMap(Map<String, Object> columnMap) {
  294. return getBaseMapper().selectByMap(columnMap);
  295. }
  296. /**
  297. * 根据 Wrapper,查询一条记录 <br/>
  298. * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
  299. *
  300. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  301. */
  302. default T getOne(Wrapper<T> queryWrapper) {
  303. return getOne(queryWrapper, true);
  304. }
  305. /**
  306. * 根据 Wrapper,查询一条记录 <br/>
  307. * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
  308. *
  309. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  310. * @return {@link Optional} 返回一个Optional对象
  311. */
  312. default Optional<T> getOneOpt(Wrapper<T> queryWrapper) {
  313. return getOneOpt(queryWrapper, true);
  314. }
  315. /**
  316. * 根据 Wrapper,查询一条记录
  317. *
  318. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  319. * @param throwEx 有多个 result 是否抛出异常
  320. */
  321. T getOne(Wrapper<T> queryWrapper, boolean throwEx);
  322. /**
  323. * 根据 Wrapper,查询一条记录
  324. *
  325. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  326. * @param throwEx 有多个 result 是否抛出异常
  327. * @return {@link Optional} 返回一个Optional对象
  328. */
  329. Optional<T> getOneOpt(Wrapper<T> queryWrapper, boolean throwEx);
  330. /**
  331. * 根据 Wrapper,查询一条记录
  332. *
  333. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  334. */
  335. Map<String, Object> getMap(Wrapper<T> queryWrapper);
  336. /**
  337. * 根据 Wrapper,查询一条记录
  338. *
  339. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  340. * @param mapper 转换函数
  341. */
  342. <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
  343. /**
  344. * 查询指定条件是否存在数据
  345. *
  346. * @see Wrappers#emptyWrapper()
  347. */
  348. default boolean exists(Wrapper<T> queryWrapper) {
  349. return getBaseMapper().exists(queryWrapper);
  350. }
  351. /**
  352. * 查询总记录数
  353. *
  354. * @see Wrappers#emptyWrapper()
  355. */
  356. default long count() {
  357. return count(Wrappers.emptyWrapper());
  358. }
  359. /**
  360. * 根据 Wrapper 条件,查询总记录数
  361. *
  362. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  363. */
  364. default long count(Wrapper<T> queryWrapper) {
  365. return SqlHelper.retCount(getBaseMapper().selectCount(queryWrapper));
  366. }
  367. /**
  368. * 查询列表
  369. *
  370. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  371. */
  372. default List<T> list(Wrapper<T> queryWrapper) {
  373. return getBaseMapper().selectList(queryWrapper);
  374. }
  375. /**
  376. * 查询列表
  377. *
  378. * @param page 分页条件
  379. * @param queryWrapper queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  380. * @return 列表数据
  381. * @since 3.5.3.2
  382. */
  383. default List<T> list(IPage<T> page, Wrapper<T> queryWrapper) {
  384. return getBaseMapper().selectList(page, queryWrapper);
  385. }
  386. /**
  387. * 查询所有
  388. *
  389. * @see Wrappers#emptyWrapper()
  390. */
  391. default List<T> list() {
  392. return list(Wrappers.emptyWrapper());
  393. }
  394. /**
  395. * 分页查询单表数据
  396. *
  397. * @param page 分页条件
  398. * @return 列表数据
  399. * @since 3.5.3.2
  400. */
  401. default List<T> list(IPage<T> page) {
  402. return list(page, Wrappers.emptyWrapper());
  403. }
  404. /**
  405. * 翻页查询
  406. *
  407. * @param page 翻页对象
  408. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  409. */
  410. default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
  411. return getBaseMapper().selectPage(page, queryWrapper);
  412. }
  413. /**
  414. * 无条件翻页查询
  415. *
  416. * @param page 翻页对象
  417. * @see Wrappers#emptyWrapper()
  418. */
  419. default <E extends IPage<T>> E page(E page) {
  420. return page(page, Wrappers.emptyWrapper());
  421. }
  422. /**
  423. * 查询列表
  424. *
  425. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  426. */
  427. default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
  428. return getBaseMapper().selectMaps(queryWrapper);
  429. }
  430. /**
  431. * 查询列表
  432. *
  433. * @param page 分页条件
  434. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  435. * @return 列表数据
  436. * @since 3.5.3.2
  437. */
  438. default List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page, Wrapper<T> queryWrapper) {
  439. return getBaseMapper().selectMaps(page, queryWrapper);
  440. }
  441. /**
  442. * 查询所有列表
  443. *
  444. * @see Wrappers#emptyWrapper()
  445. */
  446. default List<Map<String, Object>> listMaps() {
  447. return listMaps(Wrappers.emptyWrapper());
  448. }
  449. /**
  450. * 查询列表
  451. *
  452. * @param page 分页条件
  453. * @see Wrappers#emptyWrapper()
  454. */
  455. default List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page) {
  456. return listMaps(page, Wrappers.emptyWrapper());
  457. }
  458. /**
  459. * 查询全部记录
  460. */
  461. default <E> List<E> listObjs() {
  462. return getBaseMapper().selectObjs(null);
  463. }
  464. /**
  465. * 查询全部记录
  466. *
  467. * @param mapper 转换函数
  468. */
  469. default <V> List<V> listObjs(Function<? super Object, V> mapper) {
  470. return listObjs(Wrappers.emptyWrapper(), mapper);
  471. }
  472. /**
  473. * 根据 Wrapper 条件,查询全部记录
  474. *
  475. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  476. */
  477. default <E> List<E> listObjs(Wrapper<T> queryWrapper) {
  478. return getBaseMapper().selectObjs(queryWrapper);
  479. }
  480. /**
  481. * 根据 Wrapper 条件,查询全部记录
  482. *
  483. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  484. * @param mapper 转换函数
  485. */
  486. default <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
  487. return getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
  488. }
  489. /**
  490. * 翻页查询
  491. *
  492. * @param page 翻页对象
  493. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  494. */
  495. default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
  496. return getBaseMapper().selectMapsPage(page, queryWrapper);
  497. }
  498. /**
  499. * 无条件翻页查询
  500. *
  501. * @param page 翻页对象
  502. * @see Wrappers#emptyWrapper()
  503. */
  504. default <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
  505. return pageMaps(page, Wrappers.emptyWrapper());
  506. }
  507. /**
  508. * 获取对应 entity 的 BaseMapper
  509. *
  510. * @return BaseMapper
  511. */
  512. BaseMapper<T> getBaseMapper();
  513. /**
  514. * 获取 entity 的 class
  515. *
  516. * @return {@link Class<T>}
  517. */
  518. Class<T> getEntityClass();
  519. /**
  520. * 以下的方法使用介绍:
  521. *
  522. * 一. 名称介绍
  523. * 1. 方法名带有 query 的为对数据的查询操作, 方法名带有 update 的为对数据的修改操作
  524. * 2. 方法名带有 lambda 的为内部方法入参 column 支持函数式的
  525. * 二. 支持介绍
  526. *
  527. * 1. 方法名带有 query 的支持以 {@link ChainQuery} 内部的方法名结尾进行数据查询操作
  528. * 2. 方法名带有 update 的支持以 {@link ChainUpdate} 内部的方法名为结尾进行数据修改操作
  529. *
  530. * 三. 使用示例,只用不带 lambda 的方法各展示一个例子,其他类推
  531. * 1. 根据条件获取一条数据: `query().eq("column", value).one()`
  532. * 2. 根据条件删除一条数据: `update().eq("column", value).remove()`
  533. *
  534. */
  535. /**
  536. * 链式查询 普通
  537. *
  538. * @return QueryWrapper 的包装类
  539. */
  540. default QueryChainWrapper<T> query() {
  541. return ChainWrappers.queryChain(getBaseMapper());
  542. }
  543. /**
  544. * 链式查询 lambda 式
  545. * <p>注意:不支持 Kotlin </p>
  546. *
  547. * @return LambdaQueryWrapper 的包装类
  548. */
  549. default LambdaQueryChainWrapper<T> lambdaQuery() {
  550. return ChainWrappers.lambdaQueryChain(getBaseMapper(), getEntityClass());
  551. }
  552. /**
  553. * 链式查询 lambda 式
  554. * <p>注意:不支持 Kotlin </p>
  555. *
  556. * @param entity 实体对象
  557. * @return LambdaQueryWrapper 的包装类
  558. */
  559. default LambdaQueryChainWrapper<T> lambdaQuery(T entity) {
  560. return ChainWrappers.lambdaQueryChain(getBaseMapper(), entity);
  561. }
  562. /**
  563. * 链式查询 lambda 式
  564. * kotlin 使用
  565. *
  566. * @return KtQueryWrapper 的包装类
  567. */
  568. default KtQueryChainWrapper<T> ktQuery() {
  569. return ChainWrappers.ktQueryChain(getBaseMapper(), getEntityClass());
  570. }
  571. /**
  572. * 链式查询 lambda 式
  573. * kotlin 使用
  574. *
  575. * @return KtQueryWrapper 的包装类
  576. */
  577. default KtUpdateChainWrapper<T> ktUpdate() {
  578. return ChainWrappers.ktUpdateChain(getBaseMapper(), getEntityClass());
  579. }
  580. /**
  581. * 链式更改 普通
  582. *
  583. * @return UpdateWrapper 的包装类
  584. */
  585. default UpdateChainWrapper<T> update() {
  586. return ChainWrappers.updateChain(getBaseMapper());
  587. }
  588. /**
  589. * 链式更改 lambda 式
  590. * <p>注意:不支持 Kotlin </p>
  591. *
  592. * @return LambdaUpdateWrapper 的包装类
  593. */
  594. default LambdaUpdateChainWrapper<T> lambdaUpdate() {
  595. return ChainWrappers.lambdaUpdateChain(getBaseMapper());
  596. }
  597. /**
  598. * <p>
  599. * 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
  600. * 此次修改主要是减少了此项业务代码的代码量(存在性验证之后的saveOrUpdate操作)
  601. * </p>
  602. * <p>
  603. * 该方法不推荐在多线程并发下使用,并发可能存在间隙锁的问题,可以采用先查询后判断是否更新或保存。
  604. * </p>
  605. * <p>
  606. * 该方法存在安全隐患将在后续大版本删除
  607. * </p>
  608. *
  609. * @param entity 实体对象
  610. */
  611. @Deprecated
  612. default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
  613. return update(entity, updateWrapper) || saveOrUpdate(entity);
  614. }
  615. }