国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 幫助中心 > 正文

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

2024-07-09 22:42:03
字體:
供稿:網(wǎng)友

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>

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

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

在security的配置文件中,加入上邊的代碼即可。當然還有其他的辦法,大家可在網(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實體:

package org.meng.project.entity; import javax.persistence.*;import java.io.Serializable;import java.util.Objects; /** * <p><b>用戶實體類</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}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长顺县| 简阳市| 咸丰县| 凉山| 香格里拉县| 嘉定区| 沈阳市| 余庆县| 安泽县| 南平市| 嘉禾县| 开封市| 陆丰市| 会昌县| 罗城| 台湾省| 万源市| 明光市| 闽清县| 靖远县| 会宁县| 阿克陶县| 峨眉山市| 白沙| 历史| 平潭县| 苍南县| 枝江市| 蕉岭县| 贺兰县| 桂东县| 沈丘县| 宜昌市| 二连浩特市| 陵川县| 龙川县| 洞口县| 方正县| 华容县| 定远县| 友谊县|