1.在Mapper中
/** * 批量添加实体 * @param equmentEntityList */ void addBatch(@Param("equmentEntityList") List<EqumentEntity> equmentEntityList);
2.在Mapper.xml
<insert id="addBatch" parameterType="java.util.List"> INSERT INTO new_equment( equipment_code, agency_id, equipment_name, equipment_type, equipment_communication, equipment_bluetooth, equipment_url ) VALUES <foreach collection ="equmentEntityList" item="item" separator =","> ( #{item.equipmentCode}, #{item.agencyId}, #{item.equipmentName}, #{item.equipmentType}, #{item.equipmentCommunication}, #{item.equipmentBluetooth}, #{item.equipmentUrl} ) </foreach > </insert>
3.在controller
// 1.用于批量插入 equmentEntityList = new ArrayList<>(); //3.将excel转换list集合 List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>(); listMap = ExcelUtils.excelToShopIdList(excelFile.getInputStream()); System.out.println(listMap.size()); //4.遍历excel集合并组装插入的集合 for (Map<String, Object> m : listMap) { entity = new EqumentEntity(); entity.setEquipmentType(equipmentType); entity.setEquipmentCommunication(equipmentCommunication); entity.setEquipmentBluetooth(equipmentBluetooth); entity.setAgencyId(agencyId); entity.setEquipmentCode(m.get("c0").toString());//设备编码 entity.setEquipmentUrl(m.get("c1").toString());//视频地址 equmentEntityList.add(entity); } //5.批量添加 service.addBatch(equmentEntityList);
标签:java,批量,插入,listMap,entity,equipment,equmentEntityList,item,new From: https://www.cnblogs.com/ckfuture/p/16813080.html