Java使用Easyexcel导入大批量数据
文章目录[隐藏]
WPJAM TOCJava使用Easyexcel导入大批量数据
ps:博主测试的60w数据是没问题的
pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
一、Controller
value = "importExcel", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
(value = "导入社区")
(name = "文件", value = "file") })
({ (public Result importExcel( (value="file") MultipartFile file) throws ControllerException {
try {
Result result = communityService.importExcel(file);
return result;
} catch (Exception e) {
return Result.failed("导入失败");
}
}
二、实体类
/**
* 社区信息
*
* @author LXF
* @email
* @date 2021-11-10 15:29:38
*/
public class Community implements Serializable {
private static final long serialVersionUID = 1L;
using = ToStringSerializer.class)
( value = "ID", type = IdType.AUTO)
( private Long id;//
private String nation;// 国家
private String province;// 省市
private String city;
private String county;// 区县
private String town;// 镇
private String community;// 村民委员会
private String village;// 社区(村)
private String address;// 地址
private String headImgUrl;// 头像
private String telephone;// 电话
private String contact;// 联系人
private String content;// 简介
using = ToStringSerializer.class)
( private Long userId;// 用户编号
private String dmsAccount;// dms账号
private String workAccount;// 工作号
private Integer enabled;// 是否启用:1启用,0屏蔽
pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
( private Date createTime;// 创建时间
pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
( private Date updateTime;// 最近一次更新时间
using = ToStringSerializer.class)
( private Long operatorId;// 操作者
private String operateIp;// 最近一次更新操作的网址
using = ToStringSerializer.class)
( private Long version;// 版本号
}
public class CommunityImportBo implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
"国家")
( private String nation;
"省")
( private String province;
"市")
( private String city;
"区县")
( private String county;
"乡镇街道")
( private String town;
"居委会")
( private String community;
}
三、Service
/**
* 导入社区excel
*
* @param file 文件
* @param
* @return {@link Result}
*/
Result importExcel(MultipartFile file) throws ServiceException, IOException;
四、ServiceImpl
/**
* 导入社区excel
*
* @param file 文件
* @param
* @return {@link Result}
*/
public Result importExcel(MultipartFile file) throws ServiceException, IOException {
// 写法1:JDK8+ ,不用额外写一个DemoDataListener
// since: 3.0.0-beta1
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
// 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
//还有几种写法具体参照官网给的demo,大同小异
EasyExcel.read(file.getInputStream(), CommunityImportBo.class, new PageReadListener<CommunityImportBo>(dataList -> {
Community community = new Community();
community.setUserId(Long.valueOf("1277137734524300032"));
community.setEnabled(1);
community.setCreateTime(new Date());
List<Community> list = new ArrayList<>();
for (CommunityImportBo demoData : dataList) {
BeanUtil.copyProperties(demoData,community);
list.add(community);
}
communityDao.insertBatchTwo(list);
})).sheet().doRead();
return Result.succeed("导入成功");
}
}
五、Dao
//返回主键
@Options(useGeneratedKeys=true,keyProperty="id")
void insertBatchTwo(List<Community> list);
<insert id="insertBatchTwo" parameterType="java.util.List">
insert into community(
nation,
province,
city,
county,
town,
community,
village,
address,
head_img_url,
telephone,
contact,
content,
user_id,
dms_account,
work_account,
enabled,
create_time,
update_time,
operator_id,
operate_ip,
version)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{ item.nation},
#{ item.province},
#{ item.city},
#{ item.county},
#{ item.town},
#{ item.community},
#{ item.village},
#{ item.address},
#{ item.headImgUrl},
#{ item.telephone},
#{ item.contact},
#{ item.content},
#{ item.userId},
#{ item.dmsAccount},
#{ item.workAccount},
#{ item.enabled},
#{ item.createTime},
#{ item.updateTime},
#{ item.operatorId},
#{ item.operateIp},
#{ item.version}
)
</foreach>
</insert>
版权声明:
作者:admin
链接:http://lixiaofang.top/2022/02/15/java%e4%bd%bf%e7%94%a8easyexcel%e5%af%bc%e5%85%a5%e5%a4%a7%e6%89%b9%e9%87%8f%e6%95%b0%e6%8d%ae/
来源:码农日志
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
共有 0 条评论