Db.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * Copyright (c) 2011-2024, 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.toolkit;
  17. import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
  18. import com.baomidou.mybatisplus.core.metadata.IPage;
  19. import com.baomidou.mybatisplus.core.metadata.TableInfo;
  20. import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
  21. import com.baomidou.mybatisplus.core.toolkit.Assert;
  22. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  23. import com.baomidou.mybatisplus.core.toolkit.Constants;
  24. import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
  25. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  26. import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
  27. import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
  28. import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
  29. import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
  30. import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
  31. import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper;
  32. import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper;
  33. import org.apache.ibatis.executor.BatchResult;
  34. import org.apache.ibatis.logging.Log;
  35. import org.apache.ibatis.logging.LogFactory;
  36. import java.io.Serializable;
  37. import java.util.*;
  38. import java.util.stream.Collectors;
  39. import java.util.stream.IntStream;
  40. /**
  41. * 以静态方式调用Service中的函数
  42. *
  43. * @author VampireAchao
  44. * @since 2022-05-03
  45. */
  46. public class Db {
  47. private static final Log log = LogFactory.getLog(Db.class);
  48. private Db() {
  49. /* Do not new me! */
  50. }
  51. /**
  52. * 插入一条记录(选择字段,策略插入)
  53. *
  54. * @param entity 实体对象
  55. */
  56. public static <T> boolean save(T entity) {
  57. if (Objects.isNull(entity)) {
  58. return false;
  59. }
  60. Integer result = SqlHelper.execute(getEntityClass(entity), baseMapper -> baseMapper.insert(entity));
  61. return SqlHelper.retBool(result);
  62. }
  63. /**
  64. * 插入(批量)
  65. *
  66. * @param entityList 实体对象集合
  67. */
  68. public static <T> boolean saveBatch(Collection<T> entityList) {
  69. return saveBatch(entityList, Constants.DEFAULT_BATCH_SIZE);
  70. }
  71. /**
  72. * 插入(批量)
  73. *
  74. * @param entityList 实体对象集合
  75. * @param batchSize 插入批次数量
  76. */
  77. public static <T> boolean saveBatch(Collection<T> entityList, int batchSize) {
  78. if (CollectionUtils.isEmpty(entityList)) {
  79. return false;
  80. }
  81. Class<T> entityClass = getEntityClass(entityList);
  82. List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.saveBatch(entityList, batchSize));
  83. return batchResults.stream().flatMapToInt(r -> IntStream.of(r.getUpdateCounts())).allMatch(i -> i > 0);
  84. }
  85. /**
  86. * 批量修改插入
  87. *
  88. * @param entityList 实体对象集合
  89. */
  90. public static <T> boolean saveOrUpdateBatch(Collection<T> entityList) {
  91. return saveOrUpdateBatch(entityList, Constants.DEFAULT_BATCH_SIZE);
  92. }
  93. /**
  94. * 批量修改插入
  95. *
  96. * @param entityList 实体对象集合
  97. * @param batchSize 每次的数量
  98. */
  99. public static <T> boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
  100. if (CollectionUtils.isEmpty(entityList)) {
  101. return false;
  102. }
  103. Class<T> entityClass = getEntityClass(entityList);
  104. List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.saveOrUpdateBatch(entityList, batchSize));
  105. return batchResults.stream().flatMapToInt(r -> IntStream.of(r.getUpdateCounts())).allMatch(i -> i > 0);
  106. }
  107. /**
  108. * 根据 ID 删除
  109. *
  110. * @param id 主键ID
  111. * @param entityClass 实体类
  112. */
  113. public static <T> boolean removeById(Serializable id, Class<T> entityClass) {
  114. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.deleteById(id)));
  115. }
  116. /**
  117. * 根据实体(ID)删除
  118. *
  119. * @param entity 实体
  120. */
  121. public static <T> boolean removeById(T entity) {
  122. if (Objects.isNull(entity)) {
  123. return false;
  124. }
  125. return SqlHelper.execute(getEntityClass(entity), baseMapper -> SqlHelper.retBool(baseMapper.deleteById(entity)));
  126. }
  127. /**
  128. * 根据 entity 条件,删除记录
  129. *
  130. * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  131. */
  132. public static <T> boolean remove(AbstractWrapper<T, ?, ?> queryWrapper) {
  133. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> SqlHelper.retBool(baseMapper.delete(queryWrapper)));
  134. }
  135. /**
  136. * 根据 ID 选择修改
  137. *
  138. * @param entity 实体对象
  139. */
  140. public static <T> boolean updateById(T entity) {
  141. if (Objects.isNull(entity)) {
  142. return false;
  143. }
  144. return SqlHelper.execute(getEntityClass(entity), baseMapper -> SqlHelper.retBool(baseMapper.updateById(entity)));
  145. }
  146. /**
  147. * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
  148. *
  149. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  150. */
  151. public static <T> boolean update(AbstractWrapper<T, ?, ?> updateWrapper) {
  152. return update(null, updateWrapper);
  153. }
  154. /**
  155. * 根据 whereEntity 条件,更新记录
  156. *
  157. * @param entity 实体对象
  158. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  159. */
  160. public static <T> boolean update(T entity, AbstractWrapper<T, ?, ?> updateWrapper) {
  161. return SqlHelper.execute(getEntityClass(updateWrapper), baseMapper -> SqlHelper.retBool(baseMapper.update(entity, updateWrapper)));
  162. }
  163. /**
  164. * 根据ID 批量更新
  165. *
  166. * @param entityList 实体对象集合
  167. */
  168. public static <T> boolean updateBatchById(Collection<T> entityList) {
  169. return updateBatchById(entityList, Constants.DEFAULT_BATCH_SIZE);
  170. }
  171. /**
  172. * 根据ID 批量更新
  173. *
  174. * @param entityList 实体对象集合
  175. * @param batchSize 更新批次数量
  176. */
  177. public static <T> boolean updateBatchById(Collection<T> entityList, int batchSize) {
  178. Class<T> entityClass = getEntityClass(entityList);
  179. List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.updateBatchById(entityList, batchSize));
  180. return batchResults.stream().flatMapToInt(r -> IntStream.of(r.getUpdateCounts())).allMatch(i -> i > 0);
  181. }
  182. /**
  183. * 删除(根据ID 批量删除)
  184. *
  185. * @param list 主键ID或实体列表
  186. * @param entityClass 实体类
  187. */
  188. public static <T> boolean removeByIds(Collection<? extends Serializable> list, Class<T> entityClass) {
  189. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.deleteBatchIds(list)));
  190. }
  191. /**
  192. * 根据 columnMap 条件,删除记录
  193. *
  194. * @param columnMap 表字段 map 对象
  195. * @param entityClass 实体类
  196. */
  197. public static <T> boolean removeByMap(Map<String, Object> columnMap, Class<T> entityClass) {
  198. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.deleteByMap(columnMap)));
  199. }
  200. /**
  201. * TableId 注解存在更新记录,否插入一条记录
  202. *
  203. * @param entity 实体对象
  204. */
  205. public static <T> boolean saveOrUpdate(T entity) {
  206. if (Objects.isNull(entity)) {
  207. return false;
  208. }
  209. return SqlHelper.execute(getEntityClass(entity), baseMapper -> baseMapper.saveOrUpdate(entity));
  210. }
  211. /**
  212. * 根据 ID 查询
  213. *
  214. * @param id 主键ID
  215. * @param entityClass 实体类
  216. */
  217. public static <T> T getById(Serializable id, Class<T> entityClass) {
  218. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectById(id));
  219. }
  220. /**
  221. * 根据 Wrapper,查询一条记录 <br/>
  222. * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
  223. *
  224. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  225. */
  226. public static <T> T getOne(AbstractWrapper<T, ?, ?> queryWrapper) {
  227. return getOne(queryWrapper, true);
  228. }
  229. /**
  230. * 根据 entity里不为空的字段,查询一条记录 <br/>
  231. * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
  232. *
  233. * @param entity 实体对象
  234. */
  235. public static <T> T getOne(T entity) {
  236. return getOne(Wrappers.lambdaQuery(entity), true);
  237. }
  238. /**
  239. * 根据 entity里不为空的字段,查询一条记录
  240. *
  241. * @param entity 实体对象
  242. * @param throwEx 有多个 result 是否抛出异常
  243. */
  244. public static <T> T getOne(T entity, boolean throwEx) {
  245. return getOne(Wrappers.lambdaQuery(entity), throwEx);
  246. }
  247. /**
  248. * 根据 Wrapper,查询一条记录
  249. *
  250. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  251. * @param throwEx 有多个 result 是否抛出异常
  252. */
  253. public static <T> T getOne(AbstractWrapper<T, ?, ?> queryWrapper, boolean throwEx) {
  254. Class<T> entityClass = getEntityClass(queryWrapper);
  255. if (throwEx) {
  256. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectOne(queryWrapper));
  257. }
  258. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.getObject(log, baseMapper.selectList(queryWrapper)));
  259. }
  260. /**
  261. * 查询(根据 columnMap 条件)
  262. *
  263. * @param columnMap 表字段 map 对象
  264. * @param entityClass 实体类
  265. */
  266. public static <T> List<T> listByMap(Map<String, Object> columnMap, Class<T> entityClass) {
  267. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectByMap(columnMap));
  268. }
  269. /**
  270. * 查询(根据ID 批量查询)
  271. *
  272. * @param idList 主键ID列表
  273. * @param entityClass 实体类
  274. */
  275. public static <T> List<T> listByIds(Collection<? extends Serializable> idList, Class<T> entityClass) {
  276. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectBatchIds(idList));
  277. }
  278. /**
  279. * 根据 Wrapper,查询一条记录
  280. *
  281. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  282. */
  283. public static <T> Map<String, Object> getMap(AbstractWrapper<T, ?, ?> queryWrapper) {
  284. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> SqlHelper.getObject(log, baseMapper.selectMaps(queryWrapper)));
  285. }
  286. /**
  287. * 根据 entity不为空条件,查询一条记录
  288. *
  289. * @param entity 实体对象
  290. */
  291. public static <T> Map<String, Object> getMap(T entity) {
  292. return getMap(Wrappers.lambdaQuery(entity));
  293. }
  294. /**
  295. * 查询总记录数
  296. *
  297. * @param entityClass 实体类
  298. * @see Wrappers#emptyWrapper()
  299. */
  300. public static <T> long count(Class<T> entityClass) {
  301. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectCount(null));
  302. }
  303. /**
  304. * 根据entity中不为空的数据查询记录数
  305. *
  306. * @param entity 实体类
  307. */
  308. public static <T> long count(T entity) {
  309. return count(Wrappers.lambdaQuery(entity));
  310. }
  311. /**
  312. * 根据 Wrapper 条件,查询总记录数
  313. *
  314. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  315. */
  316. public static <T> long count(AbstractWrapper<T, ?, ?> queryWrapper) {
  317. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectCount(queryWrapper));
  318. }
  319. /**
  320. * 查询列表
  321. *
  322. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  323. */
  324. public static <T> List<T> list(AbstractWrapper<T, ?, ?> queryWrapper) {
  325. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectList(queryWrapper));
  326. }
  327. /**
  328. * @param page 分页条件
  329. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  330. * @param <T> entity
  331. * @return 列表数据
  332. */
  333. public static <T> List<T> list(IPage<T> page, AbstractWrapper<T, ?, ?> queryWrapper) {
  334. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectList(page, queryWrapper));
  335. }
  336. /**
  337. * 查询所有
  338. *
  339. * @param entityClass 实体类
  340. * @see Wrappers#emptyWrapper()
  341. */
  342. public static <T> List<T> list(Class<T> entityClass) {
  343. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectList(null));
  344. }
  345. /**
  346. * @param page 分页条件
  347. * @param entityClass 实体类
  348. * @param <T> entity
  349. * @return 列表数据
  350. * @since 3.5.3.2
  351. */
  352. public static <T> List<T> list(IPage<T> page, Class<T> entityClass) {
  353. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectList(page, null));
  354. }
  355. /**
  356. * 根据entity中不为空的字段进行查询
  357. *
  358. * @param entity 实体类
  359. * @see Wrappers#emptyWrapper()
  360. */
  361. public static <T> List<T> list(T entity) {
  362. return list(Wrappers.lambdaQuery(entity));
  363. }
  364. /**
  365. * 根据entity中不为空的字段进行查询
  366. *
  367. * @param page 分页条件
  368. * @param entity 实体类
  369. * @param <T> entity
  370. * @return 列表数据
  371. * @since 3.5.3.2
  372. */
  373. public static <T> List<T> list(IPage<T> page, T entity) {
  374. return list(page, Wrappers.lambdaQuery(entity));
  375. }
  376. /**
  377. * 查询列表
  378. *
  379. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  380. */
  381. public static <T> List<Map<String, Object>> listMaps(AbstractWrapper<T, ?, ?> queryWrapper) {
  382. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectMaps(queryWrapper));
  383. }
  384. /**
  385. * @since 3.5.3.2
  386. * @param page 分页参数
  387. * @param queryWrapper queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  388. * @return 列表数据
  389. * @param <T> entity
  390. */
  391. public static <T> List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page, AbstractWrapper<T, ?, ?> queryWrapper) {
  392. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectMaps(page, queryWrapper));
  393. }
  394. /**
  395. * 查询所有列表
  396. *
  397. * @param entityClass 实体类
  398. * @see Wrappers#emptyWrapper()
  399. */
  400. public static <T> List<Map<String, Object>> listMaps(Class<T> entityClass) {
  401. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectMaps(null));
  402. }
  403. /**
  404. * 分页查询列表
  405. *
  406. * @param page 分页条件
  407. * @param entityClass 实体类
  408. * @param <T> entity
  409. * @return 列表数据
  410. * @since 3.5.3.2
  411. */
  412. public static <T> List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page, Class<T> entityClass) {
  413. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectMaps(page, null));
  414. }
  415. /**
  416. * 根据entity不为空的条件查询列表
  417. *
  418. * @param entity 实体类
  419. */
  420. public static <T> List<Map<String, Object>> listMaps(T entity) {
  421. return listMaps(Wrappers.lambdaQuery(entity));
  422. }
  423. /**
  424. * 根据entity不为空的条件查询列表
  425. *
  426. * @param page 分页条件
  427. * @param entity entity
  428. * @param <T> entity
  429. * @return 列表数据
  430. * @since 3.5.3.2
  431. */
  432. public static <T> List<Map<String, Object>> listMaps(IPage<? extends Map<String, Object>> page, T entity) {
  433. return listMaps(page, Wrappers.lambdaQuery(entity));
  434. }
  435. /**
  436. * 查询全部记录
  437. *
  438. * @param entityClass 实体类
  439. */
  440. public static <T> List<T> listObjs(Class<T> entityClass) {
  441. return listObjs(entityClass, i -> i);
  442. }
  443. /**
  444. * 根据 Wrapper 条件,查询全部记录
  445. *
  446. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  447. */
  448. public static <E, T> List<E> listObjs(AbstractWrapper<T, ?, ?> queryWrapper) {
  449. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectObjs(queryWrapper));
  450. }
  451. /**
  452. * 根据 Wrapper 条件,查询全部记录
  453. *
  454. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  455. * @param mapper 转换函数
  456. */
  457. public static <T, V> List<V> listObjs(AbstractWrapper<T, ?, ?> queryWrapper, SFunction<? super T, V> mapper) {
  458. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectList(queryWrapper).stream().map(mapper).collect(Collectors.toList()));
  459. }
  460. /**
  461. * 查询全部记录
  462. *
  463. * @param entityClass 实体类
  464. * @param mapper 转换函数
  465. */
  466. public static <T, V> List<V> listObjs(Class<T> entityClass, SFunction<? super T, V> mapper) {
  467. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectList(null).stream().map(mapper).collect(Collectors.toList()));
  468. }
  469. /**
  470. * 无条件翻页查询
  471. *
  472. * @param page 翻页对象
  473. * @param entityClass 实体类
  474. * @see Wrappers#emptyWrapper()
  475. */
  476. public static <T, E extends IPage<Map<String, Object>>> E pageMaps(E page, Class<T> entityClass) {
  477. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectMapsPage(page, null));
  478. }
  479. /**
  480. * 翻页查询
  481. *
  482. * @param page 翻页对象
  483. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  484. */
  485. public static <T, E extends IPage<Map<String, Object>>> E pageMaps(E page, AbstractWrapper<T, ?, ?> queryWrapper) {
  486. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectMapsPage(page, queryWrapper));
  487. }
  488. /**
  489. * 无条件翻页查询
  490. *
  491. * @param page 翻页对象
  492. * @param entityClass 实体类
  493. * @see Wrappers#emptyWrapper()
  494. */
  495. public static <T> IPage<T> page(IPage<T> page, Class<T> entityClass) {
  496. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectPage(page, null));
  497. }
  498. /**
  499. * 翻页查询
  500. *
  501. * @param page 翻页对象
  502. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  503. */
  504. public static <T> IPage<T> page(IPage<T> page, AbstractWrapper<T, ?, ?> queryWrapper) {
  505. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectPage(page, queryWrapper));
  506. }
  507. /**
  508. * 链式查询 普通
  509. *
  510. * @return QueryWrapper 的包装类
  511. */
  512. public static <T> QueryChainWrapper<T> query(Class<T> entityClass) {
  513. return ChainWrappers.queryChain(entityClass);
  514. }
  515. /**
  516. * kt链式查询
  517. *
  518. * @return KtQueryWrapper 的包装类
  519. */
  520. public static <T> KtQueryChainWrapper<T> ktQuery(Class<T> entityClass) {
  521. return ChainWrappers.ktQueryChain(entityClass);
  522. }
  523. /**
  524. * 链式查询 lambda 式
  525. * <p>注意:不支持 Kotlin </p>
  526. *
  527. * @return LambdaQueryWrapper 的包装类
  528. */
  529. public static <T> LambdaQueryChainWrapper<T> lambdaQuery(Class<T> entityClass) {
  530. return ChainWrappers.lambdaQueryChain(entityClass);
  531. }
  532. /**
  533. * 链式更改 普通
  534. *
  535. * @return UpdateWrapper 的包装类
  536. */
  537. public static <T> UpdateChainWrapper<T> update(Class<T> entityClass) {
  538. return ChainWrappers.updateChain(entityClass);
  539. }
  540. /**
  541. * kt链式更改
  542. *
  543. * @return KtUpdateWrapper 的包装类
  544. */
  545. public static <T> KtUpdateChainWrapper<T> ktUpdate(Class<T> entityClass) {
  546. return ChainWrappers.ktUpdateChain(entityClass);
  547. }
  548. /**
  549. * 链式更改 lambda 式
  550. * <p>注意:不支持 Kotlin </p>
  551. *
  552. * @return LambdaUpdateWrapper 的包装类
  553. */
  554. public static <T> LambdaUpdateChainWrapper<T> lambdaUpdate(Class<T> entityClass) {
  555. return ChainWrappers.lambdaUpdateChain(entityClass);
  556. }
  557. /**
  558. * <p>
  559. * 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
  560. * 此次修改主要是减少了此项业务代码的代码量(存在性验证之后的saveOrUpdate操作)
  561. * </p>
  562. *
  563. * @param entity 实体对象
  564. */
  565. public static <T> boolean saveOrUpdate(T entity, AbstractWrapper<T, ?, ?> updateWrapper) {
  566. return update(entity, updateWrapper) || saveOrUpdate(entity);
  567. }
  568. /**
  569. * 根据 Wrapper,查询一条记录
  570. *
  571. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  572. * @param mapper 转换函数
  573. */
  574. public static <T, V> V getObj(AbstractWrapper<T, ?, ?> queryWrapper, SFunction<? super T, V> mapper) {
  575. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> mapper.apply(baseMapper.selectOne(queryWrapper)));
  576. }
  577. /**
  578. * 从集合中获取实体类型
  579. *
  580. * @param entityList 实体集合
  581. * @param <T> 实体类型
  582. * @return 实体类型
  583. */
  584. protected static <T> Class<T> getEntityClass(Collection<T> entityList) {
  585. Class<T> entityClass = null;
  586. for (T entity : entityList) {
  587. if (entity != null && entity.getClass() != null) {
  588. entityClass = getEntityClass(entity);
  589. break;
  590. }
  591. }
  592. Assert.notNull(entityClass, "error: can not get entityClass from entityList");
  593. return entityClass;
  594. }
  595. /**
  596. * 从wrapper中尝试获取实体类型
  597. *
  598. * @param queryWrapper 条件构造器
  599. * @param <T> 实体类型
  600. * @return 实体类型
  601. */
  602. protected static <T> Class<T> getEntityClass(AbstractWrapper<T, ?, ?> queryWrapper) {
  603. Class<T> entityClass = queryWrapper.getEntityClass();
  604. if (entityClass == null) {
  605. T entity = queryWrapper.getEntity();
  606. if (entity != null) {
  607. entityClass = getEntityClass(entity);
  608. }
  609. }
  610. Assert.notNull(entityClass, "error: can not get entityClass from wrapper");
  611. return entityClass;
  612. }
  613. /**
  614. * 从entity中尝试获取实体类型
  615. *
  616. * @param entity 实体
  617. * @param <T> 实体类型
  618. * @return 实体类型
  619. */
  620. @SuppressWarnings("unchecked")
  621. protected static <T> Class<T> getEntityClass(T entity) {
  622. return (Class<T>) entity.getClass();
  623. }
  624. /**
  625. * 获取表信息,获取不到报错提示
  626. *
  627. * @param entityClass 实体类
  628. * @param <T> 实体类型
  629. * @return 对应表信息
  630. */
  631. protected static <T> TableInfo getTableInfo(Class<T> entityClass) {
  632. return Optional.ofNullable(TableInfoHelper.getTableInfo(entityClass)).orElseThrow(() -> ExceptionUtils.mpe("error: can not find TableInfo from Class: \"%s\".", entityClass.getName()));
  633. }
  634. }