国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微服務 Spring Boot Mybatis-Plus 整合 EasyPOI 實現(xiàn) Excel 一對多 導入

這篇具有很好參考價值的文章主要介紹了微服務 Spring Boot Mybatis-Plus 整合 EasyPOI 實現(xiàn) Excel 一對多 導入。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

?引言

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導入excel一對多,前后端分離項目實戰(zhàn),mybatis,微服務,spring boot,數(shù)據(jù)庫,excel

以上的商品信息該如何進行映射呢?

EasyPOI為我們提供了專門的映射集合的注解,@ExcelCollection

  • 表示一個集合,主要針對一對多的導出,比如一個老師對應多個科目,科目就可以用集合表示

easypoi導入excel一對多,前后端分離項目實戰(zhàn),mybatis,微服務,spring boot,數(shù)據(jù)庫,excel

采用 注解進行映射即可

后端源碼

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

easypoi導入excel一對多,前后端分離項目實戰(zhàn),mybatis,微服務,spring boot,數(shù)據(jù)庫,excel

導入測試

easypoi導入excel一對多,前后端分離項目實戰(zhàn),mybatis,微服務,spring boot,數(shù)據(jù)庫,excel

導入采用手動導入,點擊確定后,將表單提交至后端

斷點進入

easypoi導入excel一對多,前后端分離項目實戰(zhàn),mybatis,微服務,spring boot,數(shù)據(jù)庫,excel

我們看到已經(jīng)完成了對象映射,直接進行映射對象,插入數(shù)據(jù)庫即可

擴展知識:IDEA 斷點快捷鍵 f8 按行進行斷點調(diào)試、f9 進入下一次斷點位置,若沒有,直接結束

三、效果圖展示

easypoi導入excel一對多,前后端分離項目實戰(zhàn),mybatis,微服務,spring boot,數(shù)據(jù)庫,excel

?小結

以上就是【Bug 終結者】對 微服務 Spring Boot Mybatis-Plus 整合 EasyPOI 實現(xiàn) Excel 一對多 導入 的簡單介紹,Excel一對多導入其實如此簡單,Excel導入也是很常見的功能,希望帶來這個案例,大家可以遇到需求時,進行舉一反三,感謝支持!

如果這篇【文章】有幫助到你,希望可以給【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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • spring boot mybatis-plus操作實踐

    1、先建一個數(shù)據(jù)表實體類 2、service目錄下新建接口IDeptService,然后擴展mp的IService 3、service目錄的impl目錄下新建類 DeptServiceImpl,擴展mp的ServiceImpl,實現(xiàn)接口IDeptService,然后注冊@Service 注解 4、DeptMapper 接口需要擴展mp的BaseMapper接口 5、調(diào)試一下看看是否可以使用

    2024年02月01日
    瀏覽(24)
  • spring boot集成mybatis-plus——Mybatis Plus 查詢數(shù)據(jù)(圖文講解)

    spring boot集成mybatis-plus——Mybatis Plus 查詢數(shù)據(jù)(圖文講解)

    ?更新時間 2023-01-03 16:07:12 大家好,我是小哈。 本小節(jié)中,我們將學習如何通過 Mybatis Plus 查詢數(shù)據(jù)庫表中的數(shù)據(jù)。 在前面小節(jié)中,我們已經(jīng)定義好了一個用于測試的用戶表, 執(zhí)行腳本如下: 定義一個名為? User ?實體類: 不明白 Mybatis Plus 實體類注解的小伙伴,可參考前面

    2024年02月02日
    瀏覽(23)
  • spring boot+mybatis-plus配置讀寫分離

    對于mysql主從機制的讀寫分離,對于寫操作寫入主庫,讀取操作讀取從庫 一、安裝依賴 二、配置文件配置數(shù)據(jù)源 三、在service,如果是讀取操作的話,則需要加上注解@DS(“slave”)即可,可看如下示例

    2024年01月25日
    瀏覽(22)
  • spring boot集成mybatis-plus——Mybatis Plus 批量 Insert_新增數(shù)據(jù)(圖文講解)

    spring boot集成mybatis-plus——Mybatis Plus 批量 Insert_新增數(shù)據(jù)(圖文講解)

    ?更新時間 2023-01-10 16:02:58 大家好,我是小哈。 本小節(jié)中,我們將學習如何通過 Mybatis Plus 實現(xiàn) MySQL 批量插入數(shù)據(jù)。 先拋出一個問題:假設老板給你下了個任務,向數(shù)據(jù)庫中添加 100 萬條數(shù)據(jù),并且不能耗時太久! 通常來說,我們向 MySQL 中新增一條記錄,SQL 語句類似如下:

    2024年02月04日
    瀏覽(28)
  • Spring Boot + MyBatis-Plus實現(xiàn)數(shù)據(jù)庫讀寫分離

    Spring Boot + MyBatis-Plus實現(xiàn)數(shù)據(jù)庫讀寫分離

    ??Spring Boot + MyBatis-Plus實現(xiàn)數(shù)據(jù)庫讀寫分離 ☆* o(≧▽≦)o *☆嗨~我是IT·陳寒?? ?博客主頁:IT·陳寒的博客 ??該系列文章專欄:架構設計 ??其他專欄:Java學習路線 Java面試技巧 Java實戰(zhàn)項目 AIGC人工智能 數(shù)據(jù)結構學習 ??文章作者技術和水平有限,如果文中出現(xiàn)錯誤,希望

    2024年02月05日
    瀏覽(44)
  • 從零開始學Spring Boot系列-集成MyBatis-Plus

    從零開始學Spring Boot系列-集成MyBatis-Plus

    在Spring Boot應用開發(fā)中,MyBatis-Plus是一個強大且易于使用的MyBatis增強工具,它提供了很多實用的功能,如代碼生成器、條件構造器、分頁插件等,極大地簡化了MyBatis的使用和配置。本篇文章將指導大家如何在Spring Boot項目中集成MyBatis-Plus。 首先,確保你已經(jīng)安裝了Java開發(fā)環(huán)

    2024年04月08日
    瀏覽(75)
  • spring boot集成mybatis-plus——Mybatis Plus 新增數(shù)據(jù)并返回主鍵 ID(圖文講解)

    spring boot集成mybatis-plus——Mybatis Plus 新增數(shù)據(jù)并返回主鍵 ID(圖文講解)

    ?更新時間 2023-01-10 15:37:37 大家好,我是小哈。 本小節(jié)中,我們將學習如何通過 Mybatis Plus 框架給數(shù)據(jù)庫表新增數(shù)據(jù),主要內(nèi)容思維導圖如下: Mybatis Plus 新增數(shù)據(jù)思維導圖 為了演示新增數(shù)據(jù),在前面小節(jié)中,我們已經(jīng)定義好了一個用于測試的用戶表, 執(zhí)行腳本如下: 定義一

    2024年02月02日
    瀏覽(40)
  • spring boot集成mybatis-plus——Mybatis Plus 多表聯(lián)查(包含分頁關聯(lián)查詢,圖文講解)...

    spring boot集成mybatis-plus——Mybatis Plus 多表聯(lián)查(包含分頁關聯(lián)查詢,圖文講解)...

    ?更新時間 2023-01-03 21:41:38 大家好,我是小哈。 本小節(jié)中,我們將學習如何通過 Mybatis Plus 實現(xiàn) 多表關聯(lián)查詢 ,以及 分頁關聯(lián)查詢 。 本文以 查詢用戶所下訂單 ,來演示 Mybatis Plus 的關聯(lián)查詢,數(shù)據(jù)庫表除了前面小節(jié)中已經(jīng)定義好的用戶表外,再額外創(chuàng)建一張訂單表,然后

    2024年02月01日
    瀏覽(25)
  • Spring Boot3整合MyBatis Plus

    Spring Boot3整合MyBatis Plus

    目錄 1.前置條件 2.導坐標 3.配置數(shù)據(jù)源 4.mybatis-plus基礎配置 5.配置mapper掃描路徑 6.MyBatis Plus代碼生成器整合 1.導坐標 2.編寫代碼生成邏輯 7.整合Druid連接池 已經(jīng)初始化好一個spring boot項目且版本為3X,項目可正常啟動 初始化教程: 新版idea創(chuàng)建spring boot項目-CSDN博客 https://blog

    2024年01月23日
    瀏覽(20)
  • Spring Boot + MyBatis-Plus 實現(xiàn) MySQL 主從復制動態(tài)數(shù)據(jù)源切換

    Spring Boot + MyBatis-Plus 實現(xiàn) MySQL 主從復制動態(tài)數(shù)據(jù)源切換

    MySQL 主從復制是一種常見的數(shù)據(jù)庫架構,它可以提高數(shù)據(jù)庫的性能和可用性。 動態(tài)數(shù)據(jù)源切換則可以根據(jù)業(yè)務需求,在不同場景下使用不同的數(shù)據(jù)源,比如在讀多寫少的場景下,可以通過切換到從庫來分擔主庫的壓力 。 在本文中,我們將介紹如何在 Spring Boot 中實現(xiàn) MySQL 動

    2024年02月19日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包