位置: IT常识 - 正文

微服务 Spring Boot Mybatis-Plus 整合 EasyPOI 实现 Excel 一对多 导入(微服务springboot结构)

编辑:rootadmin
微服务 Spring Boot Mybatis-Plus 整合 EasyPOI 实现 Excel 一对多 导入 文章目录⛄引言一、EasyPOI 实现Excel 的一对多导入 -- 代码实现⛅需求说明⚡核心源码实现二、Easy POI 实现一对多导入 -- 测试三、效果图展示⛵小结⛄引言

推荐整理分享微服务 Spring Boot Mybatis-Plus 整合 EasyPOI 实现 Excel 一对多 导入(微服务springboot结构),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:微服务 spring security,微服务springboot结构,微服务 spring mvc,微服务springboot,微服务 spring mvc,微服务 spring,微服务springboot与springcloud,微服务springboot结构,内容如对您有帮助,希望把文章链接给更多的朋友!

Excel导入 是 开发中 很常用的 功能 ,本篇讲解 如何使用 Spring Boot + MyBatis -Plus 整合 EasyPOI 实现Excel 的一对多导入。

EasyPOI官网

一、EasyPOI 实现Excel 的一对多导入 – 代码实现⛅需求说明

采用 微服务 Spring Boot、Mybatis-Plus 整合 EasyPOI 实现Excel的一对多导入

Excel 导入 实现详细细节

前端采用 Vue+ElementUI 实现导入页面展示,要求弹出上传框、 展示导入模板、 并且要求文件手动上传后端导入 要求实现EasyPOI实现、采用工具类完成导入的集合映射要求使用 Mybatis-Plus 实现 批量导入⚡核心源码实现

Excel 一对多导入如下所示

以上的商品信息该如何进行映射呢?

EasyPOI为我们提供了专门的映射集合的注解,@ExcelCollection

表示一个集合,主要针对一对多的导出,比如一个老师对应多个科目,科目就可以用集合表示

采用 注解进行映射即可

后端源码

SysUserExcel

package com.chen.excel;import cn.afterturn.easypoi.excel.annotation.Excel;import cn.afterturn.easypoi.excel.annotation.ExcelCollection;import com.chen.entity.GoodsEntity;import lombok.Data;import java.util.Date;import java.util.List;@Datapublic class SysUserExcel { @Excel(name = "序号", orderNum = "0", format = "isAddIndex") private Integer index = 1; @Excel(name = "用户账号 *") private String username; @Excel(name = "真实姓名 *") private String realName; @Excel(name = "用户性别 *", replace = {"男_1", "女_2"}) private Integer gender; @Excel(name = "电子邮箱", width = 25) private String email; @Excel(name = "手机号码 *") private String mobile; // 注解映射商品信息集合 @ExcelCollection(name = "商品信息") private List<GoodsExcel> goodsList;}

GoodsExcel

package com.chen.excel;import cn.afterturn.easypoi.excel.annotation.Excel;import cn.afterturn.easypoi.excel.annotation.ExcelCollection;import com.chen.entity.GoodsEntity;import lombok.Data;import java.util.List;@Datapublic class GoodsExcel { @Excel(name = "商品编号", orderNum = "0", format = "isAddIndex") private Integer index = 1; @Excel(name = "商品名称") private String goodsName; @Excel(name = "商品价格") private Double goodsPrice; @Excel(name = "收货地址") private String address;}

SysUserService

public interface SysUserService extends IService<SysUserEntity> { ResultBean<PageInfo<SysUserDTO>> page(SysUserDTO param); ResultBean<Integer> insert(SysUserDTO param); ResultBean<Integer> importExcel(MultipartFile file);}

SysUserServiceImpl

@Overridepublic ResultBean<Integer> importExcel(MultipartFile file) { ImportParams importParams = new ImportParams(); //标题行设置为1行,默认是0,可以不设置;依实际情况设置。 importParams.setTitleRows(0); // 表头设置为1行 importParams.setHeadRows(2); try { //读取excel List<SysUserExcel> sysUserExcelList = ExcelImportUtil.importExcel(file.getInputStream(), SysUserExcel.class, importParams); batchInsert(sysUserExcelList); return ResultBean.create(0, "success"); } catch (Exception e) { log.error("导入 Excel 异常! e ==> {}", e); return ResultBean.create(10, "导入excel 异常!"+e.getMessage()); }}public void batchInsert(List<SysUserExcel> param) throws Exception{ //1.转换为dto集合 List<SysUserEntity> sysUserEntityList = BeanUtil.copyToList(param, SysUserEntity.class); //2.转换为商品实体集合 List<GoodsEntity> goodsEntityList = BeanUtil.copyToList(param.get(0).getGoodsList(), GoodsEntity.class); //3.转换集合 sysUserEntityList.stream().filter(obj -> obj.getUsername() != null) .forEach(obj -> obj.setPassword("123")); //4.批量保存 saveBatch(sysUserEntityList); // 保存用户id至商品id sysUserEntityList.stream().forEach(obj -> { goodsEntityList.stream().forEach(goods -> { goods.setUserId(obj.getId()); }); }); goodsService.saveBatch(goodsEntityList);}微服务 Spring Boot Mybatis-Plus 整合 EasyPOI 实现 Excel 一对多 导入(微服务springboot结构)

商品业务类

GoodsEntity

@Data@TableName("tb_goods")public class GoodsEntity { private Long id; private Long userId; private String goodsName; private Double goodsPrice; private String address; private Date createTime; private Date updateTime;}

GoodsService

import com.baomidou.mybatisplus.extension.service.IService;import com.chen.entity.GoodsEntity;/** * @author whc */public interface GoodsService extends IService<GoodsEntity> {}

GoodsServiceImpl

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.chen.entity.GoodsEntity;import com.chen.mapper.GoodsMapper;import com.chen.service.GoodsService;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;/** * @author whc */@Slf4j@Servicepublic class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {}

前端源码

SysUserList Vue 部分

<el-dialog :visible.sync="userVisibleOnly" width="780px" title="新增/编辑"> <el-form :model="sysUser" :rules="rules" ref="sysUserForm" label-width="120px"> <el-row> <el-form-item style="font-weight: bold" label="模板下载"> <el-button type="text">导入用户模板</el-button> </el-form-item> </el-row> <el-row> <el-form-item style="font-weight: bold" label="相关附件"> <el-upload class="upload-demo" :action="importUrl" :on-success="uploadSuccess" accept=".xlsx" ref="upload" multiple :limit="3" :auto-upload="false" :on-exceed="handleExceed"> <el-button type="primary" icon="el-icon-upload2" size="small">导入</el-button> </el-upload> <!-- <el-button type="primary" size="small">点击上传</el-button>--> </el-form-item> </el-row> </el-form> <span slot="footer" class="dialog-footer"> <el-button type="primary" size="mini" @click="clkBtnUpload">确定</el-button> <el-button type="warning" size="mini" @click="userVisibleOnly = false">取消</el-button> </span></el-dialog>

SysUserList JS 部分

data(){return:{userVisibleOnly: false}}metheds:{uploadSuccess(val) { if (val.code == 0) { this.$message.success("导入成功~"); this.getSysUserList(); } }, handleExceed(val) { }, clkBtnUpload() { this.submitUpload(); this.$message({ showClose: true, message: '上传成功,正在导入...' }); this.userVisibleOnly = false; }, submitUpload() { this.$refs.upload.submit(); },}

代码编写完毕,进行测试

二、Easy POI 实现一对多导入 – 测试

启动后端、前端 访问 : 127.0.0.1:80

导入测试

导入采用手动导入,点击确定后,将表单提交至后端

断点进入

我们看到已经完成了对象映射,直接进行映射对象,插入数据库即可

扩展知识:IDEA 断点快捷键 f8 按行进行断点调试、f9 进入下一次断点位置,若没有,直接结束

三、效果图展示

⛵小结

以上就是【Bug 终结者】对 微服务 Spring Boot Mybatis-Plus 整合 EasyPOI 实现 Excel 一对多 导入 的简单介绍,Excel一对多导入其实如此简单,Excel导入也是很常见的功能,希望带来这个案例,大家可以遇到需求时,进行举一反三,感谢支持!

如果这篇【文章】有帮助到你,希望可以给【Bug 终结者】点个赞👍,创作不易,如果有对【后端技术】、【前端领域】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 【Bug 终结者】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】💝💝💝!

本文链接地址:https://www.jiuchutong.com/zhishi/298671.html 转载请保留说明!

上一篇:【JavaScript数据结构与算法】字符串类(反转字符串中的单词)(javascript数据类型)

下一篇:openCV实践项目:拖拽虚拟方块(opencv项目开发实战)

  • 业务招待费福利费怎么算超支
  • 一般纳税人需要缴纳哪些税种
  • 所得税年报工资薪金支出怎么填
  • 支付土地使用权的会计处理
  • 开票资料都包括什么内容
  • 纳税人领购普通发票要求
  • 个人房屋租金收入怎么交税
  • 预交税费怎么做分录
  • 付出的房屋押金可以退吗
  • 企业收回报废产品合法吗
  • 股东分红如何合理节税
  • 印花税一定要贴花吗
  • 开一张增值税发票需要交哪些税
  • 补助及救济费用
  • 长期待摊费用账户按用途和结构分类应属于
  • 电费先付后开票怎么做账
  • 宽带合同要交印花税吗
  • 金税盘年费怎么做账
  • 成本费用怎么做分录
  • 公司股权转让协议标准范本
  • 2020年开公司优惠政策
  • 小规模销项负数发票怎么做账
  • 生产部门设备折旧费
  • 销售折让怎么做分录
  • 以报销形式发放的工资 劳动仲裁
  • php中字符串函数
  • php字符串函数有哪些
  • 生产车间闲置的固定资产
  • 新公司成立前期费用
  • mac桌面的东西怎么放回去
  • 公司报销客户的差旅费
  • 公司给员工发放的福利都要扣个税吗
  • 公证处会计分录
  • php imagestring
  • wordpress调用指定文章
  • 44岁就没有月经了正常吗
  • 大学生问卷spss数据分析作业
  • uniapp微信小程序兼容
  • 什么情况下开劳务费发票
  • 公司账号能给个人打款吗
  • 织梦系统如何更换网站内容
  • 如何恢复sql数据库
  • sqlserver 恢复数据库
  • 需要登记的权利
  • 应收账款科目的期末余额
  • 必要报酬率怎么求
  • 旅行社开具的发票抵扣
  • 公司员工报销没有发票挂内账有风险吗
  • 中小企业会计科目
  • 销售样品分录
  • 预缴税款留抵是什么意思
  • 非税收入包括哪几种
  • 费用报销冲抵借支
  • 公益性捐赠支出计入什么科目
  • 金三税务系统怎样修改财务人员跟办税人?
  • 建设项目开办费包括哪些
  • 收到同业清算互联前置如何入账
  • 高新企业研发项目规定几个
  • 新成立的公司如何申请资质
  • 房地产开发企业增值税税率
  • 红十字会是事业编还是行政编
  • 新公司建账初始数据可以全部为零吗
  • 安装fedora33
  • fedora怎么安装软件
  • freebsd怎么样
  • ubuntu rar压缩
  • ps到底怎么用
  • sendmail -t
  • w8系统怎么用
  • win10系统下如何打开internet(ISS)信息服务
  • apache2 rewrite
  • win10系统打游戏
  • linux常用命令chmod的使用
  • linux命令有啥用
  • css网页布局在线生成
  • 基于javascript的毕业设计选题
  • python time模块日期运算
  • JavaScript获取网页内容
  • javascript基础教程教材答案
  • 土地招标拍卖挂牌
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设