?引言
Excel導入 是 開發(fā)中 很常用的 功能 ,本篇講解 如何使用 Spring Boot + MyBatis -Plus 整合 EasyPOI 實現(xiàn)Excel 的一對多導入。
EasyPOI官網(wǎng)
一、EasyPOI 實現(xiàn)Excel 的一對多導入 – 代碼實現(xiàn)
?需求說明
采用 微服務 Spring Boot、Mybatis-Plus 整合 EasyPOI 實現(xiàn)Excel的一對多導入
Excel 導入 實現(xiàn)詳細細節(jié)
- 前端采用 Vue+ElementUI 實現(xiàn)導入頁面展示,要求彈出上傳框、 展示導入模板、 并且要求文件手動上傳
- 后端導入 要求實現(xiàn)EasyPOI實現(xiàn)、采用工具類完成導入的集合映射
- 要求使用 Mybatis-Plus 實現(xiàn) 批量導入
?核心源碼實現(xiàn)
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;
@Data
public 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;
@Data
public 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
@Override
public 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);
}
商品業(yè)務類
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
@Service
public 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 實現(xiàn)一對多導入 – 測試
啟動后端、前端 訪問 : 127.0.0.1:80
導入測試
導入采用手動導入,點擊確定后,將表單提交至后端
斷點進入
我們看到已經(jīng)完成了對象映射,直接進行映射對象,插入數(shù)據(jù)庫即可
擴展知識:IDEA 斷點快捷鍵 f8 按行進行斷點調(diào)試、f9 進入下一次斷點位置,若沒有,直接結束
三、效果圖展示
?小結
以上就是【Bug 終結者】對 微服務 Spring Boot Mybatis-Plus 整合 EasyPOI 實現(xiàn) Excel 一對多 導入 的簡單介紹,Excel一對多導入其實如此簡單,Excel導入也是很常見的功能,希望帶來這個案例,大家可以遇到需求時,進行舉一反三,感謝支持!文章來源:http://www.zghlxwxcb.cn/news/detail-780609.html
如果這篇【文章】有幫助到你,希望可以給【Bug 終結者】點個贊??,創(chuàng)作不易,如果有對【后端技術】、【前端領域】感興趣的小可愛,也歡迎關注?????? 【Bug 終結者】??????,我將會給你帶來巨大的【收獲與驚喜】??????!文章來源地址http://www.zghlxwxcb.cn/news/detail-780609.html
到了這里,關于微服務 Spring Boot Mybatis-Plus 整合 EasyPOI 實現(xiàn) Excel 一對多 導入的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!