🎨 调整下方法位置,方便下次升级 mybatis-plus 比对代码
This commit is contained in:
@@ -3,10 +3,11 @@ package com.hccake.extend.mybatis.plus.service;
|
||||
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;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 以前继承 com.baomidou.mybatisplus.extension.service.IService 的实现类,现在继承当前类
|
||||
@@ -22,11 +23,6 @@ public interface ExtendService<T> {
|
||||
*/
|
||||
int DEFAULT_BATCH_SIZE = 1000;
|
||||
|
||||
/**
|
||||
* 默认一次批量插入的数量
|
||||
*/
|
||||
int DEFAULT_INSERT_BATCH_SIZE = 5000;
|
||||
|
||||
/**
|
||||
* 插入一条记录(选择字段,策略插入)
|
||||
* @param entity 实体对象
|
||||
@@ -35,6 +31,22 @@ public interface ExtendService<T> {
|
||||
return SqlHelper.retBool(getBaseMapper().insert(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入(批量)
|
||||
* @param entityList 实体对象集合
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
default boolean saveBatch(Collection<T> entityList) {
|
||||
return saveBatch(entityList, DEFAULT_BATCH_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入(批量)
|
||||
* @param entityList 实体对象集合
|
||||
* @param batchSize 插入批次数量
|
||||
*/
|
||||
boolean saveBatch(Collection<T> entityList, int batchSize);
|
||||
|
||||
/**
|
||||
* 根据 ID 删除
|
||||
* @param id 主键ID
|
||||
@@ -124,7 +136,7 @@ public interface ExtendService<T> {
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
default boolean saveBatchSomeColumn(Collection<T> list) {
|
||||
return this.saveBatchSomeColumn(list, DEFAULT_INSERT_BATCH_SIZE);
|
||||
return this.saveBatchSomeColumn(list, DEFAULT_BATCH_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,20 +148,4 @@ public interface ExtendService<T> {
|
||||
*/
|
||||
boolean saveBatchSomeColumn(Collection<T> list, int batchSize);
|
||||
|
||||
/**
|
||||
* 插入(批量)
|
||||
* @param entityList 实体对象集合
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
default boolean saveBatch(Collection<T> entityList) {
|
||||
return saveBatch(entityList, DEFAULT_INSERT_BATCH_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入(批量)
|
||||
* @param entityList 实体对象集合
|
||||
* @param batchSize 插入批次数量
|
||||
*/
|
||||
boolean saveBatch(Collection<T> entityList, int batchSize);
|
||||
|
||||
}
|
||||
|
||||
@@ -7,9 +7,6 @@ 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 java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import org.apache.ibatis.binding.MapperMethod;
|
||||
import org.apache.ibatis.logging.Log;
|
||||
import org.apache.ibatis.logging.LogFactory;
|
||||
@@ -17,6 +14,10 @@ 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.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* 以前继承 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl 的实现类,现在继承本类
|
||||
*
|
||||
@@ -66,6 +67,19 @@ public class ExtendServiceImpl<M extends ExtendMapper<T>, T> implements ExtendSe
|
||||
return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), ExtendServiceImpl.class, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入
|
||||
* @param entityList ignore
|
||||
* @param batchSize ignore
|
||||
* @return ignore
|
||||
*/
|
||||
@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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取mapperStatementId
|
||||
* @param sqlMethod 方法名
|
||||
@@ -123,12 +137,4 @@ public class ExtendServiceImpl<M extends ExtendMapper<T>, T> implements ExtendSe
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean saveBatch(Collection<T> entityList, int batchSize) {
|
||||
String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);
|
||||
return SqlHelper.executeBatch(this.entityClass, this.log, entityList, batchSize,
|
||||
((sqlSession, entity) -> sqlSession.insert(sqlStatement, entity)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user