Db.java 25 KB

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