🔥 移除 mybatis IService 的依赖,将部分方法置入 ExtendService,防止 service 直接进行条件构造
This commit is contained in:
@@ -31,8 +31,8 @@ public class AccessLogAdminSaveThread extends AbstractQueueThread<AdminAccessLog
|
||||
|
||||
/**
|
||||
* 错误日志打印
|
||||
* @param e
|
||||
* @param list
|
||||
* @param e 错误堆栈
|
||||
* @param list 后台访问日志列表
|
||||
*/
|
||||
@Override
|
||||
public void errorLog(Throwable e, List<AdminAccessLog> list) {
|
||||
@@ -41,11 +41,11 @@ public class AccessLogAdminSaveThread extends AbstractQueueThread<AdminAccessLog
|
||||
|
||||
/**
|
||||
* 数据保存
|
||||
* @param list
|
||||
* @param list 后台访问日志列表
|
||||
*/
|
||||
@Override
|
||||
public void save(List<AdminAccessLog> list) throws Exception {
|
||||
adminAccessLogService.saveBatch(list);
|
||||
adminAccessLogService.saveBatchSomeColumn(list);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,10 +19,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author lingting 2020-08-10 17:21
|
||||
@@ -46,41 +43,17 @@ public class LovServiceImpl extends ExtendServiceImpl<LovMapper, Lov> implements
|
||||
if (!updateById(lov)) {
|
||||
return false;
|
||||
}
|
||||
List<Long> removeIds = new ArrayList<>();
|
||||
// 获取现有lov body
|
||||
// 更新 LovBody,先删除再插入
|
||||
String keyword = lov.getKeyword();
|
||||
List<LovBody> lovBodyList = bodyService.listByKeyword(keyword);
|
||||
|
||||
// 获取现有的id
|
||||
Set<Long> ids = bodyList.stream().map(LovBody::getId).collect(Collectors.toSet());
|
||||
// 筛选需要删除的id
|
||||
for (LovBody body : lovBodyList) {
|
||||
if (!ids.contains(body.getId())) {
|
||||
removeIds.add(body.getId());
|
||||
}
|
||||
}
|
||||
bodyService.removeByIds(removeIds);
|
||||
|
||||
bodyService.removeByKeyword(keyword);
|
||||
bodyList.forEach((body -> body.setKeyword(keyword)));
|
||||
bodyService.saveOrUpdateBatch(bodyList);
|
||||
|
||||
// 清空已有需要删除的id
|
||||
removeIds.clear();
|
||||
// 获取现有lov body
|
||||
List<LovSearch> lovSearchList = searchService.listByKeyword(keyword);
|
||||
|
||||
// 获取现有的id
|
||||
ids = searchList.stream().map(LovSearch::getId).collect(Collectors.toSet());
|
||||
// 筛选需要删除的id
|
||||
for (LovSearch search : lovSearchList) {
|
||||
if (!ids.contains(search.getId())) {
|
||||
removeIds.add(search.getId());
|
||||
}
|
||||
}
|
||||
searchService.removeByIds(removeIds);
|
||||
bodyService.saveBatchSomeColumn(bodyList);
|
||||
|
||||
// 更新 LovSearch,先删除再插入
|
||||
searchService.removeByKeyword(keyword);
|
||||
searchList.forEach((body -> body.setKeyword(keyword)));
|
||||
searchService.saveOrUpdateBatch(searchList);
|
||||
searchService.saveBatchSomeColumn(searchList);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ public class AnnouncementLoginEventListener {
|
||||
.filter(x -> filterMatched(x, filterAttrs)).map(Announcement::getId)
|
||||
.map(id -> userAnnouncementService.prodUserAnnouncement(userId, id)).collect(Collectors.toList());
|
||||
try {
|
||||
userAnnouncementService.saveBatch(userAnnouncements);
|
||||
userAnnouncementService.saveBatchSomeColumn(userAnnouncements);
|
||||
}
|
||||
catch (Exception exception) {
|
||||
log.error("用户公告保存失败:[{}]", userAnnouncements, exception);
|
||||
|
||||
@@ -105,7 +105,7 @@ public class PushEventListener {
|
||||
.setNeedBroadcast(false);
|
||||
messageDistributor.distribute(messageDO);
|
||||
|
||||
userAnnouncementService.saveBatch(userAnnouncements);
|
||||
userAnnouncementService.saveBatchSomeColumn(userAnnouncements);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -196,7 +196,7 @@ public class TemplateDirectoryEntryServiceImpl
|
||||
entry.setCreateTime(null);
|
||||
entry.setUpdateTime(null);
|
||||
}
|
||||
this.saveBatch(list);
|
||||
this.saveBatchSomeColumn(list);
|
||||
|
||||
// 3. =============== 获取新老ID的映射表,key: oldId, value: newId ==========
|
||||
Map<Integer, Integer> idMap = new HashMap<>();
|
||||
|
||||
@@ -41,5 +41,9 @@
|
||||
<groupId>com.hccake</groupId>
|
||||
<artifactId>ballcat-common-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,15 +1,116 @@
|
||||
package com.hccake.extend.mybatis.plus.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 以前继承 com.baomidou.mybatisplus.extension.service.IService 的实现类,现在继承当前类
|
||||
*
|
||||
* @author lingting 2020/7/21 9:58
|
||||
*/
|
||||
public interface ExtendService<T> extends IService<T> {
|
||||
public interface ExtendService<T> {
|
||||
|
||||
// ======= Copy From com.baomidou.mybatisplus.extension.service.IService 开始 =======
|
||||
|
||||
/**
|
||||
* 默认批次提交数量
|
||||
*/
|
||||
int DEFAULT_BATCH_SIZE = 1000;
|
||||
|
||||
/**
|
||||
* 插入一条记录(选择字段,策略插入)
|
||||
* @param entity 实体对象
|
||||
*/
|
||||
default boolean save(T entity) {
|
||||
return SqlHelper.retBool(getBaseMapper().insert(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 删除
|
||||
* @param id 主键ID
|
||||
*/
|
||||
default boolean removeById(Serializable id) {
|
||||
return SqlHelper.retBool(getBaseMapper().deleteById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除(根据ID 批量删除)
|
||||
* @param idList 主键ID列表
|
||||
*/
|
||||
default boolean removeByIds(Collection<? extends Serializable> idList) {
|
||||
if (CollectionUtils.isEmpty(idList)) {
|
||||
return false;
|
||||
}
|
||||
return SqlHelper.retBool(getBaseMapper().deleteBatchIds(idList));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 选择修改
|
||||
* @param entity 实体对象
|
||||
*/
|
||||
default boolean updateById(T entity) {
|
||||
return SqlHelper.retBool(getBaseMapper().updateById(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID 批量更新
|
||||
* @param entityList 实体对象集合
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
default boolean updateBatchById(Collection<T> entityList) {
|
||||
return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID 批量更新
|
||||
* @param entityList 实体对象集合
|
||||
* @param batchSize 更新批次数量
|
||||
*/
|
||||
boolean updateBatchById(Collection<T> entityList, int batchSize);
|
||||
|
||||
/**
|
||||
* 根据 ID 查询
|
||||
* @param id 主键ID
|
||||
*/
|
||||
default T getById(Serializable id) {
|
||||
return getBaseMapper().selectById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询(根据ID 批量查询)
|
||||
* @param idList 主键ID列表
|
||||
*/
|
||||
default List<T> listByIds(Collection<? extends Serializable> idList) {
|
||||
return getBaseMapper().selectBatchIds(idList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有
|
||||
*
|
||||
*/
|
||||
default List<T> list() {
|
||||
return getBaseMapper().selectList(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对应 entity 的 BaseMapper
|
||||
* @return BaseMapper
|
||||
*/
|
||||
BaseMapper<T> getBaseMapper();
|
||||
|
||||
/**
|
||||
* 获取 entity 的 class
|
||||
* @return {@link Class<T>}
|
||||
*/
|
||||
Class<T> getEntityClass();
|
||||
|
||||
// ^^^^^^ Copy From com.baomidou.mybatisplus.extension.service.IService end ^^^^^^
|
||||
|
||||
/**
|
||||
* 批量插入数据
|
||||
|
||||
@@ -1,19 +1,96 @@
|
||||
package com.hccake.extend.mybatis.plus.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
|
||||
import com.hccake.extend.mybatis.plus.mapper.ExtendMapper;
|
||||
import com.hccake.extend.mybatis.plus.service.ExtendService;
|
||||
import org.apache.ibatis.binding.MapperMethod;
|
||||
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 org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* 以前继承 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl 的实现类,现在继承本类
|
||||
*
|
||||
* @author lingting 2020/7/21 10:00
|
||||
*/
|
||||
public class ExtendServiceImpl<M extends ExtendMapper<T>, T> extends ServiceImpl<M, T> implements ExtendService<T> {
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ExtendServiceImpl<M extends ExtendMapper<T>, T> implements ExtendService<T> {
|
||||
|
||||
// ======= Copy From com.baomidou.mybatisplus.extension.service.impl.ServiceImpl 开始
|
||||
// =======
|
||||
|
||||
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<T> mapperClass = currentMapperClass();
|
||||
|
||||
protected Class<T> currentMapperClass() {
|
||||
return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 0);
|
||||
}
|
||||
|
||||
protected Class<T> currentModelClass() {
|
||||
return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取mapperStatementId
|
||||
* @param sqlMethod 方法名
|
||||
* @return 命名id
|
||||
* @since 3.4.0
|
||||
*/
|
||||
protected String getSqlStatement(SqlMethod sqlMethod) {
|
||||
return SqlHelper.getSqlStatement(mapperClass, sqlMethod);
|
||||
}
|
||||
|
||||
@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);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行批量操作
|
||||
* @param list 数据集合
|
||||
* @param batchSize 批量大小
|
||||
* @param consumer 执行方法
|
||||
* @param <E> 泛型
|
||||
* @return 操作结果
|
||||
* @since 3.3.1
|
||||
*/
|
||||
protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
|
||||
return SqlHelper.executeBatch(this.entityClass, this.log, list, batchSize, consumer);
|
||||
}
|
||||
|
||||
// ^^^^^^ Copy From com.baomidou.mybatisplus.extension.service.impl.ServiceImpl end
|
||||
// ^^^^^^
|
||||
|
||||
@Override
|
||||
public boolean saveBatchSomeColumn(Collection<T> list) {
|
||||
|
||||
Reference in New Issue
Block a user