|
@@ -15,15 +15,6 @@
|
|
|
*/
|
|
|
package com.baomidou.mybatisplus.service.impl;
|
|
|
|
|
|
-import java.io.Serializable;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import org.apache.ibatis.logging.Log;
|
|
|
-import org.apache.ibatis.logging.LogFactory;
|
|
|
-import org.apache.ibatis.session.SqlSession;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-
|
|
|
import com.baomidou.mybatisplus.entity.TableInfo;
|
|
|
import com.baomidou.mybatisplus.enums.IdType;
|
|
|
import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
|
|
@@ -37,6 +28,14 @@ import com.baomidou.mybatisplus.toolkit.MapUtils;
|
|
|
import com.baomidou.mybatisplus.toolkit.ReflectionKit;
|
|
|
import com.baomidou.mybatisplus.toolkit.StringUtils;
|
|
|
import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
|
|
|
+import org.apache.ibatis.logging.Log;
|
|
|
+import org.apache.ibatis.logging.LogFactory;
|
|
|
+import org.apache.ibatis.session.SqlSession;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -48,240 +47,239 @@ import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
|
|
|
*/
|
|
|
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
|
|
|
- private static final Log logger = LogFactory.getLog(ServiceImpl.class);
|
|
|
-
|
|
|
- @Autowired
|
|
|
- protected M baseMapper;
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- protected Class<T> currentModleClass() {
|
|
|
- return ReflectionKit.getSuperClassGenricType(getClass(), 1);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * <p>
|
|
|
- * 批量操作 SqlSession
|
|
|
- * </p>
|
|
|
- */
|
|
|
- protected SqlSession sqlSessionBatch() {
|
|
|
- return SqlHelper.sqlSessionBatch(currentModleClass());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * <p>
|
|
|
- * TableId 注解存在更新记录,否插入一条记录
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @param entity
|
|
|
- * 实体对象
|
|
|
- * @return boolean
|
|
|
- */
|
|
|
- public boolean insertOrUpdate(T entity) {
|
|
|
- if (null != entity) {
|
|
|
- Class<?> cls = entity.getClass();
|
|
|
- TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
|
|
|
- if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
|
|
|
- Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
|
|
|
- if (StringUtils.checkValNull(idVal)) {
|
|
|
- return insert(entity);
|
|
|
- } else {
|
|
|
+ private static final Log logger = LogFactory.getLog(ServiceImpl.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ protected M baseMapper;
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ protected Class<T> currentModleClass() {
|
|
|
+ return ReflectionKit.getSuperClassGenricType(getClass(), 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * 批量操作 SqlSession
|
|
|
+ * </p>
|
|
|
+ */
|
|
|
+ protected SqlSession sqlSessionBatch() {
|
|
|
+ return SqlHelper.sqlSessionBatch(currentModleClass());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * TableId 注解存在更新记录,否插入一条记录
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @param entity
|
|
|
+ * 实体对象
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public boolean insertOrUpdate(T entity) {
|
|
|
+ if (null != entity) {
|
|
|
+ Class<?> cls = entity.getClass();
|
|
|
+ TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
|
|
|
+ if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
|
|
|
+ Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
|
|
|
+ if (StringUtils.checkValNull(idVal)) {
|
|
|
+ return insert(entity);
|
|
|
+ } else {
|
|
|
/* 特殊处理 INPUT 主键策略逻辑 */
|
|
|
- if (IdType.INPUT == tableInfo.getIdType()) {
|
|
|
- T entityValue = selectById((Serializable) idVal);
|
|
|
- if (null != entityValue) {
|
|
|
- return updateById(entity);
|
|
|
- } else {
|
|
|
- return insert(entity);
|
|
|
- }
|
|
|
- }
|
|
|
- return updateById(entity);
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new MybatisPlusException("Error: Can not execute. Could not find @TableId.");
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean insert(T entity) {
|
|
|
- return SqlHelper.retBool(baseMapper.insert(entity));
|
|
|
- }
|
|
|
-
|
|
|
- public boolean insertBatch(List<T> entityList) {
|
|
|
- return insertBatch(entityList, 30);
|
|
|
- }
|
|
|
-
|
|
|
- public boolean insertOrUpdateBatch(List<T> entityList) {
|
|
|
- return insertOrUpdateBatch(entityList, 30);
|
|
|
- }
|
|
|
-
|
|
|
- public boolean insertOrUpdateBatch(List<T> entityList, int batchSize) {
|
|
|
- if (CollectionUtils.isEmpty(entityList)) {
|
|
|
- throw new IllegalArgumentException("Error: entityList must not be empty");
|
|
|
- }
|
|
|
- try {
|
|
|
- SqlSession batchSqlSession = sqlSessionBatch();
|
|
|
- int size = entityList.size();
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
- insertOrUpdate(entityList.get(i));
|
|
|
- if (i % batchSize == 0) {
|
|
|
- batchSqlSession.flushStatements();
|
|
|
- }
|
|
|
- }
|
|
|
- batchSqlSession.flushStatements();
|
|
|
- } catch (Exception e) {
|
|
|
- logger.warn("Error: Cannot execute insertOrUpdateBatch Method. Cause:" + e);
|
|
|
- return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量插入
|
|
|
- *
|
|
|
- * @param entityList
|
|
|
- * @param batchSize
|
|
|
- * @return
|
|
|
- */
|
|
|
- public boolean insertBatch(List<T> entityList, int batchSize) {
|
|
|
- if (CollectionUtils.isEmpty(entityList)) {
|
|
|
- throw new IllegalArgumentException("Error: entityList must not be empty");
|
|
|
- }
|
|
|
- SqlSession batchSqlSession = sqlSessionBatch();
|
|
|
- try {
|
|
|
- int size = entityList.size();
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
- baseMapper.insert(entityList.get(i));
|
|
|
- if (i % batchSize == 0) {
|
|
|
- batchSqlSession.flushStatements();
|
|
|
- }
|
|
|
- }
|
|
|
- batchSqlSession.flushStatements();
|
|
|
- } catch (Exception e) {
|
|
|
- logger.warn("Error: Cannot execute insertBatch Method. Cause:" + e);
|
|
|
- return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public boolean deleteById(Serializable id) {
|
|
|
- return SqlHelper.retBool(baseMapper.deleteById(id));
|
|
|
- }
|
|
|
-
|
|
|
- public boolean deleteByMap(Map<String, Object> columnMap) {
|
|
|
- if (MapUtils.isEmpty(columnMap)) {
|
|
|
- throw new MybatisPlusException("deleteByMap columnMap is empty.");
|
|
|
- }
|
|
|
- return SqlHelper.retBool(baseMapper.deleteByMap(columnMap));
|
|
|
- }
|
|
|
-
|
|
|
- public boolean delete(Wrapper<T> wrapper) {
|
|
|
- return SqlHelper.retBool(baseMapper.delete(wrapper));
|
|
|
- }
|
|
|
-
|
|
|
- public boolean deleteBatchIds(List<? extends Serializable> idList) {
|
|
|
- return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
|
|
|
- }
|
|
|
-
|
|
|
- public boolean updateById(T entity) {
|
|
|
- return SqlHelper.retBool(baseMapper.updateById(entity));
|
|
|
- }
|
|
|
-
|
|
|
- public boolean update(T entity, Wrapper<T> wrapper) {
|
|
|
- return SqlHelper.retBool(baseMapper.update(entity, wrapper));
|
|
|
- }
|
|
|
-
|
|
|
- public boolean updateBatchById(List<T> entityList) {
|
|
|
- if (CollectionUtils.isEmpty(entityList)) {
|
|
|
- throw new IllegalArgumentException("Error: entityList must not be empty");
|
|
|
- }
|
|
|
- SqlSession batchSqlSession = sqlSessionBatch();
|
|
|
- try {
|
|
|
- int size = entityList.size();
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
- baseMapper.updateById(entityList.get(i));
|
|
|
- if (i % 30 == 0) {
|
|
|
- batchSqlSession.flushStatements();
|
|
|
- }
|
|
|
- }
|
|
|
- batchSqlSession.flushStatements();
|
|
|
- } catch (Exception e) {
|
|
|
- logger.warn("Error: Cannot execute insertBatch Method. Cause:" + e);
|
|
|
- return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- public T selectById(Serializable id) {
|
|
|
- return baseMapper.selectById(id);
|
|
|
- }
|
|
|
-
|
|
|
- public List<T> selectBatchIds(List<? extends Serializable> idList) {
|
|
|
- return baseMapper.selectBatchIds(idList);
|
|
|
- }
|
|
|
-
|
|
|
- public List<T> selectByMap(Map<String, Object> columnMap) {
|
|
|
- return baseMapper.selectByMap(columnMap);
|
|
|
- }
|
|
|
-
|
|
|
- public T selectOne(Wrapper<T> wrapper) {
|
|
|
- List<T> list = baseMapper.selectList(wrapper);
|
|
|
- if (CollectionUtils.isNotEmpty(list)) {
|
|
|
- int size = list.size();
|
|
|
- if (size > 1) {
|
|
|
- logger.warn(String.format("Warn: selectOne Method There are %s results.", size));
|
|
|
- }
|
|
|
- return list.get(0);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public Map<String, Object> selectMap(Wrapper<T> wrapper) {
|
|
|
- List<Map<String, Object>> list = baseMapper.selectMaps(wrapper);
|
|
|
- if (CollectionUtils.isNotEmpty(list)) {
|
|
|
- int size = list.size();
|
|
|
- if (size > 1) {
|
|
|
- logger.warn(String.format("Warn: selectMap Method There are %s results.", size));
|
|
|
- }
|
|
|
- return list.get(0);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public int selectCount(Wrapper<T> wrapper) {
|
|
|
- Integer result = baseMapper.selectCount(wrapper);
|
|
|
- return (null == result) ? 0 : result;
|
|
|
- }
|
|
|
-
|
|
|
- public List<T> selectList(Wrapper<T> wrapper) {
|
|
|
- return baseMapper.selectList(wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- public Page<T> selectPage(Page<T> page) {
|
|
|
- page.setRecords(baseMapper.selectPage(page, null));
|
|
|
- return page;
|
|
|
- }
|
|
|
-
|
|
|
- public List<Map<String, Object>> selectMaps(Wrapper<T> wrapper) {
|
|
|
- return baseMapper.selectMaps(wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
- public Page<Map<String, Object>> selectMapsPage(Page page, Wrapper<T> wrapper) {
|
|
|
- if (null != wrapper) {
|
|
|
- wrapper.orderBy(page.getOrderByField(), page.isAsc());
|
|
|
- }
|
|
|
- page.setRecords(baseMapper.selectMapsPage(page, wrapper));
|
|
|
- return page;
|
|
|
- }
|
|
|
-
|
|
|
- public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper) {
|
|
|
- if (null != wrapper) {
|
|
|
- wrapper.orderBy(page.getOrderByField(), page.isAsc());
|
|
|
- }
|
|
|
- page.setRecords(baseMapper.selectPage(page, wrapper));
|
|
|
- return page;
|
|
|
- }
|
|
|
+ if (IdType.INPUT == tableInfo.getIdType()) {
|
|
|
+ T entityValue = selectById((Serializable) idVal);
|
|
|
+ if (null != entityValue) {
|
|
|
+ return updateById(entity);
|
|
|
+ } else {
|
|
|
+ return insert(entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return updateById(entity);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new MybatisPlusException("Error: Can not execute. Could not find @TableId.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean insert(T entity) {
|
|
|
+ return SqlHelper.retBool(baseMapper.insert(entity));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean insertBatch(List<T> entityList) {
|
|
|
+ return insertBatch(entityList, 30);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean insertOrUpdateBatch(List<T> entityList) {
|
|
|
+ return insertOrUpdateBatch(entityList, 30);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean insertOrUpdateBatch(List<T> entityList, int batchSize) {
|
|
|
+ if (CollectionUtils.isEmpty(entityList)) {
|
|
|
+ throw new IllegalArgumentException("Error: entityList must not be empty");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ SqlSession batchSqlSession = sqlSessionBatch();
|
|
|
+ int size = entityList.size();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ insertOrUpdate(entityList.get(i));
|
|
|
+ if (i % batchSize == 0) {
|
|
|
+ batchSqlSession.flushStatements();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ batchSqlSession.flushStatements();
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.warn("Error: Cannot execute insertOrUpdateBatch Method. Cause:" + e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量插入
|
|
|
+ *
|
|
|
+ * @param entityList
|
|
|
+ * @param batchSize
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean insertBatch(List<T> entityList, int batchSize) {
|
|
|
+ if (CollectionUtils.isEmpty(entityList)) {
|
|
|
+ throw new IllegalArgumentException("Error: entityList must not be empty");
|
|
|
+ }
|
|
|
+ SqlSession batchSqlSession = sqlSessionBatch();
|
|
|
+ try {
|
|
|
+ int size = entityList.size();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ baseMapper.insert(entityList.get(i));
|
|
|
+ if (i % batchSize == 0) {
|
|
|
+ batchSqlSession.flushStatements();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ batchSqlSession.flushStatements();
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.warn("Error: Cannot execute insertBatch Method. Cause:" + e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean deleteById(Serializable id) {
|
|
|
+ return SqlHelper.retBool(baseMapper.deleteById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean deleteByMap(Map<String, Object> columnMap) {
|
|
|
+ if (MapUtils.isEmpty(columnMap)) {
|
|
|
+ throw new MybatisPlusException("deleteByMap columnMap is empty.");
|
|
|
+ }
|
|
|
+ return SqlHelper.retBool(baseMapper.deleteByMap(columnMap));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean delete(Wrapper<T> wrapper) {
|
|
|
+ return SqlHelper.retBool(baseMapper.delete(wrapper));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean deleteBatchIds(List<? extends Serializable> idList) {
|
|
|
+ return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean updateById(T entity) {
|
|
|
+ return SqlHelper.retBool(baseMapper.updateById(entity));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean update(T entity, Wrapper<T> wrapper) {
|
|
|
+ return SqlHelper.retBool(baseMapper.update(entity, wrapper));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean updateBatchById(List<T> entityList) {
|
|
|
+ if (CollectionUtils.isEmpty(entityList)) {
|
|
|
+ throw new IllegalArgumentException("Error: entityList must not be empty");
|
|
|
+ }
|
|
|
+ SqlSession batchSqlSession = sqlSessionBatch();
|
|
|
+ try {
|
|
|
+ int size = entityList.size();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ baseMapper.updateById(entityList.get(i));
|
|
|
+ if (i % 30 == 0) {
|
|
|
+ batchSqlSession.flushStatements();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ batchSqlSession.flushStatements();
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.warn("Error: Cannot execute insertBatch Method. Cause:" + e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public T selectById(Serializable id) {
|
|
|
+ return baseMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<T> selectBatchIds(List<? extends Serializable> idList) {
|
|
|
+ return baseMapper.selectBatchIds(idList);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<T> selectByMap(Map<String, Object> columnMap) {
|
|
|
+ return baseMapper.selectByMap(columnMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ public T selectOne(Wrapper<T> wrapper) {
|
|
|
+ List<T> list = baseMapper.selectList(wrapper);
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ int size = list.size();
|
|
|
+ if (size > 1) {
|
|
|
+ logger.warn(String.format("Warn: selectOne Method There are %s results.", size));
|
|
|
+ }
|
|
|
+ return list.get(0);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, Object> selectMap(Wrapper<T> wrapper) {
|
|
|
+ List<Map<String, Object>> list = baseMapper.selectMaps(wrapper);
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ int size = list.size();
|
|
|
+ if (size > 1) {
|
|
|
+ logger.warn(String.format("Warn: selectMap Method There are %s results.", size));
|
|
|
+ }
|
|
|
+ return list.get(0);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int selectCount(Wrapper<T> wrapper) {
|
|
|
+ return SqlHelper.retCount(baseMapper.selectCount(wrapper));
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<T> selectList(Wrapper<T> wrapper) {
|
|
|
+ return baseMapper.selectList(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Page<T> selectPage(Page<T> page) {
|
|
|
+ page.setRecords(baseMapper.selectPage(page, null));
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map<String, Object>> selectMaps(Wrapper<T> wrapper) {
|
|
|
+ return baseMapper.selectMaps(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
+ public Page<Map<String, Object>> selectMapsPage(Page page, Wrapper<T> wrapper) {
|
|
|
+ if (null != wrapper) {
|
|
|
+ wrapper.orderBy(page.getOrderByField(), page.isAsc());
|
|
|
+ }
|
|
|
+ page.setRecords(baseMapper.selectMapsPage(page, wrapper));
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper) {
|
|
|
+ if (null != wrapper) {
|
|
|
+ wrapper.orderBy(page.getOrderByField(), page.isAsc());
|
|
|
+ }
|
|
|
+ page.setRecords(baseMapper.selectPage(page, wrapper));
|
|
|
+ return page;
|
|
|
+ }
|
|
|
|
|
|
}
|