Db.java 21 KB

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