mybatis 批量上传

新建实体类
BatchModel ins=new BatchModel();
//把实体类装list里
List<BatchModel> batchModel = new ArrayList<>();
for (int i = 0; i < arr.size(); i++) {
ArrayList<String> row = arr.get(i);
ins.setA(subFileName);
ins.setB(row.get(0));
ins.setC(row.get(1));
ins.setD(row.get(2));
ins.setE(row.get(3));
batchModel.add(ins);
给实体类赋值
)
由于一些数据库的SQL对执行的SQL长度有限制,所以使用foreach元素的时候需要预估对象的长度

int partsize=80;//每次的数量
int index=0;//第几条
int iflg=0;//到几条
int recordcont=batchModel.size();//获取总数量
//index<总数量 循环到需要的数量

for(;index<recordcont;index+=partsize){
iflg=(index+partsize)>=recordcont ? recordcont:(index+partsize);
programService.storagekufripackage(batchModel.subList(index,iflg));
}
if(recordcont-iflg>0){

programService.storagekufripackage(batchModel.subList(index,iflg)); //开始插入数据库
}

//我用的 spring boot//   加入 @Param("param")注解

void test(@Param("param") List<BatchModel> batchModels);

 

//mybatis 里是sql

<insert id="test" parameterType="com.cck.model.auxiliary.test">
insert into ods_groupstock_actual (
filenames, rowno, typeclass,
jan, feb, mar, apr,
may, jun, jul, aug,
sep, oct, nev, dec,
total, upload_user
)
values
<foreach collection="param" item="veh" separator=",">
(
#{veh.A},#{veh.P}, #{veh.B},
#{veh.C},#{veh.D}, #{veh.E}, #{veh.F},
#{veh.G},#{veh.H}, #{veh.I}, #{veh.J},
#{veh.K}, #{veh.L}, #{veh.M}, #{veh.N},
#{veh.O}, #{veh.Q}
)
</foreach>
</insert>




转载于:https://www.cnblogs.com/cjb1/p/10262259.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值