第一种 ---------------------------------------------------------------------------------
// 指定数据源
@Autowired
@Qualifier("fhycSqlSessionFactory")
SqlSessionFactory sqlSessionFactory;
// 说明 TMediumAndLongTradeDao.class Dao层
// 说明 sessionMapper.insertBatch(); .xml文件中的方法
// 开始 封装 对象
List<TMediumAndLongTrade> dataSource = saveBatchExcel(listData, versionNew);
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
try {
TMediumAndLongTradeDao sessionMapper = session.getMapper(TMediumAndLongTradeDao.class);
int session_Bath = 400;
if (dataSource.size() <= session_Bath) {
sessionMapper.insertBatch(dataSource); // yourEntityList 是包含多个实体的列表
session.commit();
} else {
int str = 0;
int end = session_Bath;
for (int i = 0; i < dataSource.size(); i++) {
List<TMediumAndLongTrade> tempLast = new ArrayList<>(dataSource.subList(str, dataSource.size()));
if (tempLast.size() <= session_Bath) {
sessionMapper.insertBatch(tempLast); // yourEntityList 是包含多个实体的列表
session.commit();
break;
}
List<TMediumAndLongTrade> temp = new ArrayList<>(dataSource.subList(str, end));
sessionMapper.insertBatch(temp); // yourEntityList 是包含多个实体的列表
session.commit();
str = str + session_Bath;
end = end + session_Bath;
}
}
} catch (Exception e) {
log.error(e.getMessage());
session.rollback();
return Result.failed("解析中长期交易Excel 失败!!");
} finally {
session.close();
}
第二种 ------------------------------------
用JDBC批量保存
@Autowired
private JdbcTemplate jdbcTemplate;
private void tMediumAndLongTrade_InsertBatch(List<TMediumAndLongTrade> dataSource) {
String sql = "INSERT INTO " + tableUtil.getPName() + ".T_MEDIUM_AND_LONG_TRADE (UUID, TRADE_NUM, NAME, SEND, RECEIVE, TRADE_TYPE, MCORHRS, START_DATE, END_DATE, MAX_VALUE, MIN_VALUE, " +
"AVG_VALUE, DAY_ELECTRICITY, VERSION_NUM, IS_EFFECT, CREATE_TIME, V00, V01, V02, V03, V04, V05, V06, V07, V08, V09, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23) " +
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
//定义插入参数数组
List<Object[]> insert_params = new ArrayList<>();
dataSource.forEach(tMediumAndLongTrade -> {
// TODO 因为 实体类字段顺序 和 数据库字段顺序不一致 且有多于字段,和防止后序实体类改动 ,进而手动赋值排序
Object[] item = new Object[]{
tMediumAndLongTrade.getUuid(),
tMediumAndLongTrade.getTradeNum(),
tMediumAndLongTrade.getName(),
tMediumAndLongTrade.getSend(),
tMediumAndLongTrade.getReceive(),
tMediumAndLongTrade.getTradeType(),
tMediumAndLongTrade.getMcorhrs(),
tMediumAndLongTrade.getStartDate(),
tMediumAndLongTrade.getEndDate(),
tMediumAndLongTrade.getMaxValue(),
tMediumAndLongTrade.getMinValue(),
tMediumAndLongTrade.getAvgValue(),
tMediumAndLongTrade.getDayElectricity(),
tMediumAndLongTrade.getVersionNum(),
tMediumAndLongTrade.getIsEffect(),
tMediumAndLongTrade.getCreateTime(),
tMediumAndLongTrade.getV00(),
tMediumAndLongTrade.getV01(),
tMediumAndLongTrade.getV02(),
tMediumAndLongTrade.getV03(),
tMediumAndLongTrade.getV04(),
tMediumAndLongTrade.getV05(),
tMediumAndLongTrade.getV06(),
tMediumAndLongTrade.getV07(),
tMediumAndLongTrade.getV08(),
tMediumAndLongTrade.getV09(),
tMediumAndLongTrade.getV10(),
tMediumAndLongTrade.getV11(),
tMediumAndLongTrade.getV12(),
tMediumAndLongTrade.getV13(),
tMediumAndLongTrade.getV14(),
tMediumAndLongTrade.getV15(),
tMediumAndLongTrade.getV16(),
tMediumAndLongTrade.getV17(),
tMediumAndLongTrade.getV18(),
tMediumAndLongTrade.getV19(),
tMediumAndLongTrade.getV20(),
tMediumAndLongTrade.getV21(),
tMediumAndLongTrade.getV22(),
tMediumAndLongTrade.getV23()
};
insert_params.add(item);
});
jdbcTemplate.batchUpdate(sql, insert_params);
}
大数据批量新增或者更新 几种方法
于 2024-09-12 14:32:52 首次发布