Db.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Copyright (c) 2011-2022, 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.service.IService;
  29. import org.apache.ibatis.binding.MapperMethod;
  30. import org.apache.ibatis.logging.Log;
  31. import org.apache.ibatis.logging.LogFactory;
  32. import java.io.Serializable;
  33. import java.util.*;
  34. import java.util.stream.Collectors;
  35. /**
  36. * 以静态方式调用Service中的函数
  37. *
  38. * @author VampireAchao
  39. * @since 2022-05-03
  40. */
  41. public class Db {
  42. private static final Log log = LogFactory.getLog(Db.class);
  43. private Db() {
  44. /* Do not new me! */
  45. }
  46. /**
  47. * 插入一条记录(选择字段,策略插入)
  48. *
  49. * @param entity 实体对象
  50. */
  51. public static <T> boolean save(T entity) {
  52. if (Objects.isNull(entity)) {
  53. return false;
  54. }
  55. @SuppressWarnings("unchecked")
  56. Class<T> entityClass = (Class<T>) entity.getClass();
  57. Integer result = SqlHelper.execute(entityClass, 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, LogFactory.getLog(Db.class), 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, LogFactory.getLog(Db.class), 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. @SuppressWarnings("unchecked")
  135. Class<T> entityClass = (Class<T>) entity.getClass();
  136. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.deleteById(entity)));
  137. }
  138. /**
  139. * 根据 entity 条件,删除记录
  140. *
  141. * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  142. */
  143. public static <T> boolean remove(AbstractWrapper<T, ?, ?> queryWrapper) {
  144. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> SqlHelper.retBool(baseMapper.delete(queryWrapper)));
  145. }
  146. /**
  147. * 根据 ID 选择修改
  148. *
  149. * @param entity 实体对象
  150. */
  151. public static <T> boolean updateById(T entity) {
  152. if (Objects.isNull(entity)) {
  153. return false;
  154. }
  155. @SuppressWarnings("unchecked")
  156. Class<T> entityClass = (Class<T>) entity.getClass();
  157. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.updateById(entity)));
  158. }
  159. /**
  160. * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
  161. *
  162. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  163. */
  164. public static <T> boolean update(AbstractWrapper<T, ?, ?> updateWrapper) {
  165. return update(null, updateWrapper);
  166. }
  167. /**
  168. * 根据 whereEntity 条件,更新记录
  169. *
  170. * @param entity 实体对象
  171. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
  172. */
  173. public static <T> boolean update(T entity, AbstractWrapper<T, ?, ?> updateWrapper) {
  174. return SqlHelper.execute(getEntityClass(updateWrapper), baseMapper -> SqlHelper.retBool(baseMapper.update(entity, updateWrapper)));
  175. }
  176. /**
  177. * 根据ID 批量更新
  178. *
  179. * @param entityList 实体对象集合
  180. */
  181. public static <T> boolean updateBatchById(Collection<T> entityList) {
  182. return updateBatchById(entityList, IService.DEFAULT_BATCH_SIZE);
  183. }
  184. /**
  185. * 根据ID 批量更新
  186. *
  187. * @param entityList 实体对象集合
  188. * @param batchSize 更新批次数量
  189. */
  190. public static <T> boolean updateBatchById(Collection<T> entityList, int batchSize) {
  191. Class<T> entityClass = getEntityClass(entityList);
  192. TableInfo tableInfo = getTableInfo(entityClass);
  193. String sqlStatement = SqlHelper.getSqlStatement(ClassUtils.toClassConfident(tableInfo.getCurrentNamespace()), SqlMethod.UPDATE_BY_ID);
  194. return SqlHelper.executeBatch(entityClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> {
  195. MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
  196. param.put(Constants.ENTITY, entity);
  197. sqlSession.update(sqlStatement, param);
  198. });
  199. }
  200. /**
  201. * 删除(根据ID 批量删除)
  202. *
  203. * @param list 主键ID或实体列表
  204. * @param entityClass 实体类
  205. */
  206. public static <T> boolean removeByIds(Collection<? extends Serializable> list, Class<T> entityClass) {
  207. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.deleteBatchIds(list)));
  208. }
  209. /**
  210. * 根据 columnMap 条件,删除记录
  211. *
  212. * @param columnMap 表字段 map 对象
  213. * @param entityClass 实体类
  214. */
  215. public static <T> boolean removeByMap(Map<String, Object> columnMap, Class<T> entityClass) {
  216. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.deleteByMap(columnMap)));
  217. }
  218. /**
  219. * TableId 注解存在更新记录,否插入一条记录
  220. *
  221. * @param entity 实体对象
  222. */
  223. public static <T> boolean saveOrUpdate(T entity) {
  224. if (Objects.isNull(entity)) {
  225. return false;
  226. }
  227. @SuppressWarnings("unchecked")
  228. Class<T> entityClass = (Class<T>) entity.getClass();
  229. TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
  230. Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
  231. String keyProperty = tableInfo.getKeyProperty();
  232. Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
  233. Object idVal = tableInfo.getPropertyValue(entity, tableInfo.getKeyProperty());
  234. return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal, entityClass)) ? save(entity) : updateById(entity);
  235. }
  236. /**
  237. * 根据 ID 查询
  238. *
  239. * @param id 主键ID
  240. * @param entityClass 实体类
  241. */
  242. public static <T> T getById(Serializable id, Class<T> entityClass) {
  243. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectById(id));
  244. }
  245. /**
  246. * 根据 Wrapper,查询一条记录 <br/>
  247. * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
  248. *
  249. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  250. */
  251. public static <T> T getOne(AbstractWrapper<T, ?, ?> queryWrapper) {
  252. return getOne(queryWrapper, true);
  253. }
  254. /**
  255. * 根据 Wrapper,查询一条记录
  256. *
  257. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  258. * @param throwEx 有多个 result 是否抛出异常
  259. */
  260. public static <T> T getOne(AbstractWrapper<T, ?, ?> queryWrapper, boolean throwEx) {
  261. Class<T> entityClass = getEntityClass(queryWrapper);
  262. if (throwEx) {
  263. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectOne(queryWrapper));
  264. }
  265. return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.getObject(log, baseMapper.selectList(queryWrapper)));
  266. }
  267. /**
  268. * 查询(根据 columnMap 条件)
  269. *
  270. * @param columnMap 表字段 map 对象
  271. * @param entityClass 实体类
  272. */
  273. public static <T> List<T> listByMap(Map<String, Object> columnMap, Class<T> entityClass) {
  274. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectByMap(columnMap));
  275. }
  276. /**
  277. * 查询(根据ID 批量查询)
  278. *
  279. * @param idList 主键ID列表
  280. * @param entityClass 实体类
  281. */
  282. public static <T> List<T> listByIds(Collection<? extends Serializable> idList, Class<T> entityClass) {
  283. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectBatchIds(idList));
  284. }
  285. /**
  286. * 根据 Wrapper,查询一条记录
  287. *
  288. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  289. */
  290. public static <T> Map<String, Object> getMap(AbstractWrapper<T, ?, ?> queryWrapper) {
  291. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> SqlHelper.getObject(log, baseMapper.selectMaps(queryWrapper)));
  292. }
  293. /**
  294. * 查询总记录数
  295. *
  296. * @param entityClass 实体类
  297. * @see Wrappers#emptyWrapper()
  298. */
  299. public static <T> long count(Class<T> entityClass) {
  300. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectCount(null));
  301. }
  302. /**
  303. * 根据 Wrapper 条件,查询总记录数
  304. *
  305. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  306. */
  307. public static <T> long count(AbstractWrapper<T, ?, ?> queryWrapper) {
  308. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectCount(queryWrapper));
  309. }
  310. /**
  311. * 查询列表
  312. *
  313. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  314. */
  315. public static <T> List<T> list(AbstractWrapper<T, ?, ?> queryWrapper) {
  316. Class<T> entityClass = getEntityClass(queryWrapper);
  317. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectList(queryWrapper));
  318. }
  319. /**
  320. * 查询所有
  321. *
  322. * @param entityClass 实体类
  323. * @see Wrappers#emptyWrapper()
  324. */
  325. public static <T> List<T> list(Class<T> entityClass) {
  326. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectList(null));
  327. }
  328. /**
  329. * 查询列表
  330. *
  331. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  332. */
  333. public static <T> List<Map<String, Object>> listMaps(AbstractWrapper<T, ?, ?> queryWrapper) {
  334. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectMaps(queryWrapper));
  335. }
  336. /**
  337. * 查询所有列表
  338. *
  339. * @param entityClass 实体类
  340. * @see Wrappers#emptyWrapper()
  341. */
  342. public static <T> List<Map<String, Object>> listMaps(Class<T> entityClass) {
  343. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectMaps(null));
  344. }
  345. /**
  346. * 查询全部记录
  347. *
  348. * @param entityClass 实体类
  349. */
  350. public static <T> List<T> listObjs(Class<T> entityClass) {
  351. return listObjs(entityClass, i -> i);
  352. }
  353. /**
  354. * 根据 Wrapper 条件,查询全部记录
  355. *
  356. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  357. */
  358. public static <T> List<Object> listObjs(AbstractWrapper<T, ?, ?> queryWrapper) {
  359. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectObjs(queryWrapper));
  360. }
  361. /**
  362. * 根据 Wrapper 条件,查询全部记录
  363. *
  364. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  365. * @param mapper 转换函数
  366. */
  367. public static <T, V> List<V> listObjs(AbstractWrapper<T, ?, ?> queryWrapper, SFunction<? super T, V> mapper) {
  368. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectList(queryWrapper).stream().map(mapper).collect(Collectors.toList()));
  369. }
  370. /**
  371. * 查询全部记录
  372. *
  373. * @param entityClass 实体类
  374. * @param mapper 转换函数
  375. */
  376. public static <T, V> List<V> listObjs(Class<T> entityClass, SFunction<? super T, V> mapper) {
  377. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectList(null).stream().map(mapper).collect(Collectors.toList()));
  378. }
  379. /**
  380. * 无条件翻页查询
  381. *
  382. * @param page 翻页对象
  383. * @param entityClass 实体类
  384. * @see Wrappers#emptyWrapper()
  385. */
  386. public static <T, E extends IPage<Map<String, Object>>> E pageMaps(E page, Class<T> entityClass) {
  387. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectMapsPage(page, null));
  388. }
  389. /**
  390. * 翻页查询
  391. *
  392. * @param page 翻页对象
  393. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  394. */
  395. public static <T, E extends IPage<Map<String, Object>>> E pageMaps(E page, AbstractWrapper<T, ?, ?> queryWrapper) {
  396. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectMapsPage(page, queryWrapper));
  397. }
  398. /**
  399. * 无条件翻页查询
  400. *
  401. * @param page 翻页对象
  402. * @param entityClass 实体类
  403. * @see Wrappers#emptyWrapper()
  404. */
  405. public static <T> IPage<T> page(IPage<T> page, Class<T> entityClass) {
  406. return SqlHelper.execute(entityClass, baseMapper -> baseMapper.selectPage(page, null));
  407. }
  408. /**
  409. * 翻页查询
  410. *
  411. * @param page 翻页对象
  412. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  413. */
  414. public static <T> IPage<T> page(IPage<T> page, AbstractWrapper<T, ?, ?> queryWrapper) {
  415. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> baseMapper.selectPage(page, queryWrapper));
  416. }
  417. /**
  418. * 链式查询 普通
  419. *
  420. * @return QueryWrapper 的包装类
  421. */
  422. public static <T> QueryChainWrapper<T> query(Class<T> entityClass) {
  423. return ChainWrappers.queryChain(entityClass);
  424. }
  425. /**
  426. * 链式查询 lambda 式
  427. * <p>注意:不支持 Kotlin </p>
  428. *
  429. * @return LambdaQueryWrapper 的包装类
  430. */
  431. public static <T> LambdaQueryChainWrapper<T> lambdaQuery(Class<T> entityClass) {
  432. return ChainWrappers.lambdaQueryChain(entityClass);
  433. }
  434. /**
  435. * 链式更改 普通
  436. *
  437. * @return UpdateWrapper 的包装类
  438. */
  439. public static <T> UpdateChainWrapper<T> update(Class<T> entityClass) {
  440. return ChainWrappers.updateChain(entityClass);
  441. }
  442. /**
  443. * 链式更改 lambda 式
  444. * <p>注意:不支持 Kotlin </p>
  445. *
  446. * @return LambdaUpdateWrapper 的包装类
  447. */
  448. public static <T> LambdaUpdateChainWrapper<T> lambdaUpdate(Class<T> entityClass) {
  449. return ChainWrappers.lambdaUpdateChain(entityClass);
  450. }
  451. /**
  452. * <p>
  453. * 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
  454. * 此次修改主要是减少了此项业务代码的代码量(存在性验证之后的saveOrUpdate操作)
  455. * </p>
  456. *
  457. * @param entity 实体对象
  458. */
  459. public static <T> boolean saveOrUpdate(T entity, AbstractWrapper<T, ?, ?> updateWrapper) {
  460. return update(entity, updateWrapper) || saveOrUpdate(entity);
  461. }
  462. /**
  463. * 根据 Wrapper,查询一条记录
  464. *
  465. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
  466. * @param mapper 转换函数
  467. */
  468. public static <T, V> V getObj(AbstractWrapper<T, ?, ?> queryWrapper, SFunction<? super T, V> mapper) {
  469. return SqlHelper.execute(getEntityClass(queryWrapper), baseMapper -> mapper.apply(baseMapper.selectOne(queryWrapper)));
  470. }
  471. /**
  472. * 从集合中获取实体类型
  473. *
  474. * @param entityList 实体集合
  475. * @param <T> 实体类型
  476. * @return 实体类型
  477. */
  478. @SuppressWarnings("unchecked")
  479. protected static <T> Class<T> getEntityClass(Collection<T> entityList) {
  480. Class<T> entityClass = null;
  481. for (T entity : entityList) {
  482. if (entity != null && entity.getClass() != null) {
  483. entityClass = (Class<T>) entity.getClass();
  484. break;
  485. }
  486. }
  487. Assert.notNull(entityClass, "error: can not get entityClass from entityList");
  488. return entityClass;
  489. }
  490. /**
  491. * 从wrapper中尝试获取实体类型
  492. *
  493. * @param queryWrapper 条件构造器
  494. * @param <T> 实体类型
  495. * @return 实体类型
  496. */
  497. @SuppressWarnings("unchecked")
  498. protected static <T> Class<T> getEntityClass(AbstractWrapper<T, ?, ?> queryWrapper) {
  499. Class<T> entityClass = queryWrapper.getEntityClass();
  500. if (entityClass == null) {
  501. T entity = queryWrapper.getEntity();
  502. if (entity != null) {
  503. entityClass = (Class<T>) entity.getClass();
  504. }
  505. }
  506. Assert.notNull(entityClass, "error: can not get entityClass from wrapper");
  507. return entityClass;
  508. }
  509. /**
  510. * 获取表信息,获取不到报错提示
  511. *
  512. * @param entityClass 实体类
  513. * @param <T> 实体类型
  514. * @return 对应表信息
  515. */
  516. protected static <T> TableInfo getTableInfo(Class<T> entityClass) {
  517. return Optional.ofNullable(TableInfoHelper.getTableInfo(entityClass)).orElseThrow(() -> ExceptionUtils.mpe("error: can not find TableInfo from Class: \"%s\".", entityClass.getName()));
  518. }
  519. }