package com.baomidou.mybatisplus.extension.service.impl;
@SuppressWarnings("unchecked") public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
protected Log log = LogFactory.getLog(getClass());
@Autowired protected M baseMapper;
@Override public M getBaseMapper() { return baseMapper; }
protected Class<T> entityClass = currentModelClass();
@Override public Class<T> getEntityClass() { return entityClass; }
protected Class<M> mapperClass = currentMapperClass();
@Deprecated protected boolean retBool(Integer result) { return SqlHelper.retBool(result); }
protected Class<M> currentMapperClass() { return (Class<M>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServiceImpl.class, 0); }
protected Class<T> currentModelClass() { return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServiceImpl.class, 1); }
@Deprecated protected SqlSession sqlSessionBatch() { return SqlHelper.sqlSessionBatch(entityClass); }
@Deprecated protected void closeSqlSession(SqlSession sqlSession) { SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(entityClass)); }
@Deprecated protected String sqlStatement(SqlMethod sqlMethod) { return SqlHelper.table(entityClass).getSqlStatement(sqlMethod.getMethod()); }
@Transactional(rollbackFor = Exception.class) @Override public boolean saveBatch(Collection<T> entityList, int batchSize) { String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE); return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity)); }
protected String getSqlStatement(SqlMethod sqlMethod) { return SqlHelper.getSqlStatement(mapperClass, sqlMethod); }
@Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdate(T entity) { if (null != entity) { TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass); Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); String keyProperty = tableInfo.getKeyProperty(); Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); Object idVal = tableInfo.getPropertyValue(entity, tableInfo.getKeyProperty()); return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity); } return false; }
@Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) { TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); String keyProperty = tableInfo.getKeyProperty(); Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> { Object idVal = tableInfo.getPropertyValue(entity, keyProperty); return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity)); }, (sqlSession, entity) -> { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); sqlSession.update(getSqlStatement(SqlMethod.UPDATE_BY_ID), param); }); }
@Transactional(rollbackFor = Exception.class) @Override public boolean updateBatchById(Collection<T> entityList, int batchSize) { String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID); return executeBatch(entityList, batchSize, (sqlSession, entity) -> { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); sqlSession.update(sqlStatement, param); }); }
@Override public T getOne(Wrapper<T> queryWrapper, boolean throwEx) { if (throwEx) { return baseMapper.selectOne(queryWrapper); } return SqlHelper.getObject(log, baseMapper.selectList(queryWrapper)); }
@Override public Map<String, Object> getMap(Wrapper<T> queryWrapper) { return SqlHelper.getObject(log, baseMapper.selectMaps(queryWrapper)); }
@Override public <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) { return SqlHelper.getObject(log, listObjs(queryWrapper, mapper)); }
@Deprecated protected boolean executeBatch(Consumer<SqlSession> consumer) { return SqlHelper.executeBatch(this.entityClass, this.log, consumer); }
protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) { return SqlHelper.executeBatch(this.entityClass, this.log, list, batchSize, consumer); }
protected <E> boolean executeBatch(Collection<E> list, BiConsumer<SqlSession, E> consumer) { return executeBatch(list, DEFAULT_BATCH_SIZE, consumer); }
@Override public boolean removeById(Serializable id) { TableInfo tableInfo = TableInfoHelper.getTableInfo(getEntityClass()); if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) { return removeById(id, true); } return SqlHelper.retBool(getBaseMapper().deleteById(id)); }
@Override @Transactional(rollbackFor = Exception.class) public boolean removeByIds(Collection<?> list) { if (CollectionUtils.isEmpty(list)) { return false; } TableInfo tableInfo = TableInfoHelper.getTableInfo(getEntityClass()); if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) { return removeBatchByIds(list, true); } return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list)); }
@Override public boolean removeById(Serializable id, boolean useFill) { TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); if (useFill && tableInfo.isWithLogicDelete()) { if (!entityClass.isAssignableFrom(id.getClass())) { T instance = tableInfo.newInstance(); tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), id); return removeById(instance); } } return SqlHelper.retBool(getBaseMapper().deleteById(id)); }
@Override @Transactional(rollbackFor = Exception.class) public boolean removeBatchByIds(Collection<?> list, int batchSize) { TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); return removeBatchByIds(list, batchSize, tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()); }
@Override @Transactional(rollbackFor = Exception.class) public boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) { String sqlStatement = getSqlStatement(SqlMethod.DELETE_BY_ID); TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); return executeBatch(list, batchSize, (sqlSession, e) -> { if (useFill && tableInfo.isWithLogicDelete()) { if (entityClass.isAssignableFrom(e.getClass())) { sqlSession.update(sqlStatement, e); } else { T instance = tableInfo.newInstance(); tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), e); sqlSession.update(sqlStatement, instance); } } else { sqlSession.update(sqlStatement, e); } }); }
}
|