序言:
我们在做项目的时候,经常会遇到,对数据的新增动作,如果数据量很少的情况下,单个新增对性能还好,但是一旦涉及到 大数据量,如十万,百万,千万,这个时候如果采用单个新增,那么整个系统都得瘫痪,响应时间 小时级别了,就可以出门左转,28路公交,火车站,回老家了。
吐槽:
对于项目而言,性能是最重要的,性能的快慢,对客户的体验和公司的名誉有着直接关系,要想代码写得好,首先围着性能跑。这几天一直在搞大数据插入的功能,自己总结了一些经验,供于各位大佬参考:
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("Dictionary_Info")
@ApiModel(value="DictionaryInfo对象", description="")
public class DictionaryInfo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主键ID")
@TableId(value = "ID", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "编码")
@TableField("DI_Code")
private String diCode;
@ApiModelProperty(value = "名称")
@TableField("DI_Name")
private String diName;
@ApiModelProperty(value = "描述")
@TableField("DI_Desc")
private String diDesc;
@ApiModelProperty(value = "类型")
@TableField("DI_Type")
private String diType;
@ApiModelProperty(value = "是否删除")
@TableField("IsDelete")
private Boolean isdelete;
@TableField("TS")
private Date ts;
}
INSERT INTO Dictionary_Info (DI_Code, DI_Name, DI_Desc, DI_Type, IsDelete, TS) VALUES(#{diCode}, #{diName}, #{diDesc}, #{diType}, #{isDelete, #{ts};
//服务类
public interface IDictionaryInfoService extends IService {
void insertBs(List list);
}
@Mapper
public interface DictionaryInfoMapper extends BaseMapper {
//MyBatis以集合方式批量新增
void insertBs(DictionaryInfomodel);
}
@Service
//服务实现类
public class DictionaryInfoServiceImpl extends ServiceImpl implements IDictionaryInfoService {
@Autowired
private SqlSessionFactory sqlSessionFactory;
//MyBatis以集合方式批量新增
public void insertBs(List list) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
list.stream().forEach(p -> baseMapper.insertBs(p));
sqlSession.commit();
sqlSession.clearCache();
}
public void TestInsert(){
long start = System.currentTimeMillis();
List dicList = new ArrayList<>();
DictionaryInfo dInfo;
for(int i = 0 ;i < 10000; i++) {
dInfo.setDiCode("1"+i);
dInfo.setDiDesc("2");
dInfo.setDiType("c");
dInfo.setIsdelete(false);
dInfo.setDiName("test");
dicList .add(dInfo);
}
//批量插入
insertBs(dicList);
}
}
@Service
//服务实现类
public class DictionaryInfoServiceImpl extends ServiceImpl implements IDictionaryInfoService {
@Autowired
private SqlSessionFactory sqlSessionFactory;
//MyBatis以集合方式批量新增
public void insertBs(List list) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
list.stream().forEach(p -> baseMapper.insertBs(p));
sqlSession.commit();
sqlSession.clearCache();
}
public void TestInsert(){
long start = System.currentTimeMillis();
List dicList = new ArrayList<>();
DictionaryInfo dInfo;
for(int i = 0 ;i < 10000; i++) {
dInfo.setDiCode("1"+i);
dInfo.setDiDesc("2");
dInfo.setDiType("c");
dInfo.setIsdelete(false);
dInfo.setDiName("test");
dicList .add(dInfo);
}
//批量插入
saveBatch(dicList);
}
}
//服务类
public interface IDictionaryInfoService extends IService {
void insertBs(List list);
}
public class BatchPlusSqlInjector extends DefaultSqlInjector {
@Override
//传入对象,类,反射对象的属性
public List getMethodList(Class> mapperClass, TableInfo tableInfo) {
List methodList = super.getMethodList(mapperClass, tableInfo);
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
return methodList;
}
}
@Configuration
public class MybatisPlusConfig {
public BatchPlusSqlInjector sqlInjector() {
return new BatchPlusSqlInjector();
}
}
public interface BatchPlusBaseMapper extends BaseMapper {
Integer insertBatchSomeColumn(Collection entityList);
}
@Mapper
public interface DictionaryInfoMapper extends EasyBaseMapper {
}
@Service
//服务实现类
public class DictionaryInfoServiceImpl extends ServiceImpl implements IDictionaryInfoService {
public void TestInsert(){
long start = System.currentTimeMillis();
List dicList = new ArrayList<>();
DictionaryInfo dInfo;
for(int i = 0 ;i < 10000; i++) {
dInfo.setDiCode("1"+i);
dInfo.setDiDesc("2");
dInfo.setDiType("c");
dInfo.setIsdelete(false);
dInfo.setDiName("test");
dicList .add(dInfo);
}
//批量插入
baseMapper.insertBatchSomeColumn(dicList )
}
}