最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot實(shí)現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫

 更新時間:2019年11月30日 08:16:59   作者:W_Meng_H  
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Spring boot + Spring data jpa + Thymeleaf
批量插入 + POI讀取 + 文件上傳

pom.xml:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi</artifactId>
 <version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi-ooxml</artifactId>
 <version>3.17</version>
</dependency>

upload.html:

<form enctype="multipart/form-data" method="post" action="/upload/excel">
 文件
 <input type="file" name="file" /> <input type="submit" value="上傳" />
</form>

如果自己的項(xiàng)目中使用了Spring  security,頁面提交文件之后,會出現(xiàn)403的錯誤,最快的解決辦法,如下:

http.csrf().ignoringAntMatchers("/upload/**").

在security的配置文件中,加入上邊的代碼即可。當(dāng)然還有其他的辦法,大家可在網(wǎng)上查找。 

Controller:

package org.meng.project.controller;
 
import org.meng.project.service.ExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 *<p><b>上傳Controller類</b></p>
 *<p> 上傳文件的Controller</p>
 * @Author MengMeng 
 * @Date 2018/10/6 </p>
 * @version: 0.1
 * @since JDK 1.80_144
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
 @Autowired
 private HttpServletRequest request;
 
 @Autowired
 private ExcelService excelService;
 
 
 //跳轉(zhuǎn)到上傳文件的頁面
 @RequestMapping(value = "", method = RequestMethod.GET)
 public String goUpload() {
 //跳轉(zhuǎn)到 templates 中tools目錄下的 upload.html
 return "tools/upload";
 }
 
 
 
 @RequestMapping(value = "/excel",method = RequestMethod.POST)
 public String upload(MultipartFile file, Model model) throws Exception {
 
 boolean flag = excelService.getExcel(file);
 
 if(flag){
  model.addAttribute("Message", "上傳成功");
 }else{
  model.addAttribute("Message", "上傳失敗");
 }
 
 return "tools/upload";
 }
 
}

Excel實(shí)體:

package org.meng.project.entity;
 
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
 
/**
 * <p><b>用戶實(shí)體類</b></p>
 * @ClassName User
 * @Author MengMeng 
 * @Date 2018/10/6 </p>
 * @Version: 0.1
 * @Since JDK 1.80_171
 */
 
@Entity
@Table(name = "test", schema = "project")
public class Excel implements Serializable {
 
 private static final long serialVersionUID = 1L;
 
 @Id
 @Column(length=36)
 private String id;
 
 @Column(length=45,nullable=false,unique=true)
 private String username;
 
 @Column(length=100,nullable=false,unique=true)
 private String email;
 
 @Column(length=45,nullable=false)
 private String password;
 
 @Column(length=45)
 private String role;
 
 public Excel() {
 }
 
 public Excel(Excel user){
  this.id = user.getId();
  this.username = user.getUsername();
  this.role = user.getRole();
  this.email = user.getEmail();
  this.password = user.getPassword();
 }
 
 //get 和 set
}

Service:

package org.meng.project.service;
 
import com.alibaba.fastjson.JSON;
 
import org.meng.project.entity.*;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
 
/**
 *<p><b>Excel的Service類接口</b></p>
 *<p> Excel的Service類接口,與Excel有關(guān)的業(yè)務(wù)邏輯方法</p>
 * @Author MengMeng 
 * @Date 2018/10/6 </p>
 * @version: 0.1
 * @since JDK 1.80_144
 */
public interface ExcelService {
 
 boolean getExcel(MultipartFile file) throws Exception;
 
}

ServiceImpl:

package org.meng.project.service;
 
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.meng.project.entity.*;
import org.meng.project.repository.ExcelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.*;
 
/**
 * <p><b>Excel的Service類</b></p>
 * <p>Excel的Service類,與User有關(guān)的業(yè)務(wù)邏輯方法</p>
 * @Author MengMeng
 * @Date 2018/10/6
 * @version: 0.1
 * @since JDK 1.80_144
 */
@Service
public class ExcelServiceImpl implements ExcelService {
 
 @Autowired
 private ExcelRepository excelRepository;
 
 @Override
 public boolean getExcel(MultipartFile file) throws Exception {
 // TODO Auto-generated method stub
 List<Excel> list = new ArrayList<Excel>();
 
  //1.得到上傳的表
  Workbook workbook2 = WorkbookFactory.create(file.getInputStream());
  //2、獲取test工作表
  Sheet sheet2 = workbook2.getSheet("test");
  //獲取表的總行數(shù)
  int num = sheet2.getLastRowNum();
  //System.out.println(num);
  //總列數(shù)
  int col = sheet2.getRow(0).getLastCellNum();
 
 
  //遍歷excel每一行
  for (int j = 0; j <= num; j++) {
  Row row1 = sheet2.getRow(j);
  
  //如果單元格中有數(shù)字或者其他格式的數(shù)據(jù),則調(diào)用setCellType()轉(zhuǎn)換為string類型
  Cell cell1 = row1.getCell(0);
  cell1.setCellType(CellType.STRING);
  //獲取表中第i行,第2列的單元格
  Cell cell2 = row1.getCell(1);
  //excel表的第i行,第3列的單元格
  Cell cell3 = row1.getCell(2);
  cell3.setCellType(CellType.STRING);
  Cell cell4 = row1.getCell(3);
  Cell cell5 = row1.getCell(4);
  
  //這里new 一個對象,用來裝填從頁面上傳的Excel數(shù)據(jù),字段根據(jù)上傳的excel決定
  Excel excel= new Excel();
  excel.setId(cell1.getStringCellValue());
  excel.setEmail(cell2.getStringCellValue());
  excel.setPassword(cell3.getStringCellValue());
  excel.setRole(cell4.getStringCellValue());
  excel.setUsername(cell5.getStringCellValue());
  list.add(excel);
 }
 
  excelRepository.saveAll(list);
 return true;
 }
}

注意:需統(tǒng)一數(shù)據(jù)庫和Excel表中數(shù)據(jù)值的類型,不然會出現(xiàn)Cannot get a STRING value from a NUMERIC cell等錯誤。

Repository:

package org.meng.project.repository;
 
import org.meng.project.entity.Excel;
import org.meng.project.repository.base.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
 
/**
 *<p><b>Excel的DAO類接口</b></p>
 *<p> Excel的DAO類接口,與Excel有關(guān)的持久化操作方法</p>
 * @Author MengMeng 
 * @Date 2018/10/6 </p>
 * @version: 0.1
 * @since JDK 1.80_144
 */
@Repository
public interface ExcelRepository extends BaseRepository<Excel, String> {
 
 
}

即可實(shí)現(xiàn)文件上傳:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中stream是什么及如何使用

    Java中stream是什么及如何使用

    在Java中,Stream(流)是一種用于操作集合(Collection)、數(shù)組等數(shù)據(jù)源的API,Stream的主要作用是進(jìn)行數(shù)據(jù)的轉(zhuǎn)換、篩選、聚合等操作,可以極大地簡化對數(shù)據(jù)的處理,本文給大家介紹Java中stream是什么?有什么作用?如何使用?感興趣的朋友一起看看吧
    2023-10-10
  • IDEA創(chuàng)建MyBatis配置文件模板的方法步驟

    IDEA創(chuàng)建MyBatis配置文件模板的方法步驟

    這篇文章主要介紹了IDEA創(chuàng)建MyBatis配置文件模板的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • maven項(xiàng)目切換JDK踩坑指南分享

    maven項(xiàng)目切換JDK踩坑指南分享

    文章介紹了如何在Windows系統(tǒng)中配置多版本JDK環(huán)境,并解決環(huán)境變量配置失效的問題,同時,還提供了在IntelliJ?IDEA中配置不同項(xiàng)目JDK版本的方法
    2024-11-11
  • Java使用FFmpeg提取音頻的詳細(xì)指南

    Java使用FFmpeg提取音頻的詳細(xì)指南

    FFmpeg 是一個開源的多媒體處理工具,支持視頻、音頻的編碼、解碼、轉(zhuǎn)換等多種功能,本文將詳細(xì)講解如何使用 FFmpeg 提取音頻,包括常見的音頻格式提取、音頻質(zhì)量調(diào)整、高級處理操作等,內(nèi)容淺顯易懂,適合初學(xué)者快速掌握,需要的朋友可以參考下
    2024-11-11
  • 關(guān)于mybatis resulttype 返回值異常的問題

    關(guān)于mybatis resulttype 返回值異常的問題

    這篇文章主要介紹了mybatis resulttype 返回值異常的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 怎樣將一個JAR包添加到Java應(yīng)用程序的Boot?Classpath中

    怎樣將一個JAR包添加到Java應(yīng)用程序的Boot?Classpath中

    本文文章給大家介紹如何將一個JAR包添加到Java應(yīng)用程序的Boot?Classpath中,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的的朋友參考下吧
    2023-11-11
  • java的Builder原理和實(shí)現(xiàn)詳解

    java的Builder原理和實(shí)現(xiàn)詳解

    大家好,本篇文章主要講的是java的Builder原理和實(shí)現(xiàn)詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • JAVA 多線程編程之CountDownLatch使用詳解

    JAVA 多線程編程之CountDownLatch使用詳解

    當(dāng)多個線程需要協(xié)調(diào)和同步執(zhí)行任務(wù)時,Java中的CountDownLatch(倒計(jì)時門閂)是一個常用的工具類,本文將介紹 CountDownLatch 的基本原理、用法以及示例代碼,需要的朋友可以參考下
    2023-05-05
  • SpringCloudAlibaba極簡入門整合Grpc代替OpenFeign的詳細(xì)過程

    SpringCloudAlibaba極簡入門整合Grpc代替OpenFeign的詳細(xì)過程

    本文介紹了如何將OpenFeign替換為Grpc進(jìn)行服務(wù)通信,并通過實(shí)際案例展示了如何在Spring?Boot項(xiàng)目中整合Grpc,Grpc提供了高性能、低延遲的服務(wù)間通信,而OpenFeign則注重簡化開發(fā)流程,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • 詳解spring整合hibernate的方法

    詳解spring整合hibernate的方法

    這篇文章主要介紹了spring整合hibernate的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評論

枝江市| 日土县| 都匀市| 西乌珠穆沁旗| 水富县| 玉林市| 德州市| 双江| 十堰市| 穆棱市| 措勤县| 莒南县| 德格县| 忻州市| 彩票| 宁南县| 乌鲁木齐市| 永春县| 镇巴县| 布尔津县| 诏安县| 奉新县| 昌乐县| 忻州市| 新河县| 民丰县| 香港 | 柘荣县| 漳浦县| 凤翔县| 莱芜市| 巴里| 通化市| 彭泽县| 衡阳县| 沧州市| 平南县| 防城港市| 涞水县| 惠东县| 永德县|